All resources
Git · Cheat sheet

Git cheat sheet

The complete Git reference, beginner to advanced: config, staging and committing, branching, history, remotes, merge and rebase, undoing changes, stash, tags, and power tools like bisect, reflog, cherry-pick and worktrees — with example output.

Setup & configBeginner
git config --global user.name "Jane Doe"
Set the name on your commits.
git config --global user.email [email protected]
Set the email on your commits.
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.
Start & stageBeginner
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.
## 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.
CommitBeginner
git commit -m "message"
Commit staged changes with a message.
git commit -am "message"
Stage all tracked changes and commit.
git commit --amend
Rewrite the last commit (message or contents).
git commit --amend --no-edit
Add staged changes to the last commit, keep message.
BranchingBeginner
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 & inspectionIntermediate
git log --oneline --graph --all
Compact, visual branch/commit graph.
* 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 & syncIntermediate
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-lease
Force-push safely (aborts if remote moved).
Merge & rebaseIntermediate
git merge feature
Merge a branch into the current one.
git merge --no-ff feature
Always create a merge commit (keep topology).
git rebase main
Replay current branch’s commits onto main.
git rebase -i HEAD~4
Interactively squash/reorder/edit last 4 commits.
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 & recoverIntermediate
git restore <file>
Discard unstaged changes in a file.
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~1
Discard the last commit and its changes (careful).
git revert <commit>
Create a new commit that undoes another (safe on shared).
git reflog
Log of where HEAD has been — recover "lost" commits.
a1b2c3d HEAD@{0}: reset: moving to HEAD~1
9f8e7d6 HEAD@{1}: commit: Add login
Stash, tags & cleanIntermediate
git stash
Shelve uncommitted changes and revert to clean.
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 toolsAdvanced
git bisect start / bad / good <c>
Binary-search commits to find a regression.
Bisecting: 6 revisions left to test
[a1b2c3] Add caching layer
git worktree add ../hotfix main
Check out another branch in a second directory.
git rebase --onto main old feature
Move a branch onto a new base precisely.
git filter-repo --path secrets --invert-paths
Purge a path from all history (needs the tool).
git submodule update --init --recursive
Fetch nested submodule repos.
git commit --fixup <c> && git rebase -i --autosquash
Auto-squash a fix into an earlier commit.
Go deeper
Full, hands-on DevSecOps courses
Browse courses