Git Cheat Sheet
Essential Git commands for daily development — init, commit, branch, merge, rebase, stash, log, and remote operations in one quick reference.
9 sections 31 snippets
Setup and Config
Configure identity
git config --global user.name "Your Name"
git config --global user.email "you@example.com" Initialize a repo
git init
git init my-project Clone a repo
git clone https://github.com/user/repo.git
git clone git@github.com:user/repo.git Check config
git config --list
git config user.email Basic Workflow
Check status
git status
git status -s # short format Stage changes
git add file.txt # single file
git add src/ # directory
git add -A # everything
git add -p # interactive hunks Commit
git commit -m "feat: add login page"
git commit -am "fix: typo" # stage + commit tracked
git commit --amend # edit last commit View diff
git diff # unstaged changes
git diff --staged # staged changes
git diff main..feature # between branches Branching
List branches
git branch # local
git branch -r # remote
git branch -a # all Create and switch
git branch feature
git switch feature
git switch -c feature # create + switch Delete
git branch -d feature # safe delete
git branch -D feature # force delete
git push origin --delete feature # remote Rename
git branch -m old-name new-name Merging and Rebasing
Merge
git switch main
git merge feature
git merge --no-ff feature # always create merge commit Rebase
git switch feature
git rebase main
git rebase -i HEAD~3 # interactive rebase last 3 Resolve conflicts
# Edit conflicting files, then:
git add resolved-file.txt
git rebase --continue
# or abort:
git merge --abort
git rebase --abort Cherry-pick
git cherry-pick abc1234
git cherry-pick abc1234..def5678 Remote Operations
Manage remotes
git remote -v
git remote add origin https://github.com/user/repo.git
git remote set-url origin <new-url> Push
git push origin main
git push -u origin feature # set upstream
git push --force-with-lease # safe force push Pull and fetch
git pull # fetch + merge
git pull --rebase # fetch + rebase
git fetch origin # fetch only Stash
Save and restore
git stash # stash changes
git stash pop # apply + remove
git stash apply # apply, keep stash Manage stashes
git stash list
git stash show -p stash@{0}
git stash drop stash@{0}
git stash clear History and Inspection
View log
git log --oneline
git log --oneline --graph --all
git log -5 # last 5 commits
git log --author="Alice" Blame
git blame file.txt
git blame -L 10,20 file.txt Show a commit
git show abc1234
git show HEAD~2:file.txt # file at that commit Undo and Reset
Unstage
git restore --staged file.txt Discard changes
git restore file.txt # discard working tree
git checkout -- file.txt # older syntax Reset commits
git reset --soft HEAD~1 # undo commit, keep staged
git reset --mixed HEAD~1 # undo commit + unstage
git reset --hard HEAD~1 # undo everything reset --hard is destructive and cannot be undone
Revert (safe undo)
git revert abc1234 # create inverse commit