home
›
cheatsheets
›
computer_science
›
git_version_control
Free to read Every card and diagram, and Quiz me
2
Free account Star cards. Your Quiz me marks are kept, so next time you drill just what you missed
3
Pro All 54 answers on this sheet, My sheet across every topic, and PDFs
A free account keeps your stars and Quiz me marks. Pro adds all 54 answers on this sheet, My sheet across every topic, and PDFs.
/
Read
Quiz me
Risky only
Comfortable
Compact
My misses 0
Click a command to copy it
Done 0 /0
To review 0
Safe
Rewrites, reflog can undo
Destroys, no easy undo
On this sheet
The model
01 Under the hood02 Stage & commit03 Undo & rescue04 Branch & stash05 Merge & rebase06 Remotes, push & pull07 Investigate08 Big repos & safety
The model A commit is a full snapshot of the project plus a pointer to its parent. Its hash is its name. History is a graph of commits. A branch is a movable pointer to one commit. HEAD points at the branch you're on, or straight at a commit when it's detached . git commit saves what's staged , not what's on disk.Every clone holds the whole history , so you can commit, branch and read the log offline. A server is just another copy. Anything committed can come back through git reflog, for 30 days by default. Edits you never committed can't. OUT arrows send work toward the team, and BACK arrows bring it to you. The red one overwrites edits you haven't committed.
Under the hood what Git actually stores What's in .git commit tree blob blob objects A commit points to one root tree. The tree names each file's blob by hash.Read any commit, tree or blob git cat-file -p <hash>
Show the tree the latest commit points to git cat-file -p HEAD^{tree}
List every file in the index with its blob hash git ls-files --stage
Pack loose objects into a packfile git gc
Delete unreachable commits now git reflog expire --expire-unreachable=now --all git gc --prune=now
Get the hash Git would give a file git hash-object <file>
Tip Git names each object by the hash of its content. So an unchanged file keeps the same blob in every commit.
Stage & commit turn edits into commits Stage and commit Stage one file, or everything git add <file> git add -A
Stage part of a file git add -p
Commit what's staged git commit -m "msg"
Stage all tracked changes and commit git commit -am "msg"
See edits you haven't staged git diff
See what's staged git diff --staged
Unstage a file, keep the edit git restore --staged <file>
Tip add -p asks about each change in turn, so you can commit the fix and leave out the debug print.
Trap git add stages the file as it is at that moment. Later edits stay out of the commit until you add the file again.
Fix the last commit Change the last commit's message or files git commit --amend
Add staged changes, keep the message git commit --amend --no-edit
Trap Amending makes a new commit with a new hash. If you already pushed, force-push it only to a branch that's yours alone.
Ignore files Stop tracking a file, keep it on disk git rm --cached <file>
Find which rule ignores a file git check-ignore -v <file>
Ignore editor junk in every repo git config --global core.excludesfile ~/.gitignore_global
Trap .gitignore only affects untracked files. A tracked file stays tracked until you git rm --cached it.
Write a commit message Imperative subject, 50 characters or less Return 200 for duplicate webhook events blank line Body says why, wrapped at 72 Our handler returned 500 on duplicates, so Stripe retried them for three days.
Undo & rescue take a change back, or get lost work back Undo commits with reset HEAD Index Files
git reset --soft HEAD~1 Undo the last commit, keep its changes staged
git reset HEAD~1 Undo the last commit, keep its changes unstaged
git reset --hard HEAD~1 Undo the last commit and discard all changes
reset to the target commit left alone uncommitted edits lost
Tip With no flag, reset uses --mixed. Only --hard touches your files.
Undo a pushed commit Reverse a commit with a new one git revert <hash>
Tip Use revert once a commit is pushed, and reset while it's still local. Revert adds a commit and rewrites nothing.
Throw away local edits Discard edits to one file git restore <file>
Discard all unstaged edits git restore .
Discard staged and unstaged edits git restore --staged --worktree .
List untracked files before you delete them git clean -nd
Delete untracked files and folders git clean -fd
Trap The reflog only tracks commits. Uncommitted work you delete here is gone for good.
Get lost work back List everywhere HEAD has been git reflog
Put a branch on a lost commit git switch -c rescue <hash>
Undo a rebase that's still in progress git rebase --abort
Undo a rebase you just finished git reset --hard ORIG_HEAD
Tip The reflog keeps a lost commit for 30 days by default. After that, git gc can delete it.
Trap The reflog lives only in your clone. A fresh clone can't rescue anything.
Branch & stash hold two lines of work at once Branches List local and remote branches git branch -a
Create a branch and switch to it git switch -c <name>
Switch to a branch git switch <name>
Go back to the previous branch git switch -
Rename the current branch git branch -m <new>
Delete a merged branch git branch -d <name>
Force-delete an unmerged branch git branch -D <name>
Trap If you commit on a detached HEAD, no branch holds the new commits. Run git switch -c <name> before you switch away.
Stash unfinished work Set aside changes to tracked files git stash
Stash untracked files too git stash -u
List your stashes git stash list
Apply the newest stash and drop it git stash pop
Apply a stash and keep it git stash apply
Delete a stash git stash drop stash@{0}
Work on another branch without stashing git worktree add ../app-hotfix hotfix/login A second checkout of the same repo, in its own folder. No stash, no second clone.
Trap A stash isn't tied to a branch. Pop it on the wrong branch and it can apply cleanly in the wrong place.
Merge & rebase join branches and settle conflicts Merge or rebase A B C M D E merge History as it happened, plus merge commit M.A B C D′ E′ D E rebase A straight line, but D and E get new hashes.Merge a branch into yours git merge <branch>
Replay your commits on top of a branch git rebase <branch>
Trap Never rebase commits someone else may have pulled. Their next pull brings the old copies back as duplicate commits.
Fast-forward or merge commit Merge only if it can fast-forward git merge --ff-only <branch>
Always record a merge commit git merge --no-ff <branch>
Find the common ancestor of two branches git merge-base main <branch>
Tip A fast-forward just moves the pointer and adds no commit. Git can only do it when your tip is an ancestor of the other branch.
Pick a PR merge strategy Land a PR as one commit Squash merge. It's the usual default, because one revert undoes the whole PR.
Keep every commit and show where the branch joined Merge commit. Pick it when each commit means something on its own.
Keep every commit in one straight line Rebase merge. Use it only for curated commits. CI usually tested the tip, not each replayed commit.
Trap Squash leaves the branch's own commits off main, so its step-by-step history lives only in the pull request.
Tidy commits before a PR Make a backup branch first git branch backup-tip
Squash, reorder or reword commits git rebase -i <base>
Mark a fix for an earlier commit git commit --fixup <hash>
Squash the fixup commits into place git rebase -i --autosquash <base>
Tip After squashing, git diff backup-tip prints nothing if the files came out identical.
Copy one commit to another branch Copy a commit onto your branch and note its source git cherry-pick -x <hash>
Trap Cherry-pick makes a new commit with a new hash. The same change then lives under two hashes, and a later merge conflicts if you edited the copy.
Resolve a conflict <<<<<<< HEAD yours your code ======= their code >>>>>>> feature theirs
List the files with conflicts git status
Mark a file resolved git add <file>
Continue the merge or rebase git merge --continue git rebase --continue
Give up and go back git merge --abort git rebase --abort
Replay a conflict fix you made before git config --global rerere.enabled true
Trap In a rebase, ours and theirs swap. HEAD is the branch you're replaying onto, so your own commit is "theirs".
Keep a long-lived branch in sync Count commits behind and ahead of main git rev-list --left-right --count origin/main...HEAD
Test-merge main without touching your files (2.38+) git merge-tree --write-tree origin/main HEAD
Pick a branching model Git Flow keeps develop beside main. GitHub flow and trunk-based keep only main, with short branches.
Tip Sync with main every day, not at the end. The longer you wait, the worse the conflicts. Better still, land small pieces behind a feature flag.
Trap A clean merge can still break the build. A rename on main and a new caller on your branch never touch the same lines.
Remotes, push & pull move commits between you and the team Clone and remotes Copy a repo with all its history git clone <url>
Connect a repo you made with git init git remote add origin <url>
Show remotes and their URLs git remote -v
Switch the remote from HTTPS to SSH git remote set-url origin <url>
See where fetch puts remote branches git config --get-all remote.origin.fetch
Delete a branch on the remote git push origin --delete <branch>
Tip Git runs on your machine and works offline. GitHub hosts a copy and adds pull requests, review and CI.
Forks and server repos Propose changes to a repo you can't push to Fork it on the host and clone your fork. Then open a pull request against the upstream.
Track the original repo git remote add upstream <url>
Bring your fork up to date git fetch upstream git merge upstream/main Run it on main, then push to your fork. A fork never updates itself.
Make a server repo with no working tree git init --bare app.git
Fetch, pull, push Download new commits, leave your branches alone git fetch
Fetch, then fast-forward your branch git pull It stops if your branch has diverged. git pull --no-rebase merges instead.
Fetch and rebase git pull --rebase
Push the current branch git push
Push a new branch and set its upstream git push -u origin <branch>
Tie an existing branch to its remote git branch -u origin/<branch>
Count commits ahead of and behind your upstream git status -sb
Force-push your own branch git push --force-with-lease
Force-push only if your branch has what you fetched (2.30+) git push --force-with-lease --force-if-includes
Tip Commit or stash before you pull. If the merge conflicts, git merge --abort may not bring your uncommitted edits back.
Trap Plain --force overwrites the remote branch, teammates' commits included. --force-with-lease refuses if that branch moved since your last fetch.
Tag a release Make an annotated tag for a release git tag -a v1.0 -m "msg"
Bookmark a commit with a lightweight tag git tag v1.0
Push your tags git push --tags
List tags git tag
Tip Use annotated tags for anything you share. git describe skips lightweight tags unless you pass --tags.
Trap git push doesn't send tags. Push them on purpose.
Investigate find the commit that did it Find a bad commit with bisect 1 2 3 4 good first bad bad Each test halves the suspects. 16 commits take 4 tests, and 10,000 take about 14.
Start with a bad and a good commit git bisect start <bad> <good>
Mark the commit Git checked out git bisect good git bisect bad
Let a script test each commit git bisect run ./repro.sh
Skip a commit that won't build git bisect skip
Finish and return to where you started git bisect reset
Tip Bisect assumes good stays good until one commit flips it. A flaky test breaks that.
Read history See who last changed each line git blame <file>
Find the commit that added or removed a string git log -S "text"
Find commits whose diff matches a regex git log -G "regex"
Follow one file across renames git log --follow -- <file>
Draw every branch as a graph git log --oneline --graph --all
See what one commit changed git show <hash>
Tip Git stores no renames. It guesses them at diff time from content similarity, 50% by default.
Trap A rename plus heavy edits in one commit can fall under the threshold and lose the link. Move the file in one commit, then edit it.
Big repos & safety what senior interviews add Big repos Clone only the latest commit, for CI git clone --depth=1 <url>
Clone history, fetch file contents on demand git clone --filter=blob:none <url>
Check out only some folders git sparse-checkout set --cone <dir>
Make git status fast in a huge repo git config core.fsmonitor true
Keep large binaries out of history git lfs track "*.psd"
Trap A shallow clone has no history past the cutoff. blame and bisect can't see it, and a rebase across it fails.
Share code between repos Pin another repo at one commit git submodule add <url> <dir> Clones then need --recurse-submodules.
Copy another repo's files in as commits git subtree add --prefix=<dir> <url> main --squash A plain clone gets everything, but your repo grows.
Trap Moving a submodule to a new commit is a change you must commit. Forget it and teammates keep building the old version.
Hooks and signed commits Skip the client hooks on one commit git commit --no-verify Anyone can, so client hooks give feedback, not enforcement.
Enforce a rule nobody can skip Use a pre-receive hook on the server. On GitHub, use protected branches.
Sign every commit git config --global commit.gpgsign true
Check a commit's signature git log --show-signature -1
Tip A signature proves which key made a commit, not who owns that key.
Clean up a leaked secret Rotate the secret first Treat it as public. Clones, CI logs and backups may already hold it.
Scrub the secret from every commit git filter-repo --replace-text replacements.txt
Add back the remote filter-repo removed git remote add origin <url>
Publish the rewritten history git push --force --mirror
Tip The host still serves the old commit by its hash. Ask your provider to garbage-collect it.
Trap Tell everyone to re-clone. Anyone who pulls instead merges the old history back in, secret included.
Nothing matches. Try a shorter word, or clear the search.
Practice Guided practice One question at a time, with the ones you miss coming back.
Practice 54 interview questions Full answers, from beginner to expert.
Found this useful? Pass it on.
Share on LinkedIn
Share on X
Copy link
This sheet stays free. With Pro , you can:
Every answer on this sheet. Read the full answer to all 54 questions it links, not just the 7 free ones.
My sheet. The cards you star and the ones you miss in Quiz me, from every sheet, on one page to quiz and print.
PDFs. Download this sheet and My sheet, laid out for A4, with every question a link to its answer.
The rest of Pro. Every answer in the question library, and 1,000 AI credits a month.