One last ‘Git Cheat-sheet of Commands’

Mathiazhagan Dinesh
5 min readApr 17, 2021

Yes, there is plenty of git cheat-sheet published all over the internet. Still, not every software engineer use git commands efficiently/do git operations only through IDEs/not at all using git. So, I wanted to create a post to be a crisp and one last stop for the basic git commands.

Photo by fauxels from Pexels
Photo by fauxels from Pexels

First of all, why do we need git?

I think Nadhif Suyudi explained that well here.

Doing git operations through IDEs isn’t enough?

It is enough. But remember the old days in school? We use to write everything we learn, which helped us to remember. Git commands are very similar to that, every time you use the git command, you will start remembering it and you will be on track while coding. That’s why I strongly recommend using git commands.

No more blah blah blah…

To generate your SSH Public Key

ssh-keygen -o

To check your SSH Public Key

cat ~/.ssh/id_rsa.pub

To check user name and user email

git config --global user.name
git config --global user.email

Note: global option is used to check and set things globally. Without the global option, you can check and set things on the current project alone.

To set/change user name and user

git config --global user.name <name>
git config --global user.email <email>
Eg:
git config --global user.name "Mathiazhagan D"
git config --global user.email thisisnotmymailid@gmail.com

To check git editor

git config — global core.editor

To set/change git editor

git config --global core.editor <editor-name>Eg:
git config --global core.editor vim

Note: Using vim will be very useful

To clone a git project

git clone <remote-repo-url>Eg:
git clone git@gitlab.com:kotlin-samples/hello-world.git

To find all branches and current branch

git branch

To find all branches including remote branches

git branch -a

To find all tags

git tag

To clone a specific branch of a project

git clone --branch <branch-name> <remote-repo-url>
git clone -b <branch-name> <remote-repo-url>
Eg:
git clone -b master git@gitlab.com:kotlin-samples/hello-world.git

To clone a specific tag of a project

git clone --depth 1 --branch <tag-name> <remote-repo-url>Eg:
git clone --depth 1 --branch release_v1 git@gitlab.com:kotlin-samples/hello-world.git

To change/checkout to a branch

git checkout <other-branch-name>Eg:
git checkout bug_fix_1

To create a new branch

git checkout -b <new-branch-name>Eg:
git checkout -b bug_fix_2

To create a new tag

git tag -a release_v1 -m 'Release Version 1.0'

To rename a branch

If you are on a different branch:

git branch -m <old-branch-name> <new-branch-name>Eg:
git branch -m bug_fix_2 big_fix_3

If you are on the branch you want to rename:

git branch -m <new-branch-name>Eg:
git branch -m bug_fix_3

To delete a branch

git branch -D <branch-name>Eg:
git branch -D bug_fix_3

To check git log

git log
git log -n<number-of-commits>
git log --oneline
git log --oneline -n<number-of-commits>
git log --graph
git log --graph -n<number-of-commits>
Eg:
git log --oneline -n10

To check the log of a file

git log <file>Eg:
git log app/src/main/java/com/mathi/helloworld/MainActivity.kt

To find who changed specific lines of a file

git blame <file>Eg:
git blame app/src/main/java/com/mathi/helloworld/MainActivity.kt

Add git in an existing project

Go to the project location in the terminal, then use the following commands

git init
git remote add <remote-name> <remote-url>
git pull <remote-name> <master-branch>
git checkout <your-branch>
git initgit remote add origin git@gitlab.com:kotlin-samples/hello-world.git
git pull origin master
git checkout bug_fix_1

To add all files, commit and push the code

git add .
git commit -m "<msg>"
git push (or) git push <remote-name> <branch-name>
Eg:
git add .
git commit -m "First bug is fixed"
git push origin bug_fix_1

To pull the code

git pull
git pull <remote-name> <branch-name>
Eg:
git pull origin bug_fix_2

Note[Important]:

1. If you have already pushed your changes to the remote before pulling the latest code, once you pull the code it will merge automatically with your changes unless there are conflicts.

2. If you have changes and not pushed, you need to stash the changes before pulling the code and you can stash pop once you successfully pulled the code.

To stash the code

git stash

To stash pop the code

git stash pop

To change commit message(before pushing)

git commit --amend

Note: Then, save the commit file.

To add unstaged/changed files to the last commit(before pushing)

git add .
git commit — amend

To check the latest changes(before commit)

git diff

To check the latest changes(after a commit)

git show

To check the difference between two commits

git diff <previous-commit-sha> <latest-commit-sha>Eg:
git diff abc123 def456

To merge branch

git checkout <old-branch-name>
git merge <new-branch-name>
Eg:
git checkout master
git merge bug_fix_1

Note: If there are any conflicts, you need to resolve and continue the merge.

To cherry-pick

git cherry-pick <commit-sha>Eg:
git cherry-pick def456
git cherry-pick abc123 def456

To create patch

git format-patch -nEg:
git format-patch -1

Note: n denotes the number of commits to take as patches

To apply patch

git diff > <patch-name>.patchEg:
git diff > 0001.first_bug_is_fixed.patch

To create a patch from unstaged files

git diff > <patch-name>.patchEg:
git diff > changes_of_bug_fix_2.patch

To apply patch from unstaged files

git apply <patch-name>.patchEg:
git apply changes_of_bug_fix_2.patch

Useful articles related to git

Final tip

Using Git commands is all about practice. The Only thing you need to remember is you can do whatever you want in the git before pushing your code to the remote. Once you pushed, it can’t be undone. There are some methods to resolve it, but always be aware while pushing the code.

--

--