Git Cheat Sheet
The 20 Git commands you will actually use at work, from first commit to safely undoing mistakes on a shared branch.
Every engineering team runs on Git, and interviewers notice quickly whether you are comfortable with branches, rebases and undoing mistakes. Being fluent here also makes take-home assignments and pair-programming rounds far less stressful.
Work through the sheet in a scratch repository: run each example, break something on purpose, then fix it. Tick an item only once you can use the command without looking it up.
Start a repo
Get a project under version control or grab an existing one.
1git init
Turns the current folder into a Git repository by creating a hidden
.gitdirectory. Use it when starting a brand-new project locally.- Set the default branch name with
-b mainso it matches GitHub. - Add a
.gitignorebefore the first commit to keep secrets and build output out. - Link it to a remote later with
git remote add origin <url>.
mkdir todo-api && cd todo-api git init -b main echo "node_modules/" > .gitignore git remote add origin git@github.com:you/todo-api.git git remote -v
- Set the default branch name with
2git clone
Copies a remote repository, its full history and all branches, onto your machine and sets
originto point at it.--depth 1makes a shallow clone: fast, but history is missing.-b <branch>checks out a specific branch straight away.- SSH URLs avoid typing a token on every push.
git clone git@github.com:you/todo-api.git git clone --depth 1 https://github.com/torvalds/linux.git git clone -b develop git@github.com:team/app.git app-dev
Daily loop
Check, stage, commit, push: the rhythm of every working day.
3git status
Shows which files are modified, staged or untracked and how your branch compares with its upstream. Run it before and after almost every other command.
-sgives a compact two-column view (staged / unstaged).-sbalso shows ahead/behind counts for the branch.
git status git status -sb
4git add
Moves changes into the staging area, the snapshot your next commit will contain. Staging lets you commit only part of your work.
git add -plets you pick individual hunks; great for clean, focused commits.git add .stages everything under the current folder, including files you may not want.git add -ustages modifications and deletions but skips new files.
git add src/routes/todo.js git add -p git add -u
5git commit -m
Records the staged snapshot with a message. Good commits are small, focused and explain why the change was made.
- Write messages in the imperative: "Add pagination to todo list".
--amendrewrites the last commit; only do it before pushing.-amstages tracked files and commits in one go (new files are skipped).
git commit -m "Add pagination to GET /todos" git commit --amend --no-edit # add a forgotten file to the last commit git commit -am "Fix off-by-one in page size"
6git push
Uploads your local commits to the remote branch so others can see them and open a pull request.
-u origin <branch>sets the upstream once; afterwards plaingit pushworks.- Never
--forcea shared branch. If you must rewrite your own branch, use--force-with-lease, which refuses if someone else pushed.
git push -u origin feature/pagination git push git push --force-with-lease # after rebasing your own branch
Branching
Work in isolation and bring changes together cleanly.
7git branch
Lists, creates, renames and deletes branches. A branch is just a movable pointer to a commit, so creating one is instant and cheap.
-vvshows each branch's upstream and ahead/behind status.-ddeletes only merged branches;-Dforce-deletes (unmerged work can be lost).-aincludes remote-tracking branches.
git branch -vv git branch feature/search git branch -m old-name new-name git branch -d feature/pagination
8git switch
Moves you to another branch, or creates and moves in one step. It is the clearer, branch-only replacement for the overloaded
git checkout.-ccreates a new branch from where you are (or from a given start point).git switch -jumps back to the previous branch.- Uncommitted changes travel with you; stash them if they conflict.
git switch main git switch -c feature/search origin/main git switch -
9git merge
Combines another branch's history into the current one. If histories diverged, Git creates a merge commit; otherwise it simply fast-forwards.
--no-ffalways records a merge commit, keeping the feature visible in history.- On conflicts: fix the files,
git addthem, thengit commit; or bail out withgit merge --abort. - Merging never rewrites existing commits, so it is safe on shared branches.
git switch main git pull git merge --no-ff feature/search # conflict? edit files, then: git add src/search.js && git commit
10git rebase -i
Replays your commits on top of another base and lets you reorder, squash, reword or drop them first. Use it to tidy a feature branch before review.
- It creates new commits, so only rebase branches nobody else has built on.
- In the editor:
pick,squash/fixup(merge into previous),reword,drop. - Stuck?
git rebase --abortreturns everything to how it was.
git fetch origin git rebase -i origin/main # in the editor: # pick a1b2c3 Add search endpoint # fixup d4e5f6 typo # reword 789abc Add tests git push --force-with-lease
Sync with remote
Know exactly what comes down and when it gets merged.
11git fetch
Downloads new commits and branches from the remote without touching your working files. It is always safe, so use it to look before you merge.
- fetch = download only; pull = fetch + merge (or rebase) into your branch.
--pruneremoves remote-tracking branches that were deleted on the server.- Compare afterwards with
git log HEAD..origin/main.
git fetch --all --prune git log --oneline HEAD..origin/main # what's new upstream
12git pull
Fetches from the upstream branch and immediately integrates it into your current branch. Convenient, but it can surprise you with merge commits or conflicts.
--rebaseputs your local commits on top of upstream for a linear history.--ff-onlyrefuses anything but a clean fast-forward; a safe default formain.- Commit or stash local changes first.
git pull --rebase origin main git config --global pull.ff only git pull
Inspect
Read history and changes before you act on them.
13git log --oneline
Shows commit history, one line per commit. Add a graph and decorations to see how branches split and joined.
--graph --decorate --alldraws every branch as an ASCII tree.- Filter with
--author,--since,-- <path>or-S "text"(commits that added/removed text). -n 10limits the output.
git log --oneline --graph --decorate --all -n 20 git log --oneline --since="2 weeks ago" --author="Asha" git log --oneline -S "MAX_PAGE_SIZE" -- src/
14git diff
Shows line-by-line differences. With no arguments it compares working files to the staging area; with arguments it compares commits or branches.
--stagedshows what will go into the next commit.main...featureshows only what the feature branch changed since it split off.--statgives a per-file summary.
git diff git diff --staged git diff --stat main...feature/search git diff HEAD~3 -- src/routes/todo.js
Undo safely
Fix mistakes without losing work or breaking teammates.
15git stash
Shelves uncommitted changes so you get a clean working tree, for example to switch branches for an urgent fix, then restores them later.
-ualso stashes untracked files.popapplies and removes the stash;applykeeps it in the list.- Name stashes with
-msogit stash liststays readable.
git stash push -u -m "half-done search filters" git switch main # fix the urgent bug... git switch feature/search git stash list git stash pop
16git restore
Discards changes to files. It can unstage a file or throw away working-tree edits, replacing the confusing file uses of
git checkout.--stagedunstages but keeps your edits.- Without
--stagedit overwrites the file; uncommitted edits are gone for good. --source=<commit>restores a file as it was in an older commit.
git restore --staged config.yml # unstage git restore src/app.js # discard local edits git restore --source=HEAD~2 README.md
17git reset
Moves the current branch pointer to another commit. The mode decides what happens to your staged and working changes.
--softkeeps changes staged;--mixed(default) keeps them unstaged;--harddeletes them.--hardpermanently discards uncommitted work. Double-checkgit statusfirst.- It rewrites history: use it only on commits you have not pushed. Lost something?
git reflogcan usually find it.
git reset --soft HEAD~1 # undo last commit, keep it staged git reset HEAD~2 # undo 2 commits, keep edits git reset --hard origin/main # DANGER: match remote exactly git reflog # find a commit you 'lost'
18git revert
Creates a new commit that undoes an earlier one. History stays intact, which makes it the right way to undo something already pushed to a shared branch.
- revert = safe for shared branches; reset = only for local, unpushed commits.
- Reverting a merge commit needs
-m 1to say which parent to keep. --no-editaccepts the default message.
git revert --no-edit a1b2c3d git revert -m 1 9f8e7d6 # undo a merged PR git push
Power tools
Less frequent, but they save hours when you need them.
19git cherry-pick
Copies one or more specific commits from another branch onto the current one, for example to back-port a bug fix to a release branch.
-xappends "cherry picked from ..." to the message for traceability.- Ranges work:
A^..Bincludes A through B. - Overuse leads to duplicate commits; prefer merging when you need the whole branch.
git switch release/2.3 git cherry-pick -x 4c5d6e7 git cherry-pick a1b2c3d^..f0e9d8c # on conflict: fix, git add, then git cherry-pick --continue
20git bisect
Binary-searches history to find the commit that introduced a bug. You mark commits good or bad and Git halves the range each step.
- Finds the culprit among 1,000 commits in about 10 steps.
git bisect run <script>automates it if a test exits non-zero on failure.- Always finish with
git bisect reset.
git bisect start git bisect bad # current commit is broken git bisect good v2.1.0 # this tag worked git bisect run npm test -- search.spec.js git bisect reset
21Everyday workflowWorkflow
Most team work is the same loop: branch from fresh
main, commit small steps, rebase on the latestmain, push and open a pull request. Practise it end to end until it feels automatic.- Keep branches short-lived to avoid painful conflicts.
- Delete merged branches locally and remotely to stay tidy.
git switch main && git pull --ff-only git switch -c feature/export-csv # ...edit, test... git add -p && git commit -m "Add CSV export for todos" git fetch origin && git rebase origin/main git push -u origin feature/export-csv # after the PR is merged: git switch main && git pull && git branch -d feature/export-csv
Quick tips
- Create a throwaway repo and practise destructive commands (reset --hard, rebase) there first.
- Run
git statusandgit log --oneline -n 5before anything that rewrites history. git reflogis your safety net: almost nothing committed is truly lost for about 90 days.- Never rewrite history on shared branches; use revert there and keep rebase for your own branches.
- Small, well-named commits make code review, revert and bisect dramatically easier.
Topic list based on the free Everything You Need to Get Placed checklist by Mohammad Fraz. Explanations and examples are original.