Git Cheatsheet
Practical Git commands for real-world workflows: committing, branching, syncing, and recovering from mistakes.
Everyday Workflow
git status -sbShort status with branch info.
git diffShow unstaged changes.
git diff --stagedShow staged changes.
git add <file>Stage a specific file.
git add -AStage all changes, including deletes.
git add -pInteractively stage chunks (great for clean commits).
git commit -m "message"Create a commit with a message.
git commit -vReview diff while writing the commit message.
git pushPush your branch to its upstream remote.
git log --oneline --graph --decorate --allCompact, visual commit history with branches.
Branches and Remotes
git branchList local branches;
* marks current.git branch -vvShow branch tracking and last commit.
git branch -d <branch>Delete a merged branch safely.
git branch -D <branch>Force delete a branch (use with care).
git switch -c <branch>Create and switch to a new branch.
git switch <branch>Switch to an existing branch.
git merge <branch>Merge another branch into the current branch.
git fetch --pruneUpdate remotes and remove deleted remote branches.
git pull --rebaseFetch + rebase your work on top of remote (avoids merge bubbles).
git push -u origin <branch>Create branch on remote and set upstream.
git remote -vList remote names and URLs.
git remote add origin <url>Add a new remote.
Undo and Fix-Ups
Warning: Commands that rewrite history (like reset --hard or push --force) are risky on shared branches. Prefer revert on main.
git restore <file>Throw away unstaged changes in a file (reset to HEAD).
git restore --staged <file>Unstage a file (keep changes in working tree).
git restore --source <commit> <file>Restore a file from a specific commit.
git revert <commit>Create a new commit that undoes a specific commit.
git reset --soft HEAD~1Uncommit last commit but keep all changes staged.
git reset --mixed HEAD~1Uncommit and unstage last commit, keep changes in working tree.
git reset --hard HEAD~1Drop last commit and all related changes (destructive).
git reflogFind lost commits and previous HEAD positions.
Stashing and Cleaning
git stash push -m "msg"Stash current changes with a label.
git stash push -u -m "msg"Stash tracked + untracked files.
git stash listShow stashes with their indices.
git stash show -p stash@{0}Preview what a stash contains.
git stash apply [stash@{n}]Apply a stash, keep it in the list.
git stash popApply the latest stash and drop it.
git stash drop stash@{n}Remove a stash entry.
git clean -nPreview which untracked files would be removed.
git clean -fdRemove untracked files and directories.
git clean -fdxRemove ignored files too (very destructive).
Rebasing and History Editing
git rebase mainReplay current branch on top of
main.git rebase -i HEAD~5Interactively edit, squash, or drop the last 5 commits.
git commit --fixup <commit>Create a fixup commit to autosquash later.
git rebase -i --autosquash HEAD~5Automatically reorder fixup commits.
git rebase --continueContinue rebase after resolving conflicts.
git rebase --abortAbort the rebase and return to previous state.
git cherry-pick <commit>Apply a single commit onto the current branch.
git push --force-with-leaseForce-push safely after rewriting history (checks remote head).
Inspection and Debugging
git log --statView commit history with file changes.
git show <commit>Inspect a specific commit.
git diff <branch>..HEADCompare current branch to another.
git blame <file>Find who last touched each line.
git bisect startBegin binary search for a bad commit.
git bisect bad <commit>Mark a known bad commit for the bisect.
git bisect good <commit>Mark a known good commit for the bisect.
git bisect resetEnd the bisect session and return to your branch.
Tags and Releases
git tagList all tags.
git tag -a v1.2.0 -m "release"Create an annotated tag.
git push origin --tagsPush tags to the remote.
git show v1.2.0Inspect a tag and its commit.
git checkout v1.2.0Checkout a tag in detached HEAD state.
Config and Helpers
git config --global user.name "Your Name"Set the name used for commits.
git config --global user.email "name@example.com"Set the email used for commits.
git config --global pull.rebase trueUse rebase instead of merge on pull.
git config --global init.defaultBranch mainSet the default branch name for new repos.
git config --global alias.lg "log --oneline --graph --decorate --all"Create a handy log alias.
Worktrees
git worktree add ../repo-fix <branch>Check out another branch in a new folder.
git worktree listList active worktrees and their branches.
git worktree remove <path>Remove a worktree when finished.