Git · Cheat sheet

Git cheat sheet

Git cheat sheet: staging, branching, remotes, rebase, undo, stash, tags, and tools like bisect and worktrees.

64 commands·10 sections·Updated ·By SecOpsLog

Rows marked Destructive State operation Caution Exposes secrets can remove data, change authoritative state, affect a running system, or print secrets. Read the note before running them against anything that matters.

Setup & config

Beginner
git config --global user.name "Jane Doe"
Set the name on your commits (Git 2.46+ form: git config set).
git config --global user.email jane@x.io
Set the email on your commits.
git config --global gpg.format ssh
Sign commits with SSH keys; set user.signingKey to the key path.
git config --global init.defaultBranch main
Default new repos to a main branch.
git config --global alias.st status
Create a shortcut (git st → git status).
git config list
Show the effective configuration (Git < 2.46: --list).

Start & stage

Beginner
git init
Turn the current directory into a repo.
git clone <url>
Copy a remote repo locally.
git clone --depth 1 <url>
Shallow clone — latest commit only (fast/CI).
git status -sb
Short branch + change summary.Example output
## main...origin/main [ahead 1]
 M src/app.js
?? notes.md
git add <file>
Stage a file for the next commit.
git add -p
Interactively stage individual hunks.
git restore --staged <file>
Unstage a file (keep the edits).
git diff
Unstaged changes vs the index.
git diff --staged
Staged changes vs the last commit.

Commit

Beginner
git commit -m "message"
Commit staged changes with a message.
git commit -am "message"
Stage all tracked changes and commit.
git commit --amendCaution
Rewrite the last commit (message or contents).Caution: Rewrites the last commit; if already pushed, others need a force push to follow.
git commit --amend --no-editCaution
Add staged changes to the last commit, keep message.Caution: Rewrites the last commit; if already pushed, others need a force push to follow.
git commit -S -m "message"
Create a signed commit (commit.gpgSign=true signs every commit).

Branching

Beginner
git branch
List local branches (* = current).
git switch -c feature
Create and switch to a branch (modern).
git switch main
Switch to an existing branch.
git branch -d feature
Delete a merged branch (-D forces).
git branch -m old new
Rename a branch.
git merge --abort
Bail out of an in-progress merge.

History & inspection

Intermediate
git log --oneline --graph --all
Compact, visual branch/commit graph.Example output
* a1b2c3d (HEAD -> main) Fix auth
* 9f8e7d6 Add login
| * 4d5e6f7 (feature) WIP
git log -p <file>
History of a file with diffs.
git log --author=jane --since="2 weeks"
Filter history by author/date.
git show <commit>
Full details and diff of one commit.
git blame <file>
Who last changed each line.
git diff main...feature
What feature adds over main.

Remotes & sync

Intermediate
git remote -v
List remotes and their URLs.
git remote add origin <url>
Attach a remote named origin.
git fetch
Download remote changes without merging.
git pull
Fetch + integrate the remote branch.
git pull --rebase
Fetch, then replay your commits on top (linear).
git push
Upload commits to the tracked remote branch.
git push -u origin feature
Push and set upstream tracking.
git push --force-with-leaseDestructive
Force-push, but abort if the remote moved since your last fetch.Destructive: Still overwrites remote history; add --force-if-includes if anything auto-fetches.

Merge & rebase

Intermediate
git merge feature
Merge a branch into the current one.
git merge --no-ff feature
Always create a merge commit (keep topology).
git rebase mainCaution
Replay current branch’s commits onto main.Caution: Rewrites commit IDs; avoid on branches others have already pulled.
git rebase -i HEAD~4Caution
Interactively squash/reorder/edit last 4 commits.Caution: Rewrites commit IDs; avoid on branches others have already pulled.Example output
pick a1b2c3 Add login
squash 9f8e7d Fix typo
reword 4d5e6f Update docs
git rebase --continue
Proceed after resolving conflicts.
git cherry-pick <commit>
Apply one commit from elsewhere onto HEAD.

Undo & recover

Intermediate
git restore <file>Destructive
Discard unstaged changes in a file.Destructive: Unstaged edits are discarded; they were never committed, so reflog cannot recover them.
git reset --soft HEAD~1
Undo last commit, keep changes staged.
git reset --mixed HEAD~1
Undo last commit, keep changes unstaged (default).
git reset --hard HEAD~1Destructive
Discard the last commit and its changes (careful).Destructive: Wipes uncommitted changes too; the dropped commit survives only in the reflog.
git revert <commit>
Create a new commit that undoes another (safe on shared).
git reflog
Log of where HEAD has been — recover "lost" commits.Example output
a1b2c3d HEAD@{0}: reset: moving to HEAD~1
9f8e7d6 HEAD@{1}: commit: Add login

Stash, tags & clean

Intermediate
git stash
Shelve tracked changes and reset to HEAD (-u also stashes untracked).
git stash pop
Reapply the latest stash and drop it.
git stash list
Show shelved change sets.
git tag -a v1.2.0 -m "release"
Create an annotated tag.
git push origin v1.2.0
Publish a tag to the remote.
git clean -fdn
Preview which untracked files would be removed.

Power tools

Advanced
git bisect start / bad / good <c>
Binary-search commits to find a regression.Example output
Bisecting: 675 revisions left to test after this (roughly 10 steps)
[a1b2c3] Add caching layer
git worktree add -b hotfix ../hotfix main
New branch hotfix from main in a second directory.
git rebase --onto main old featureCaution
Move a branch onto a new base precisely.Caution: Rewrites commit IDs; avoid on branches others have already pulled.
git filter-repo --path secrets --invert-pathsDestructive
Purge a path from all history (needs the tool).Destructive: Rewrites every commit ID; collaborators must re-clone. Rotate any leaked secret too.
git submodule update --init --recursive
Fetch nested submodule repos.
git commit --fixup <c> && git rebase -i --autosquashCaution
Auto-squash a fix into an earlier commit.Caution: The autosquash rebase rewrites commit IDs; avoid on already-shared branches.

Primary references

Found a technical issue on this page? Report it with the tool version you used and the behavior you saw. How resources are maintained.