26 Must-Know Git Commands in 2021

In this article, we have listed 26 Git commands that every user must know.
26 Must Know Git commands with Git logo [raftlabs.co]


Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It's a system that tracks changes to our project files over time. It enables us to record project changes and go back to a specific version of the tracked files, at any given point in time.  This change history lives on your local machine and lets you revert to a previous version of your project with ease in case something goes wrong. Git makes collaboration easy. Knowing how to use Git is one of the most important skills for any developer nowadays - and it's definitely a great addition to your resume!

Here we will look at some of the git commands to get you started with git.

  1. Check your Git configuration

          The command below returns a list of information about your git configuration including user name and email.

          git config –l

  1. Setup your Git username

          git config --global user.name "your-name"

  1. Setup your Git user email

          git config --global user.email "your-email"

  1. Cache your login credentials in Git

          You can store login credentials in the cache so you don't have to type them in each time.

          git config --global credential.helper cache

  1. Initialize a Git repo

          The first step to creating your git repository is initializing it. You can initialize a git repo for a new or existing project using the following command:

          git init

  1. Add a file to the staging area

          The next step is to add the files in the project to the staging area. You can add a file using the following command:

           git add <filename>

          To add all the files in the current directory, use the following command:

          git add .

  1. Check a repository's status

          This command will show the status of the current repository including staged, unstaged, and untracked files.

          git status

  1. Commit changes in the editor

          The common way to commit changes made to your repo is with the –m option which lets you specify a shot summary for your commit message.

          git commit -m "your commit message here"

          You can add and commit files in one step using the following command:

          git commit -a –m "your commit message here"

  1. See your commit history in Git

          To see your commit history for the current repo, use the following command:

          git log

          To see your commit history including all the files and their changes, type:

          git log –p

  1. Remove tracked files from the current working tree in Git

          git rm filename

  1. Rename files in Git

          git mv oldfile newfile

  1. Ignore files in Git

          Create a .gitignore file and commit it.

  1. Rolling back commits

          Use the below command to roll back the last commit:

          git revert HEAD

          To revert an old commit in git:

          git revert commit_id_here

  1. Create a new branch in git

          Branch allows teams to work on the same code base in parallel. If you want to add some functionality to a branch without changing the actual code, you can create a branch and if your team likes the changes, you can merge the two branches together. To create a new branch, use the command below:

          git branch branch_name

  1. Switch to a newly created branch

          git checkout branch_name

  1. Delete a branch

          git branch –d branch_name

  1. Merge two branches in git

          git merge branch_name

  1. Show the commit log as a graph

          git log –graph –online

  1. Add a remote repository

          git add remote https://repo_here

  1. See all remote repositories

          git remote –v

  1. Push changes to a remote repo

          When all your work is ready to be saved on a remote repository, you can push all changes using the command below:

          git push

  1. Pull changes from a remote repo

          If other team members are working on your repository, you can retrieve the latest changes made to the remote repository with the command below:

          git pull

  1. Fetch remote repo changes

          This command will download the changes from a remote repo but will not perform a merge on your local branch (as git pull does that instead).

          git fetch

  1. Merge a remote repo with your local repo

          If the remote repository has changes you want to merge with your local, then use the following command

          git merge origin/main

  1. Push a new branch to a remote repo

          git push -u origin branch_name

  1. Clone a git repository

          The git clone command is used to download the source code from a remote repository. When you clone a repo, the code is automatically downloaded to your local machine. To clone a git repo, use the following command:

          git clone <https://url-of-the-repository>


If you are interested in joining an organization working on cutting-edge technologies, apply to raftlabs.co/careers