Git Cheat Sheet
Setup, staging, branching, merging, remotes, stash, rebase, and history surgery — the version control core of every pipeline.
Setup & Config
9git initInitialize a repository
git config --global user.name "NAME"Set the commit author name
git config --global user.email "USER@EMAIL"Set the commit author email
git config --global core.editor codeSet the default editor
git config --listShow effective configuration
git config --global init.defaultBranch mainDefault branch to main
git clone URL DIRClone a repository
git clone --depth 1 URLShallow clone (faster)
git config alias.co checkoutCreate a shortcut alias
Staging & Committing
16git statusShow working tree state
git diffUnstaged changes
git diff --stagedStaged changes
git add FILEStage a file
git add -pStage hunks interactively
git add -AStage all changes (incl. deletions)
git rm FILERemove and stage the deletion
git mv OLD NEWRename and stage
git commit -m "message"Commit staged changes
git commit -am "message"Stage tracked changes and commit
git commit --amend -m "new msg"Redo the last commit message
git commit --amend --no-editFold staged changes into the last commit
git restore FILEDiscard unstaged changes to a file
git restore --staged FILEUnstage a file (keep changes)
git reset HEAD~1Undo the last commit, keep changes
git reset --hard HEAD~1Undo the last commit, discard changes
Branching & Merging
12git branchList local branches
git branch NAMECreate a branch
git checkout -b NAMECreate and switch to a branch
git switch -c NAMEModern create-and-switch
git switch -Switch to the previous branch
git merge NAMEMerge a branch into the current one
git merge --no-ff NAMEMerge with a merge commit
git branch -d NAMEDelete a merged branch
git branch -D NAMEForce-delete an unmerged branch
git branch --mergedBranches already merged
git branch --track NAME ORIGIN/NAMECreate a branch tracking upstream
git remote prune originRemove stale remote branches
Remotes & Syncing
9git remote -vList configured remotes
git remote add origin URLAdd an origin remote
git push origin mainPush the main branch
git push -u origin NAMEPush and set upstream
git push --force-with-leaseForce-push safely after rebase
git fetchDownload remote refs without merging
git pull --rebasePull and rebase local commits
git push origin --delete NAMEDelete a remote branch
git fetch origin PR_REF:NAMEFetch a PR into a local branch
History & Diffing
10git log --onelineCompact commit list
git log --oneline --graph --allVisual graph of all branches
git log -p FILEFull diff history of a file
git log --since='2 weeks ago' --author=NAMEFilter commits by time and author
git show COMMITShow a commit's diff
git show HEAD:FILEShow a file as of HEAD
git blame FILEWho changed each line
git diff HEAD -- FILEWorking tree vs HEAD for a file
git diff BRANCH1..BRANCH2Compare two branches
git shortlog -snCommit counts per author
Stash
8git stashSave uncommitted work aside
git stash -uAlso stash untracked files
git stash listList stashes
git stash popReapply the newest stash and drop it
git stash apply stash@{0}Reapply a stash without dropping
git stash push -m "WIP"Stash with a message
git stash branch NAMETurn a stash into a branch
git stash drop stash@{1}Drop a specific stash
Undo & Surgery
10git revert COMMITUndo a commit with a new commit
git reset --soft HEAD~1Undo commit, keep changes staged
git reset --mixed HEAD~1Undo commit, keep changes unstaged
git reset --hard ORIG_HEADUndo a bad reset
git rebase -i HEAD~3Interactive rebase of the last 3 commits
git rebase --abortCancel an in-progress rebase
git rebase --continueContinue after resolving conflicts
git cherry-pick COMMITApply a specific commit onto HEAD
git reflogEvery HEAD movement (recovery)
git gc --prune=nowGarbage-collect loose objects
Conflicts
6git statusFiles with conflict markers
git diff --name-only --diff-filter=UList conflicted files
git checkout --ours FILETake the merge-side version
git checkout --theirs FILETake the incoming version
git mergetoolLaunch the configured merge tool
git config merge.conflictStyle diff3Show the base version too
Submodules
4git submodule add URL PATHAdd a nested repository
git submodule update --init --recursiveClone and sync all submodules
git submodule foreach git pullUpdate every submodule
git submodule update --remotePull latest of every submodule