Comprehensive Guide to Essential Git Commands

Introduction: This documentation serves as a comprehensive guide to essential Git commands, providing step-by-step explanations and practical examples for each command.

Git Commands Overview:

1. git init:

  • Explanation: Initializes a new Git repository in the current directory.

  • Example:

      git init
    

    This command initializes a new Git repository in the current directory.

2. git add:

  • Explanation: Adds changes in the working directory to the staging area.

  • Example:

      git add .
    

    This command adds all changes in the current directory to the staging area.

3. git pull:

  • Explanation: Fetches changes from a remote repository and merges them into the current branch.

  • Example:

      git pull origin master
    

    This command fetches changes from the remote repository named "origin" and merges them into the current branch.

4. git commit:

  • Explanation: Command is used to record the changes made to the files in the staging area and save them as a new commit in the local repository.

  • Example:

      git commit -m "Add new feature"
    

    This command commits the staged changes to the local repository with the message "Add new feature".

5. git push:

  • Explanation: Pushes local commits to a remote repository.

  • Example:

      git push origin master
    

    This command pushes local commits to the remote repository named "origin" and the branch "master".

6. git remote add origin url:

  • Explanation: Adds a remote repository with the specified URL.

  • Command is used to add a new remote repository as a remote reference in your local Git repository.

  • Example:

      git remote add origin https://github.com/user/repo.git
    

    This command adds a remote repository named "origin" with the specified URL.

7. git merge:

  • Explanation: Merges changes from one branch into another.

  • Example:

      git merge feature_branch
    

    This command merges changes from the branch "feature_branch" into the current branch.

    If we are not bothered about commit history then we can use merge

Because One new commit_id will be added under the commit history when we merge any changes from one branch to the another branch

  • If we see here when we are merging changes from first-branch to the main branch the commits present in the first-branch added to the main branch and another new commit created for merge in the main branch that is called merge commit.

8. git rebase:

  • Explanation: Reapplies commits on top of another base tip.

  • Git rebase rewrites the commit history by reapplying commits on top of another branch, often used to maintain a clean and linear history or integrate changes from one branch to another.

  • Example:

      git rebase master
    

    Note: For the git merge another commit id will be added when merging changes from one branch to the another branch but with the git rebase only the commit ids in the one branch will be added to the another branch all linear commit ids will be added no new commit id will be added when merging changes from one branch to the another branch using rebase command.

9. git fetch:

  • Explanation:git fetch retrieves changes from the remote repository but does not automatically merge them into the current branch.

  • Instead, it updates the remote tracking branches (e.g., origin/master) in the local repository to reflect the changes on the remote repository.

  • After fetching, you can review the changes using tools like git diff or inspecting the remote tracking branches.

  • Example:

      git fetch master
    

    Unlike git pull, git fetch does not modify the working directory or the local branch. It only updates the remote tracking branches to reflect the state of the remote repository.

10. git pull:

  • Explanation:git pull is a combination of two commands: git fetch followed by git merge.

  • It retrieves changes from the remote repository and automatically merges them into the current branch in the local repository.

  • If there are no conflicting changes, git pull will perform a fast-forward merge, where the current branch is simply moved forward to incorporate the new commits.

  • However, if there are conflicting changes between the local and remote branches, Git will attempt to automatically merge them. If successful, the changes are applied, and a new merge commit is created. If unsuccessful, manual intervention is required to resolve the conflicts.

  • Example:

      git pull
    

If many merge conflicts when doing git pull we need to manually resolve those merge conflicts and pull those changes. We should open the file and resolve merge conflits.

Key Differences:

  • git pull automatically merges the changes into the current branch, while git fetch does not.

  • git pull is more convenient for quickly incorporating remote changes into your local branch but may lead to unexpected merge conflicts.

  • git fetch is safer as it allows you to review the changes before merging and gives you more control over the merge process.

In summary, git pull is a convenient way to fetch and merge changes from a remote repository into the current branch, while git fetch allows you to fetch changes without immediately merging them, providing more control and flexibility in managing remote updates.

11. git diff:

  • Explanation: Shows changes between commits, commit and working tree, etc.

  • Example:

      git diff HEAD~1 HEAD
    

    This command shows the differences between the last two commits.

    We can also use git diff after doing any changes in the particular branch and before doing git add . means we can check the difference of the file changes before adding it to the staging area.

11. git reset --hard commit_id:

  • Explanation: Resets the current branch to a specific commit and discards all changes.

  • Example:

      git reset --hard abc123
    

    This command resets the current branch to the commit with the ID "abc123" and discards all changes.

  • First compare the changes for the first-file.txt in the first-branch and second-branch.

  • And checkout the the second branch and take the commit-id.

  • Now checkout to first-branch and use the commit-id with the rest --hard then all changes in the first-branch will be removed and hardly updated with the second-branch changes.

12. git fork:

  • Explanation: Creates a fork of a repository on GitHub.

  • Example: This command is performed on the GitHub website by clicking the "Fork" button on the repository's page.

  • Fork is used to create the copy of the repository.

  • Git is a version control system where git code is present in one place but we can create multiple copies or replies of the repository.

  • Where we can see Fork 3.4K means that many people forked the repository means copied or replicated the complete repository in their GitHub account without disturbing the main repository. They can do the changes anything they want in the forked GitHub repository

  • If the main repository gets updated, this forked repository won’t get updated automatically until I do that explicitly.

13. git clone:

  • Explanation: Clones a repository into a new directory.

  • Example:

      git clone https://github.com/user/repo.git
    

    This command clones the repository from the specified URL into a new directory.

14. git cherry-pick:

  • Explanation: Picks a commit from another branch and applies it to the current branch.

  • Example:

      git cherry-pick abc123
    

    This command picks the commit with the ID "abc123" from another branch and applies it to the current branch.

15. git log:

  • Explanation: Shows commit logs.

  • Example:

      git log
    

    This command shows the commit logs in the current branch.

At the End of all the commit history we should press "q" for coming out of the prompt.

16. git log --oneline:

  • Explanation: Shows commit logs with abbreviated SHA-1 and commit messages.

  • Example:

      git log --oneline
    

    This command shows the commit logs in a concise one-line format.

17. git branch -r:

  • Explanation: If we want to see all the remote branches and which are already present locally we can use the below command.

  • Example:

      git branch -r
    

    This command shows all the remote and local branches.