Guide to Essential Git Commands

Guide to Essential Git Commands

  • Git is a free and open source distributed version control system.
  • Git’s purpose is to keep track of projects and files as they change over time with changes happening from different users.

List of some important git commands

  1. git init - Initialize a new git repository

    • Usage:

      $ git init
      Initialized empty Git repository in [path].
      
  2. git config - Configure git settings

    • Usage:

      $ git config --global user.name "Your Name"
      $ git config --global user.email "name@example.com"
      
    • Flags (Frequently used) :

      • --global - Configure settings for all the repo
      • --local - Configure settings for the current repo
      • --list - List all the configurations
    • You can learn more about it here

  3. git add - Add files to the staging area

    • Staging Area:

      • Preview the changes before committing.
      • You can verify the tracked and untracked files.
    • Usage:

      $ git add file1 file2
      #Add the mentioned files
      
      $ git add .
      # Add all files
      
  4. git status - Show the status of the repository

    • Usage:

      $ git status
      

      You may get a similar output like this. It shows which are the tracked and untracked files.

      status

      Note: You can always remove tracked files using git rm --cached file_name

  5. git commit - Commit changes to the repository

    • It captures a snapshot of a current modified files.
    • Usage:

      $ git commit -m "commit message"
      
      #-am flag is used to add and commit only the modified changes
      $ git commit -am "commit message"
      
  6. git clone - Clone a repository

    • It clones a remote repository into your local machine.

    • Usage:

      $ git clone https://github.com/username/repository_name
      
  7. git remote - Manage remote repositories

    • Usage:

      #Add a origin for a remote branch
      $ git remote add origin https://github.com/username/repository_name
      
      #Remove a remote branch
      $ git remote remove origin
      
    • You can learn more about it here
  8. git push - Push changes to a remote branch

    • push command is used to push the changes to a remote branch.
    • Usage:

      $ git push origin branch_name
      
      #Setting an upstream so that you can use git push next time.
      $ git push origin -u branch_name
      
  9. git fetch - Fetch changes from a remote branch

    • It fetches the changes from the remote branch , but doesn't merge them.

    • Usage:

      $ git fetch origin branch_name
      
  10. git pull - Pull changes from a remote branch

    • pull command is used to pull the changes from the remote branch and merge them.

    • Usage:

      $ git pull origin branch_name
      
  11. git branch - Manage branches

    • Usage:

      #Create a new branch
      $ git branch branch_name
      
      #Delete a branch
      $ git branch -d branch_name
      
  12. git merge - Merge branches

    • It merges the changes from one branch to another.

    • Usage:

      $ git merge branch_name
      
    • Usecase Scenario:
      • When a lot of developers are working on various branches. We can merge the changes from one branch to another.
    • Merge conflicts

      • When there is a conflict in the merge,you need to resolve it in your text editor and commit the changes and push it.

      • Example Usage

        $ git merge branch_name
        #Conflict occurs , resolve it , add and commit
        $ git commit -am "commit message"
        

        merge

  13. git log, git reflog - View the commit history

    • Usage:

      #View the commit history of a specific branch
      $ git log
      
      #View the commit history of all the branches
      $ git reflog
      
  14. git checkout, git switch - Switch branches

    • Usage:

      #Switch to  a branch
      $ git checkout branch_name
      $ git switch branch_name
      
      #Create a branch
      $ git checkout -b branch_name
      
      #Delete a branch
      $ git checkout -d branch_name
      
  15. git reset, git revert- Go back to previous state

    • Both reset and revert are used to go back to previous state but revert creates a new commit with the previous state and reset go back to previous state without creating a new commit. revert
    • Usage:

      $ git reset HEAD^
      $ git revert commit_hash
      
    • Frequently used:

      #reset to previous state also force deletes
      $ git reset --hard HEAD^ 
      
      #reset to previous state without deleting the staged files
      $ git reset --soft HEAD~n
      
  16. git rebase - Rebase a branch

    • A pretty useful commands which acts like git merge.
    • The main difference is that rebase have a straight line of commits.
    • It doesn't create a new commit for conflicts. rebase

    • Usage:

      $ git rebase branch_name
      
      ##After fixing the conflicts use 
      $ git rebase --continue
      
    • Interactive rebase

      You can specify the order of commits you want to rebase.

      $ git rebase -i
      
  17. git stash - Stash changes

    • It is like a temporary storage for a commit.You store those commit and pop them later.
    • Usage:

      $ git stash
      
    • Frequently used:

      #pop the last stash
      $ git stash pop
      
      #list all the stash
      $ git stash list
      
    • You can learn more about it here
  18. git cherry-pick - Pick a specific commit

    • Used to pick a specific commit on other branches and use them to our current branch.
    • Usage:

      $ git cherry-pick commit_hash
      
    • You can learn more about it here
  19. git diff - View changes in a file

    • Last but the most used and important command.
    • It helps to view all the changes in the file.

    • Usage:

      $ git diff
      

Conclusion

  • These are the most common and frequently used comment.
  • I hope you like this article.