Git Commands Short NMotes
Git Commands Short NMotes
2. Cloning a Repository
Clone an existing repository to your local machine:
bash
Copy code
git clone <repository_url>
bash
Copy code
git add <file_name> # To add a specific file
git add . # To add all modified files
4.
5. Commit Your Changes:
○ After staging, commit your changes with a clear and concise commit message.
This creates a snapshot of your changes in the local repository.
bash
6.
○ If you've made multiple changes and want to commit them all at once, you can
commit them after staging all files with git add ..
7. Check the Status:
○ It’s good practice to check the status of your repository before committing to see
which files have been staged or modified.
bash
Copy code
git status
8.
9. Optional: Check Commit History:
○ Before committing, if you want to review the last few commits, you can use:
bash
Copy code
git log --oneline
10. This gives a quick overview of recent commits and helps ensure you’re working in the
correct context.
Example Workflow:
Here’s the typical sequence of commands before checking out the main branch:
1.
2.
After this, you can proceed with checking out the main branch, pulling the latest changes, and
then merging as needed, as discussed in the previous steps.
bash
Copy code
git checkout main
2.
3. Pull the Latest Changes from the Main Branch:
○ Fetch the latest changes from the remote repository to ensure your local main
branch is up-to-date with the remote.
bash
Copy code
git pull origin main
4.
5. Checkout to Your Feature Branch:
○ Switch to the branch where you have been working and made your changes.
bash
Copy code
git checkout <your_feature_branch>
6.
7. Merge Main into Your Feature Branch (Optional but Recommended):
○ This step is important if there have been new commits to the main branch while
you were working on your feature branch. Merging the main branch into your
feature branch will ensure your branch is up-to-date with the latest changes in
the main branch and will help prevent conflicts.
bash
Copy code
git merge main
8.
○ If there are merge conflicts, you'll need to resolve them before proceeding.
9. Push Your Changes to Your Feature Branch:
○ Now, push your local commits to the remote repository in your feature branch.
bash
Copy code
git push origin <your_feature_branch>
10.
11. Create a Pull Request (PR) or Merge Request (MR):
○ After pushing the changes, you typically create a Pull Request (in GitHub) or
Merge Request (in GitLab) to merge your feature branch into the main branch.
This process is very common in teams following Gitflow or a similar branching strategy.