Cheat Sheet – Git

Reading Time: 5 minutes

Hello everyone, in this blog I’m going to discuss different git commands. I hope this Cheat Sheet – Git will be helpful to you and will clear some doubts if you have regarding any command. So, let’s begin!

Installing Git

To start, first, we need to install git. Following are the simple commands to do that:

sudo apt-get update
sudo apt-get install git


After installing git you have to configure user email and user name so that you can access your GitHub.

git config --global "your email id"
git config --global "display name"

Also, you don’t have to configure git again and again. It is just a one-time configuration.

Initializing Git in Repository

Now, if you want to initialize a repository with git then use the following command:

git init

Adding files/changes to commit

The change in files/new file is unstaged i.e the git recognizes the change but does not make them ready for the next commit. For staging them for next commit we need to add the files.

Now you can either add all the file changes or add one at a time.

For adding all the changes following is the command:

git add .

For adding a single file, following is the command:

git add <file name>

To discard changes in uncommitted files/unstage files

For discarding the changes in the file you can use following command:

git restore <file name>

To unstage a file, do the following:

git restore --staged <file name>

To see the changes in file

For looking at all the changes in the file, that can be added or can be committed we can use the following command:

git status

Before adding any file, status will be like following:

After adding, it will be like the following:

After commit, it will be like the following:

Committing changes

To commit the staged files/changes you can use the following command:

git commit

With the above command, you will be prompted to enter a commit message that will indicate the changes done in this commit.

You can also commit the changes by using following:

git commit -m "<message>"

Adding remote repository

To connect your local repository to remote server use the following command:

git remote add <variable name> <remote repository url>

It is important to do so before pushing your code first time to a remote repository. Otherwise, you will see an error.

Pushing files

Pushing files can be considered as adding files to a remote repository or sending the committed changes to your remote repository.

git push <variable name> <branch name>

Creating a new branch

There are mainly two ways to create a new branch.

git branch <branch name>

The above will create a branch but you will have to move to that branch using git checkout. An alternative to that is to create a branch using the following:

git checkout -b <branch name>

The above command will create a branch and switch to that branch as well.

Listing all the branches

To list all the branches on local use the following command:

git branch

Switching between different versions

To switch from one branch/commit/file to other we can use the following command:

git checkout <branch name>/<commit id>

Fetching the changes

To get the changes that are in the remote repository but not in local we can use the following command:

git fetch

Merging the changes

Branches can be considered as different lines of development. To integrate the history of one branch into other or to merge them we can use the following command:

git merge <branch name>

This will merge the mentioned branch to current branch.

Pull command

To fetch and merge the changes from a remote repository in one go you can use the following command:

git pull <repository>

git pull = git fetch + git merge

Enlisting commits

To get the list of all the commits in your project you can use the following command:

git log

By using this you will get the information of all the commits in reverse chronological order i.e last commit will be shown first.

Cloning the repository

To get a remote repository/existing repository on local we can clone the repository. For that the command is:

git clone <repository url>

Temporarily saving the changes

For temporarily saving your changes in your working directory you can stash them. It will save them and revert them from your working directory as well. This is helpful if you want to switch to a different branch and work on that or if you want to make new changes to the same branch. You can use the following command for that:

git stash

To reapply the changes you can use the following:

git stash pop

This will remove the changes from the stash and reapply them on your working directory.

git stash apply

This will reapply the changes but won’t remove the changes from the stash. It is helpful if you want to apply the changes to multiple branches.

I hope this was helpful and if so just bookmark it as your Cheat Sheet – Git. Do leave your valuable feedback in the comment section.



Written by 

Muskan Gupta is a Software Consulatant at Knoldus Inc having an experience of 2 years. She is passionate about Scala development and she has sound knowledge of Scala, Akka, Akka-Streams, Alpakka and Akka Http. She has interest in exploring different technologies.