Data Scientist | Statistician | Quantitative Researcher | Content Creator
Subscribe my Youtube Channel: http://facebook.com/anwar.datascientist
Follow me on Facebook: http://facebook.com/anwar.datascientist
Follow me on Twitter: http://twitter.com/anwar_stat
Follow me on Instragram: http://facebook.com/anwarstatjnu
Follow me on LinkedIn: http://facebook.com/anwarstat
Follow me on Github: http://facebook.com/anwarstat
Tired of memorizing git commands? Here is a cheat sheet with 40+ commands to simplify your life.
git init <directory>
The is optional. If you don't specify it, the current directory will be used.
git clone <url>
git add <file>
To add all files in the current directory, use . in place of .
git add .
git commit -m "<message>"
If you want to add all changes made to tracked files & commit
git commit -a -m "<message>"
git commit -am "<message>"
git reset <file>
git mv <current path> <new path>
git rm <file>
You can also remove it from staging area only
using --cached flag
git rm --cached <file>
Basic Git Concepts
Default branch name: main
Default remote name: origin
Current branch reference: HEAD
Parent of HEAD: HEAD^ or HEAD1
Grandparent of HEAD: HEAD^^ or HEAD2
git branch
Useful flags:
-a: Display all branches (local & remote)
-r: Display remote branches
-v: Display branches with last commit
git branch <branch>
You can create a branch and switch to it using the checkout command.
git checkout -b <branch>
git checkout <branch>
git branch -d <branch>
You can also force delete a branch using the -D flag.
git branch -D <branch>
git merge <branch to merge into HEAD>
Useful flags:
--no-ff: Create a merge commit even if the merge resolves as a fast-forward
--squash: Squash all commits from the specified branch into a single commit
Fast forward Merge
Fast-forward-merge
Non-Fast forward Merge
No-fast-forward-merge
It is suggested to not use the --squash flag as it will squash all commits into a single commit, leading to a messy commit history.
Rebasing is the process of moving or combining a sequence of commits to a new base commit
git rebase <branch to rebase from>
git checkout <commit id>
git revert <commit id>
git reset <commit id>
You can also add the --hard flag to delete all changes, but use it with caution.
git reset --hard <commit id>
git status
git log
git diff
You can also use the --staged flag to display the changes to staged files.
git diff --staged
git diff <commit id 01> <commit id 02>
The stash allows you to temporarily store changes without committing them.
git stash
You can also add a message to the stash.
git stash save "<message>"
git stash list
Applying the stash will NOT remove it from the stash list.
git stash apply <stash id>
If you do not specify the , the latest stash will be applied (Valid for all similar stash commands) You can also use the format stash@{} to apply a stash (Valid for all similar stash commands)
git stash apply stash@{0}
git stash drop <stash id>
git stash clear
git stash pop <stash id>
git stash show <stash id>
git remote add <remote name> <url>
git remote
Add a -v flag to display the URLs of the remote repositories.
git remote -v
git remote remove <remote name>
git remote rename <old name> <new name>
git fetch <remote name>
git fetch <remote name> <branch>
git pull <remote name> <branch>
git push <remote name>
git push <remote name> <branch>