Git Cheat Sheet
Version control for your code. Working folder → git add → staging → git commit → local repo → git push → remote (GitHub).
On this page
First-Time Setup #
| Command | Action |
|---|---|
| Your name on commits | |
| Your email on commits | |
New repos start on main | |
| Use VS Code for commit messages | |
| rebases instead of merging | |
| Show all settings |
Start a Repo #
| Command | Action |
|---|---|
| Turn the current folder into a repo | |
| Copy a remote repo | |
| Clone into a specific folder |
Daily Workflow #
| Command | Action |
|---|---|
| What changed? (use it all the time) | |
| Stage a file | |
| Stage everything in this folder | |
| Stage piece by piece (interactive) | |
| Commit staged changes | |
| Stage all tracked files and commit | |
Get the remote changes (stops if branches diverged; see pull.rebase) | |
| Send your commits to the remote | |
| First push of a new branch (sets upstream) |
Branches #
| Command | Action |
|---|---|
List local branches (-a includes remote ones) | |
| Switch branch | |
| Create and switch | |
Merge name into the current branch | |
| Replay your branch on top of main | |
Delete a merged branch (-D = force) | |
| Delete a remote branch | |
| Rename the current branch |
Inspect #
| Command | Action |
|---|---|
| Compact history with branches | |
| History of one file with its changes | |
| Unstaged changes | |
| Staged changes | |
| Difference between branches | |
| One commit in detail | |
| Who changed each line | |
| Commits per author |
Undo (from safe to dangerous) #
| Command | Action |
|---|---|
| ⚠ Discard unstaged changes in a file (can't be undone) | |
| Unstage (keeps the changes) | |
| Fix the last commit message / add forgotten files | |
| Undo the last commit, keep changes staged | |
| Undo the last commit, keep changes unstaged | |
| New commit that reverses an old one (safe for pushed commits) | |
| ⚠ Delete the last commit and its changes | |
| / | Preview / ⚠ delete untracked files and folders |
| Every place HEAD has been; rescue "lost" commits |
Never reset --hard or push --force on shared branches. If you must, use git push --force-with-lease.
Stash (park changes) #
| Command | Action |
|---|---|
| Park uncommitted changes | |
| Include untracked files | |
| List stashes | |
| Re-apply the latest stash and remove it | |
| Apply a specific stash and keep it | |
| Delete the latest stash |
Remotes #
| Command | Action |
|---|---|
| Show remotes | |
| Link a remote | |
| Change the URL (for example HTTPS → SSH) | |
| Download without merging | |
| Pull and put your commits on top |
Tags & Releases #
| Command | Action |
|---|---|
| Lightweight tag | |
| Annotated tag | |
| Push tags |
Advanced but Useful #
| Command | Action |
|---|---|
| Copy one commit onto the current branch | |
| Squash, reorder or edit the last 3 commits | |
/ good / bad | Binary-search for the commit that caused a bug |
| Check out a second branch in another folder | |
| Stop tracking a file (keeps it on disk) |
.gitignore #
node_modules/
dist/
.env
*.log
.DS_Store
.vscode/*
!.vscode/settings.json
Already committed a file that should be ignored? git rm --cached file, then commit.
Merge Conflicts #
git statusshows the conflicted files.- Edit the files and keep what you need between
<<<<<<<,=======and>>>>>>>. git add file→git commit(orgit rebase --continue).- Panic button:
git merge --abort/git rebase --abort.
git configRead and write Git settings. git config [scope] [options] [key] [value]
| Option | What it does |
|---|---|
--global | Your user settings (~/.gitconfig) |
--local | This repository only (default when writing) |
--system | Every user on the machine |
--list (-l) | Show all settings (--show-origin = and where; Git 2.46+: git config list) |
--get key | Read one value (Git 2.46+: git config get key) |
--unset key | Remove a setting (Git 2.46+: git config unset key) |
--edit (-e) | Open the config file in your editor |
user.name "Name" / user.email | Your identity on commits |
init.defaultBranch main | Branch name for new repos |
core.editor "code --wait" | Editor for commit messages |
core.autocrlf true | Windows: convert line endings (input on macOS/Linux) |
pull.rebase true | git pull rebases instead of merging |
push.autoSetupRemote true | First git push creates the remote branch |
alias.st status | Shortcut: git st |
credential.helper manager | Remember credentials (Windows) |
git pullFetch and integrate remote changes. git pull [options] [remote] [branch]
| Option | What it does |
|---|---|
--rebase (-r) | Put your commits on top instead of making a merge commit |
--ff-only | Only update if no merge is needed (safe) |
--no-rebase | Merge (without it, git pull only fast-forwards and stops if branches diverged) |
--autostash | Stash local changes before, re-apply after |
--all | Fetch all remotes |
--tags | Also fetch tags |
-v / -q | Verbose / quiet |
git initCreate a new repository. git init [options] [folder]
| Option | What it does |
|---|---|
git init | Turn the current folder into a repo |
git init my-app | Create a folder and a repo in it |
-b main (--initial-branch) | Name of the first branch |
--bare | Repo without a working folder (for servers) |
git cloneCopy a remote repository. git clone [options] url [folder]
| Option | What it does |
|---|---|
git clone url folder | Clone into a specific folder |
--depth 1 | Only the latest commit (fast, small) |
-b dev (--branch) | Check out a specific branch or tag |
--single-branch | Only fetch that one branch |
--recurse-submodules | Also clone submodules |
--filter=blob:none | Partial clone: file contents downloaded when needed |
--bare / --mirror | Server copy without a working folder / full mirror |
-o upstream | Name of the remote (default origin) |
git statusWhat changed? git status [options]
| Option | What it does |
|---|---|
-s (--short) | One line per file (M modified, A added, ?? untracked) |
-b | Show the branch (with -s) |
-uno / -uall | Hide untracked files / show every untracked file (no space after -u) |
--ignored | Also show ignored files |
git addStage changes for the next commit. git add [options] [paths]
| Option | What it does |
|---|---|
git add file | Stage one file |
git add . | Everything in this folder (new, changed, deleted) |
-A (--all) | Everything in the whole repo |
-u | Only files Git already tracks (no new files) |
-p (--patch) | Choose piece by piece (y/n/s split/q) |
-i | Interactive menu |
-n (--dry-run) | Show what would be added |
-f | Add an ignored file anyway |
-N | Intent to add: track a new file without staging its content |
git commitSave the staged changes. git commit [options]
| Option | What it does |
|---|---|
-m "message" | Message on the command line (-m twice = title + body) |
-a | Also stage all changed tracked files (-am "msg") |
--amend | Change the last commit (message and/or content) |
--amend --no-edit | Add staged changes to the last commit, keep the message |
-v | Show the diff in the editor while writing the message |
--allow-empty | Commit without changes (trigger CI) |
--no-verify (-n) | Skip the pre-commit and commit-msg hooks |
-S | Sign the commit (GPG / SSH) |
--author="Name <mail>" | Different author |
--date="2026-10-01" | Different date |
--fixup=SHA | Fix-up commit for git rebase -i --autosquash |
git pushSend your commits to the remote. git push [options] [remote] [branch]
| Option | What it does |
|---|---|
-u origin branch | First push of a branch: also sets the upstream |
--force-with-lease | Overwrite the remote safely (fails if someone else pushed) |
--force (-f) | ⚠ Overwrite the remote branch, no checks |
--tags | Push all tags (git push origin v1.0 for one) |
--delete origin branch (-d) | Delete a remote branch |
--all | Push all branches |
--dry-run (-n) | Show what would be pushed |
--no-verify | Skip pre-push hooks |
-o ci.skip | Push option (e.g. skip CI on GitLab) |
git branchList, create, rename and delete branches. git branch [options] [name]
| Option | What it does |
|---|---|
| (none) | Local branches (* = current) |
-a | Local and remote branches |
-r | Remote branches only |
-v / -vv | Last commit / and upstream tracking info |
name | Create a branch (doesn't switch to it) |
-d name | Delete a merged branch |
-D name | ⚠ Force delete, also unmerged |
-m new / -m old new | Rename the current / another branch |
--merged / --no-merged | Branches that are / aren't merged into this one |
-u origin/main | Set the upstream of the current branch |
--sort=-committerdate | Most recently used first |
--show-current | Just the current branch name |
git switchChange branches. git switch [options] branch
| Option | What it does |
|---|---|
git switch name | Go to a branch (also creates it from origin/name if that exists) |
-c name | Create and switch |
-c name origin/name | Create from a remote branch |
- | Back to the previous branch |
--detach SHA | Look at an old commit (detached HEAD) |
-C name | Create, or reset an existing branch |
--discard-changes | Throw away local changes while switching |
Older equivalent: git checkout name / git checkout -b name.
git mergeJoin another branch into the current one. git merge [options] branch
| Option | What it does |
|---|---|
--no-ff | Always create a merge commit |
--ff-only | Only fast-forward, fail otherwise |
--squash | Squash all changes into the next commit (no merge commit) |
--abort | Cancel a merge with conflicts |
--continue | Finish after solving conflicts |
-m "message" | Message for the merge commit |
-X theirs / -X ours | Prefer their / our side in conflicts |
--no-commit | Merge but stop before committing |
git rebaseReplay your commits on top of another branch. git rebase [options] [base]
| Option | What it does |
|---|---|
git rebase main | Put your branch on top of main |
-i HEAD~3 | Interactive: reorder, squash (s), edit (e), reword (r), drop (d) |
--continue | Next step after fixing conflicts |
--abort | Cancel and go back |
--skip | Skip the current commit |
--autosquash | Use fixup! commits automatically (with -i) |
--onto new old | Move a range of commits to a new base |
--autostash | Stash local changes before, re-apply after |
--update-refs | Also move branches that point into the rebased commits |
⚠ Don't rebase commits that others already pulled.
git logCommit history. git log [options] [range] [-- path]
| Option | What it does |
|---|---|
--oneline | One line per commit |
--graph | ASCII graph of branches and merges |
--all | All branches, not just the current one |
-n 10 / -10 | Last 10 commits |
-p | Show the changes (patches) |
--stat | Changed files with line counts |
--author="Milan" | Commits by an author |
--since="2 weeks ago" / --until | Date range |
--grep="fix" | Search commit messages (-i ignore case) |
-S "text" | Commits that added or removed this text ("pickaxe") |
-- path/file | Only commits touching a file (--follow across renames) |
main..feature | Commits in feature that are not in main |
--merges / --no-merges | Only / no merge commits |
--first-parent | Main line only |
--pretty=format:"%h %an %ar %s" | Custom format |
--decorate | Show branch and tag names |
Favourite: git log --oneline --graph --all --decorate
git diffShow changes. git diff [options] [commit…] [-- path]
| Option | What it does |
|---|---|
| (none) | Unstaged changes |
--staged (--cached) | Staged changes (what will be committed) |
HEAD | All local changes, staged or not |
main..feature / main...feature | Between branches / since they split |
SHA1 SHA2 | Between two commits |
--stat | Summary per file |
--name-only / --name-status | Only file names / with A/M/D |
-w | Ignore whitespace |
--word-diff | Show changed words instead of lines |
--color-words | Inline coloured word changes |
-U10 | 10 lines of context |
--check | Warn about trailing whitespace / conflict markers |
git showShow a commit (or a file at a commit). git show [options] [object]
| Usage / option | What it does |
|---|---|
git show | The last commit with its diff |
git show SHA | A specific commit |
--stat | Files changed only |
--name-only | File names only |
git show SHA:path/file | A file as it was in that commit |
git show v1.0 | A tag |
git blameWho last changed each line. git blame [options] file
| Option | What it does |
|---|---|
-L 10,20 | Only lines 10–20 (-L :funcName for a function) |
-w | Ignore whitespace changes |
-M / -C | Detect lines moved or copied within the file / from other files |
--ignore-rev SHA | Ignore a commit (big reformat) |
--ignore-revs-file .git-blame-ignore-revs | Ignore commits listed in a file |
-e | Show email addresses |
SHA -- file | Blame at an older version |
git shortlogSummarise commits per author. git shortlog [options] [range]
| Option | What it does |
|---|---|
-s | Only the number of commits |
-n | Sort by number of commits |
-e | Show emails |
--since="1 year ago" | Time range |
--no-merges | Skip merge commits |
Popular: git shortlog -sn --no-merges
git restoreUndo changes in files. git restore [options] paths
| Option | What it does |
|---|---|
git restore file | ⚠ Discard unstaged changes in a file |
--staged file (-S) | Unstage (keep the changes in the file) |
--staged --worktree file | Unstage and discard |
--source=SHA file | Restore a file from an older commit |
-p | Choose piece by piece |
. | All files in this folder |
Older equivalents: git checkout -- file / git reset file.
git resetMove the branch back and/or unstage. git reset [mode] [commit]
| Mode / option | What it does |
|---|---|
--soft HEAD~1 | Undo the last commit, keep its changes staged |
--mixed HEAD~1 (default) | Undo the commit, keep changes unstaged |
--hard HEAD~1 | ⚠ Undo the commit and delete its changes |
--hard origin/main | ⚠ Make your branch exactly like the remote |
git reset file | Unstage a file |
--keep | Like --hard but refuses if you'd lose local changes |
-p | Unstage piece by piece |
Lost something? git reflog shows where you were.
git revertUndo a commit with a new commit (safe for shared branches). git revert [options] commit…
| Option | What it does |
|---|---|
git revert SHA | New commit that reverses SHA |
--no-edit | Use the default message |
-n (--no-commit) | Apply the reversal but don't commit yet |
-m 1 SHA | Revert a merge commit (keep parent 1) |
HEAD~3..HEAD | Revert a range |
--abort / --continue | Stop / continue after conflicts |
git cleanDelete untracked files. git clean [options]
| Option | What it does |
|---|---|
-n | Dry run: show what would be deleted (always do this first) |
-f | Actually delete (required) |
-d | Also untracked folders |
-x | Also ignored files (e.g. node_modules, build output) |
-X | Only ignored files |
-i | Interactive |
-e "*.env" | Keep matching files |
git reflogEverywhere HEAD has been: the safety net for "lost" commits. git reflog [options]
| Usage / option | What it does |
|---|---|
git reflog | History of checkouts, commits, resets, rebases |
git reflog show branch | Reflog of one branch |
-n 20 | Last 20 entries |
--date=relative | Show times (HEAD@{2 hours ago}) |
git reset --hard HEAD@{3} | Go back to an earlier state |
git branch rescue HEAD@{5} | Recover a lost commit as a branch |
git stashPark uncommitted changes. git stash [command] [options]
| Command / option | What it does |
|---|---|
git stash (push) | Stash tracked changes |
-u (--include-untracked) | Also untracked files |
-a | Also ignored files |
-m "message" | Name the stash |
-p | Choose piece by piece |
push -- file | Stash only some files |
list | All stashes |
show -p stash@{1} | Diff of a stash |
pop | Re-apply the latest and remove it |
apply stash@{1} | Re-apply but keep it |
drop stash@{1} / clear | Delete one / all stashes |
branch name | New branch from a stash |
--keep-index | Stash, but leave staged changes in place |
git remoteManage remote repositories. git remote [command] [options]
| Command / option | What it does |
|---|---|
-v | List remotes with URLs |
add origin url | Add a remote |
remove name | Remove a remote |
rename old new | Rename a remote |
set-url origin url | Change the URL (HTTPS ↔ SSH) |
get-url origin | Show the URL |
show origin | Details: branches, tracking |
prune origin | Remove references to deleted remote branches |
git fetchDownload from the remote without changing your files. git fetch [options] [remote]
| Option | What it does |
|---|---|
--all | All remotes |
-p (--prune) | Remove references to deleted remote branches |
--tags | All tags |
origin main | One branch |
--depth 10 / --unshallow | Limit / complete the history of a shallow clone |
--dry-run | Show what would be fetched |
git tagMark releases. git tag [options] [name] [commit]
| Option | What it does |
|---|---|
(none) / -l "v1.*" | List tags (matching a pattern) |
v1.0.0 | Lightweight tag on the current commit |
-a v1.0.0 -m "Release" | Annotated tag (recommended for releases) |
v1.0.0 SHA | Tag an older commit |
-d v1.0.0 | Delete a local tag (git push origin --delete v1.0.0 for remote) |
-s | Signed tag |
-n | Show the tag messages |
--sort=-v:refname | Newest version first |
Tags are not pushed automatically: git push --tags.
git cherry-pickCopy commits from another branch onto this one. git cherry-pick [options] commit…
| Option | What it does |
|---|---|
git cherry-pick SHA | Apply one commit |
A..B | A range (excluding A; A^..B includes it) |
-x | Add "(cherry picked from …)" to the message |
-n (--no-commit) | Apply but don't commit |
-e | Edit the message |
-m 1 | Cherry-pick a merge commit |
--continue / --abort / --skip | After conflicts |
git bisectBinary-search the commit that introduced a bug. git bisect subcommand
| Subcommand | What it does |
|---|---|
start | Begin |
bad | The current commit has the bug |
good v1.0 | A commit (or tag) without the bug |
skip | Can't test this one, pick another |
run npm test | Automatic: run a test script on each step |
log / visualize | What has been tested |
reset | Stop and go back to where you started |
git worktreeSeveral branches checked out at once, in different folders. git worktree command
| Command | What it does |
|---|---|
add ../hotfix main | New folder with main checked out |
add -b fix ../fix | New branch in a new folder |
list | All worktrees |
remove ../hotfix | Remove a worktree |
prune | Clean up worktrees whose folder was deleted |
lock / unlock | Protect a worktree (e.g. on a USB drive) |
git rmRemove files from Git. git rm [options] paths
| Option | What it does |
|---|---|
git rm file | Delete the file and stage the deletion |
--cached file | Stop tracking, keep the file on disk (after adding it to .gitignore) |
-r folder | Recursive |
-f | Force, even with local changes |
-n | Dry run |
Nothing on this sheet matches “”.