Version ControlAll Developers
This Git cheatsheet covers both daily development commands and advanced recovery and collaboration patterns used in real projects.
Set username
git config --global user.name "Your Name"Set email
git config --global user.email "you@example.com"View all config
git config --listEnable rebase on pull
git config --global pull.rebase trueSet default branch name
git config --global init.defaultBranch mainStore credentials (macOS keychain example)
git config --global credential.helper osxkeychainCheck status
git status -sbStage specific hunks interactively
git add -pCommit
git commit -m "<message>"Amend last commit
git commit --amend --no-editPush current branch and set upstream
git push -u origin <branch>Create and switch branch
git switch -c <branch>Switch branch
git switch <branch>List local and remote branches
git branch -aDelete local branch
git branch -d <branch>Delete remote branch
git push origin --delete <branch>Fetch all remotes and prune
git fetch --all --pruneRebase current branch on main
git rebase origin/mainOne-line graph history
git log --oneline --graph --decorate --allShow file history
git log -- <path/to/file>Show changes in last commit
git show --stat HEADCompare working tree vs staged
git diff --stagedCompare current branch with main
git diff origin/main...HEADWho changed each line
git blame <path/to/file>Stash tracked changes
git stash push -m "wip: <note>"Stash including untracked files
git stash push -u -m "wip: <note>"List stashes
git stash listApply latest stash
git stash applyApply and drop latest stash
git stash popDrop specific stash
git stash drop stash@{0}Interactive rebase last 5 commits
git rebase -i HEAD~5Continue rebase after conflict fix
git rebase --continueAbort current rebase
git rebase --abortCherry-pick a commit
git cherry-pick <commit-hash>Squash-merge branch into current
git merge --squash <branch>Find and remove merged local branches
git branch --merged | grep -v "\*\|main\|master\|develop" | xargs -n 1 git branch -dRevert commit safely (shared branches)
git revert <commit-hash>Soft reset last commit
git reset --soft HEAD~1Hard reset to remote branch
git reset --hard origin/<branch>Recover lost commit references
git reflogRestore to reflog checkpoint
git reset --hard <reflog-ref>Restore deleted file from HEAD
git checkout HEAD -- <path/to/file>Fetch PR branch by ref (GitHub)
git fetch origin pull/<id>/head:pr-<id>List files changed against main
git diff --name-only origin/main...HEADShow commit range not in main
git log --oneline origin/main..HEADFixup commit for autosquash
git commit --fixup <commit-hash>Autosquash during interactive rebase
git rebase -i --autosquash origin/main