Git Cheatsheet

๐ง Git Cheatsheet โ The Ultimate Guide
Whether you're just getting started with Git or you're a seasoned developer, having a handy cheatsheet saves time and avoids Googling the same commands over and over again. This guide is organized into Basic, Intermediate, and Advanced sections to cover all levels of Git usage.
๐ข Basic Git Commands
These commands help you set up a repository and perform everyday version control tasks.
๐ง Setup
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
๐ Create or Clone Repository
git init # Initialize a new Git repo
git clone https://github.com/user/repo.git # Clone an existing repo
โ Stage & Commit
git status # See changes
git add file.txt # Stage a file
git add . # Stage all changes
git commit -m "message" # Commit staged changes
๐ Branching Basics
git branch # List branches
git switch main # Switch to branch 'main'
git switch -c new-feature # Create and switch to 'new-feature'
git merge main # Merge 'main' into current branch
๐ก Intermediate Git Commands
Useful when working in teams and collaborating via remote repositories.
๐ Remote Repos
git remote -v # Show remotes
git remote add origin URL # Add remote repo
git push -u origin main # Push first time with upstream
git push # Push commits
git pull # Fetch and merge from origin
git fetch # Fetch changes without merging
๐ Working with Files
git rm file.txt # Remove a tracked file
git mv old.txt new.txt # Rename or move file
git restore file.txt # Discard local changes
git restore --staged file.txt # Unstage a file
๐ต๏ธ View History
git log # Full commit history
git log --oneline --graph # Compact and visual log
git diff # View unstaged changes
git diff --staged # View staged changes
git blame file.txt # Show who last modified each line
๐ด Advanced Git Commands
Commands for rewriting history, debugging, and working with complex workflows.
๐ ๏ธ Rewriting History
git commit --amend # Edit the last commit
git rebase -i HEAD~3 # Interactive rebase last 3 commits
git reflog # Show all history (even deleted commits)
๐งน Cleanups & Resets
git stash # Temporarily save changes
git stash pop # Reapply saved changes
git reset HEAD~1 # Undo last commit (keep changes)
git reset --hard HEAD~1 # Delete last commit and changes
git clean -fd # Remove untracked files and folders
๐จ Undo Merge or Rebase
git merge --abort # Abort a merge
git rebase --abort # Abort a rebase
๐ Cherry Pick & Bisect
git cherry-pick <commit> # Apply a specific commit
git bisect start # Start binary search for bugs
git bisect bad # Mark current commit as bad
git bisect good <commit> # Mark known good commit
git bisect reset # End bisect session
โก Common Git Bash Aliases for Speed
Typing full Git commands repeatedly can be tedious. These aliases make common tasks faster and easier. Add them to your ~/.bashrc
or ~/.zshrc
.
๐ ๏ธ Bash Aliases
alias gs="git status"
alias ga="git add"
alias gaa="git add ."
alias gc="git commit -m"
alias gcm="git commit -m"
alias gsw="git switch"
alias gswc="git switch -c"
alias gb="git branch"
alias gm="git merge"
alias gp="git push"
alias gpl="git pull"
alias gl="git log --oneline --graph --decorate"
alias gd="git diff"
alias gds="git diff --staged"
alias gr="git restore"
alias grs="git restore --staged"
alias gstash="git stash"
alias gpop="git stash pop"
alias grh="git reset --hard"
alias gundo="git reset --soft HEAD~1"
โจ Example Usage
gs # git status
gaa # git add .
gcm "msg" # git commit -m "msg"
gswc feat-x # git switch -c feat-x
๐ Reload the Shell
source ~/.bashrc # or ~/.zshrc
๐งฉ Optional: Git Aliases in Git Config
Prefer aliases within Git itself? Add these:
git config --global alias.st status
git config --global alias.ci commit
git config --global alias.sw switch
git config --global alias.br branch
git config --global alias.lg "log --oneline --graph --decorate"
Use them like:
git st
git sw main
git lg
๐ Final Thoughts
This cheatsheet covers the most essential Git commands for day-to-day development and collaboration. As your workflow becomes more advanced, these quick references will help you stay productive and efficient.
Happy coding!
Subscribe to my newsletter
Read articles from Arpit Rathore directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Arpit Rathore
Arpit Rathore
Senior Backend Engineer