Version Control

Git Cheatsheet

Quick reference for essential Git commands including init, clone, branch, merge, rebase, stash, log, reset, and cherry-pick.

Setup & Init

CommandDescription
git initInitialize a new repository
git clone <url>Clone a remote repository
git config --global user.name "Name"Set global username
git config --global user.email "email"Set global email

Staging & Committing

CommandDescription
git statusShow working tree status
git add <file>Stage a file
git add -AStage all changes
git add -pInteractively stage hunks
git commit -m "msg"Commit with message
git commit --amendAmend last commit
git diffShow unstaged changes
git diff --stagedShow staged changes

Branching

CommandDescription
git branchList local branches
git branch -aList all branches (including remote)
git branch <name>Create a branch
git branch -d <name>Delete a branch (safe)
git branch -D <name>Force delete a branch
git checkout <branch>Switch to branch
git checkout -b <branch>Create and switch to branch
git switch <branch>Switch to branch (modern)
git switch -c <branch>Create and switch (modern)

Merging & Rebasing

CommandDescription
git merge <branch>Merge branch into current
git merge --no-ff <branch>Merge with merge commit
git merge --squash <branch>Squash merge (no commit)
git rebase <branch>Rebase current onto branch
git rebase -i HEAD~<n>Interactive rebase last n commits
git rebase --abortAbort in-progress rebase
git rebase --continueContinue after resolving conflicts

Stashing

CommandDescription
git stashStash working changes
git stash push -m "msg"Stash with message
git stash listList all stashes
git stash popApply and remove latest stash
git stash applyApply latest stash (keep in list)
git stash dropRemove latest stash
git stash clearRemove all stashes
git stash show -pShow stash diff

Log & History

CommandDescription
git logShow commit history
git log --onelineCompact log
git log --graph --oneline --allVisual branch graph
git log -p <file>Show changes to a file over time
git log --author="name"Filter by author
git log --since="2 weeks ago"Filter by date
git blame <file>Show who changed each line
git shortlog -snCommit count by author

Undoing & Resetting

CommandDescription
git reset <file>Unstage a file
git reset --soft HEAD~1Undo commit, keep changes staged
git reset --mixed HEAD~1Undo commit, keep changes unstaged
git reset --hard HEAD~1Undo commit, discard changes
git revert <commit>Create a new commit undoing a commit
git checkout -- <file>Discard changes to a file
git restore <file>Discard changes (modern)
git restore --staged <file>Unstage a file (modern)
git clean -fdRemove untracked files and dirs

Cherry-Pick

CommandDescription
git cherry-pick <commit>Apply a specific commit
git cherry-pick <a>..<b>Apply a range of commits
git cherry-pick --no-commit <commit>Apply without committing
git cherry-pick --abortAbort cherry-pick

Remote

CommandDescription
git remote -vList remotes
git remote add origin <url>Add remote
git fetchFetch all remotes
git pullFetch and merge
git pull --rebaseFetch and rebase
git pushPush to remote
git push -u origin <branch>Push and set upstream
git push --force-with-leaseSafe force push

Tags

CommandDescription
git tagList tags
git tag v1.0.0Create lightweight tag
git tag -a v1.0.0 -m "msg"Create annotated tag
git push --tagsPush all tags to remote
git tag -d v1.0.0Delete a local tag