CheatSheetHub / Git Commands

Git Commands Cheat Sheet

The commands you already understand conceptually but still look up every single time. Grouped by what you're trying to do, not alphabetically.

Basics Branching Undoing things Stashing Remote

Basics

git initCreate a new repository in the current directory.
git statusShow changed, staged, and untracked files.
git add .Stage all changed and new files in the current directory.
git commit -m "message"Commit staged changes with a message.
git commit --amendEdit the most recent commit's message or add staged changes to it.
git log --oneline --graphShow commit history as a compact, visual graph.
git diffShow unstaged changes compared to the last commit.
git diff --stagedShow staged changes compared to the last commit.
git show <commit>Show the full details and diff of a specific commit.
git log -p -- file.txtShow full history and diffs for a single file.
git log --author="name"Show commits made by a specific author.
git blame file.txtShow who last modified each line of a file.
git config --global user.name "Name"Set your Git username globally.
git config --global user.email "you@x.com"Set your Git email globally.
git config --global alias.co checkoutCreate a shorthand alias for a Git command.
git add -pInteractively stage specific chunks (hunks) within a file, not the whole file.
git mv old.txt new.txtRename a file and stage the rename in one step.
git rm file.txtRemove a file from the working directory and stage the deletion.
.gitignoreA file listing patterns Git should never track (e.g. node_modules/, *.log).

Branching & merging

git branchList local branches.
git checkout -b feature-nameCreate a new branch and switch to it.
git switch feature-nameSwitch to an existing branch (newer alternative to checkout).
git merge feature-nameMerge the named branch into the current branch.
git rebase mainReplay the current branch's commits on top of main.
git rebase -i HEAD~3Interactively edit, squash, or reorder the last 3 commits.
git branch -d feature-nameDelete a branch that's already been merged.
git branch -D feature-nameForce-delete a branch, merged or not.
git branch -m old-name new-nameRename a branch.
git branch -rList remote-tracking branches.
git branch -aList all branches, local and remote.
git merge --no-ff feature-nameForce a merge commit even if a fast-forward was possible, preserving branch history.
git merge --abortCancel a merge in progress and return to the pre-merge state.
git rebase --continueContinue a rebase after resolving a conflict.
git rebase --abortCancel an in-progress rebase and return to the original state.
git cherry-pick <commit>Apply a specific commit from another branch onto the current branch.
git tag v1.0.0Create a lightweight tag on the current commit.
git tag -a v1.0.0 -m "msg"Create an annotated tag with a message.
[ ad space — 336×280 ]

Undoing things

git reset --soft HEAD~1Undo the last commit, keep changes staged.
git reset --hard HEAD~1Undo the last commit and discard the changes entirely.
git checkout -- file.txtDiscard unstaged changes to a specific file.
git restore file.txtModern equivalent of checkout for discarding file changes.
git revert <commit>Create a new commit that undoes a specific past commit, safely.
git reflogShow a log of where HEAD has pointed, including "lost" commits.
git clean -fdRemove untracked files and directories.
git reset HEAD file.txtUnstage a file without discarding its changes.
git commit --amend --no-editAdd staged changes to the last commit without changing its message.
git bisect startBegin a binary search through commit history to find which commit introduced a bug.
git rm --cached file.txtStop tracking a file without deleting it locally.
git checkout <commit> -- file.txtRestore a file to its state at a specific past commit.

Stashing

git stashTemporarily shelve uncommitted changes.
git stash popReapply the most recent stash and remove it from the stash list.
git stash listShow all stashed changesets.
git stash apply stash@{1}Reapply a specific stash without removing it.
git stash drop stash@{1}Delete a specific stash without applying it.
git stash push -m "message"Stash changes with a custom description.
git stash -uStash changes including untracked files.
git stash show -p stash@{0}Show the full diff contained in a stash.

Working with remotes

git clone <url>Copy a remote repository to your local machine.
git fetchDownload remote changes without merging them.
git pullFetch and merge remote changes into the current branch.
git push -u origin feature-namePush a new branch and set it to track the remote branch.
git push --force-with-leaseForce-push safely — fails if someone else pushed in the meantime.
git remote -vList configured remotes and their URLs.
git remote add origin <url>Add a new remote named "origin".
git remote set-url origin <url>Change the URL of an existing remote.
git fetch --all --pruneFetch from all remotes and remove references to deleted remote branches.
git push origin --delete branch-nameDelete a branch on the remote.
git pull --rebaseFetch and rebase local commits on top, instead of creating a merge commit.
git submodule update --init --recursiveInitialize and fetch all nested submodules.

Common questions

How do I undo the last Git commit without losing my changes?

Run git reset --soft HEAD~1. This removes the commit but keeps your changes staged, so you can re-commit them with a different message or split them up.

What's the difference between git merge and git rebase?

git merge creates a new commit that combines two branch histories, keeping both intact. git rebase rewrites your branch's commits on top of another branch, producing a linear history but changing commit hashes.

How do I recover a deleted branch in Git?

Run git reflog to find the commit hash the branch pointed to, then create a new branch from it with git checkout -b branch-name <hash>. Git keeps this history for about 30 days by default.

Copied