The ultimate git cheat sheet

The ultimate git cheat sheet

Using git can be daunting and overwhelming, especially if you are new into coding and development. Maintaining code and it’s history is often confusing, and with numerous commands available and multiple options available for each one of them, using git can seem scary at times.

Before starting my first job, I had minimal idea about git. Upon joining, I found that git is a monster that I have to tame daily, so I decided that I should learn the basics git, and conquer my fears for once and for all. I didn’t go through a formal course/lesson on git but instead I started gathering various commands and their use cases in my free time at work or whenever it became a necessity, and came up with a cheat sheet for me to use while working with git.

I have decided to share this cheat sheet with you so that you don’t have to scratch your head and scout the internet to get all the right command for your use.

Let’s dive right in!

Status

Status helps you to see what changes you have made since your last commit, which is very helpful before you decide to stage/unstage/commit your changes.

To view the status of your changes:

git status - Shows what the full status is, where the HEAD is, and what the changes are (staged, not staged, untracked files)

To view the simplified version of the same:

git status --short

Manipulating Changes

Occasionally you need to modify the changes you have made, choosing which files to commit and which ones to exclude. Fortunately, there are commands available for us to do the same so that we don’t have to undo the changes we made in a file which we are not ready to commit yet.

But before we go there, let’s understand what staging area means. Staging area can be described as the preview of your changes which are going to be included in your next commit. It is similar to the preview option before you submit an article.

Here are some commands you can use:

git add file - Adds the file to the staging area
git restore file - Discards changes made to the file
git restore --staged - Removes the file from the staging area

The file may be replaced with any file or directory path you want to modify.

Using wildcards with add/restore commands

You can use some wildcards with the commands you use to manipulate your changes to make your life easier. Here are a few examples:

git add src/**/*.py - Add all python files in src folder into staging area
git restore test/ - Discard all unstaged changes from the test directory
git restore --staged . - Discard all of the staged changes starting from the current directory

Note: These wildcards can be used with the diff command too, which is covered later in this story

Committing

Phew! A lot of effort was applied to make and modify our changes, and now it’s time for us to finally commit those changes.

Committing the changes available in the staging area:

git commit -m 'Commit message'

Adding meaningful and easy to understand commit messages is the best gift you can give to yourself and your fellow developers. Imagine going through a branch where you last committed six months ago, and finding commits like these:

Message

Class A fixed
Minor violations fixed
Added Class B

Imagine the effort and time you would need to invest to understand your own changes, let alone someone else going through the commits.

Writing improper commit messages makes it difficult to track and understand the changes we made, and defeats a major use case of source code management. You can define a commit message template for yourself which will help you write better commit messages.

Configuring the commit template:

git config commit.template PATH-TO-THE-TEMPLATE-FILE

To use the template file, just drop the -m while committing.

Here’s the commit template I use:

#Commit Type: [Feature/Bugfix/Hotfix/Refactor]
# Subject Line for the commit
# This commit will....# Body of the commit# Details of the change
-
# Why is it necessary? (Bug fix, feature, improvements?)
-
# How does the change address the issue? 
-
# What side effects does this change have?
-#Closing line for the commit [Rally Task Id/User Story/Jira Id], any reference links for the same

Lines starting with '#' are ignored

Feel free to use my template or create your own template.

Making amends to your commits

What do you do if you have made a commit and now you need to change/add something. Worry not, git has a feature for you!

git commit --amend - Modify a commit in the history. This will open the configured text editor pre-filled with the commit message we entered previously, we can also use -m option for the inline message.git commit --amend --no-edit - Edit the commit but not the commit message

Note: The — amend option only allows us to modify the last commit.

Theoretically, amended commits are actually entirely new commits, which replace the old ones. Therefore, there could be a case of conflicts.

Cherry Picking Commits

Cherry picking in git means to choose a commit present in one branch, and applying it to another branch. It is very different from the traditional way of merging/rebasing your branch, and has a very specific/niche application.

Here are the commands to do so:

git cherry-pick <commit-hash> - Pick a particular commit
git cherry-pick f - Drop a commit which was cherry picked (After moving to the desired branch)

Inspecting the differences

Inspecting differences helps you understand the changes between commits, branches and your local changes, which is a real life saver when you are coding a complicated piece of logic.

Here are some useful commands for the same:

git diff - Displays the not-staged changes in your working tree
git diff --staged - Displays the staged changes in your working tree
git diff dev - Displays the changes between the working tree and the dev branch
git diff branch1..branch2 - Displays changes between the branches branch1 and branch2

If you only want to inspect the file names, use the — name-only switch. If you need to know which files were modified, added, or deleted, use the — name-status switch.

Switch

Switch was introduced in Git 2.23. Switch is very similar to checkout, and you can read more on the differences between the two here.

Nevertheless, here are your switch commands:

git switch branch-name - Switch to branch-name branch
git switch -b branch-name - Creates a branch branch-name and switches to it
git switch - - Switch to a previous branch

Branches

git branch - Returns a list of local branches. Use -r to list only remote branches or -a to list both local and remote branches
git branch -d branch-name - Deletes branch-name if already merged. Use -D to force deletion.

Stash

Stashing is a great tool to use if you are working on multiple things/branches at a time, and you are just not ready yet to commit your changes.

Stash is used when you want to record the current state of your working directory, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

git stash - Saves the current changes to a temporary place that we can apply or restore later. (Only tracked files)
git stash -u # or --include-untracked - Includes untracked files
git stash -a # or --all - Includes all files(tracked, untracked and ignored files)

By default, the changes you stash are added to a stash list. You can view the list of available stashes with the help of the following command:

git stash list

Clear the list using this command:

git stash clear

Apply your changes in the stash through these commands:

git stash apply - To apply the most recent changes in the stash and keep the changes there so that we can apply it to different branches.
git stash pop - To apply and then remove the most recent changes from stash completely.

Revisiting your stashes and making sense of them can be a pain, so in order for them to make sense, you can add a meaningful message to them. To add a meaningful context to your stash, use this:

git stash save "Some meaningful context" - Stash with a proper message

You can remove a particular stash from the list using:

git stash drop stash@{<qualifier>} - Remove a particular stash

And finally:

git stash branch <branch_name> stash@{<qualifier>} - Create a new branch from the stash with qualifier and pop the stash changes onto it

Phew! That’s a lot to remember! But the good news is, you don’t have to remember any of this. Just bookmark this page and revisit it next time you need to use a command. You will start remembering the commands on your fingertips as you practice and use the commands in your projects.

Hope this was helpful. Thanks for reading!

Connect with me on LinkedIn: Rahul Singhania