Engineering
Git, Without the Mystery
The everyday commands, with pictures — a quick-reference guide for the team

1. Everyday command
The 90% of git you actually use, day to day.
Starting your work
git status # what changed and where am I
git switch main # jump to main branch
git switch -c my-feature # create + switch to a new branch
git pull # update current branch from remote
Saving your work
git add <file> # stage a specific file
git add . # stage everything in current dir
git commit -m "message" # commit staged changes
git commit -am "message" # stage tracked + commit in one shot
git push # send commits to remote
git push -u origin <branch> # first push of a new branch
Looking around
git log --oneline # compact commit history
git log --oneline --graph # history with branch graph
git diff # changes not yet staged
git diff --staged # changes staged but not committed
git branch # list local branches
git branch -a # list all (local + remote)
Undoing things
git restore <file> # discard unstaged changes to a file
git restore --staged <file> # unstage (keep the edit)
git commit --amend # fix the last commit (message or content)
git revert <sha> # create a new commit that undoes <sha>
2. Pull vs Fetch
git fetch
Downloads new commits from the remote into your local copy of the remote branches (like origin/main). Your working branch is untouched. Safe to run any time — nothing changes in your files.
git fetch # fetch from origin
git fetch --all # fetch from every remote

git pull
Shorthand for fetch + merge. Updates your current branch with the latest commits from the remote in one step.
git pull # fetch + merge
git pull --rebase # fetch + rebase (cleaner history)

💡 Use fetch when: you just want to see what changed without modifying your branch — handy before a code review or before deciding whether to merge or rebase.
3. Working with multiple remotes
A "remote" is just a named URL pointing to a copy of the repo. By default you have one called origin, but you can add as many as you want. Common reasons:
- Forks:
originis your fork,upstreamis the original repo you want to sync from. - Mirrors: push to both repos to keep them in sync.
- Migrations: pulling from the old remote, pushing to the new one during a transition.

See what remotes you have
git remote -v # list all remotes with their URLs
# origin git@github.com:me/my-fork.git (fetch)
# origin git@github.com:me/my-fork.git (push)
# upstream git@github.com:company/the-repo.git (fetch)
# upstream git@github.com:company/the-repo.git (push)
Add, rename, remove
git remote add upstream git@github.com:company/the-repo.git
git remote rename origin fork # rename a remote
git remote remove upstream # remove a remote
git remote set-url origin <new-url> # change a remote's URL
Fetch from a specific remote
git fetch upstream # download upstream's branches into upstream/*
git fetch --all # fetch from every remote at once
Pull from one remote, push to another
The classic fork workflow: keep your fork in sync with the upstream repo, but always push your own work to your fork.
git fetch upstream # get latest from the original repo
git merge upstream/main # merge their main into yours
# (or rebase, if you prefer linear history)
git rebase upstream/main
git push origin main # push the updated main to your fork

Push to a specific remote and branch
git push origin my-feature # push to origin's "my-feature"
git push upstream my-feature # push to upstream's "my-feature"
git push origin local-name:remote-name # push local branch under a different remote name
Set a different upstream for pull vs push
You can make one branch pull from upstream/main but push to origin/main automatically — no flags needed every time:
git branch --set-upstream-to=upstream/main main # pulls come from upstream
git config remote.pushDefault origin # pushes go to origin by default
After that, git pull grabs from upstream and git push sends to origin. Exactly what you want when working from a fork.
Push the same commits to two remotes
Useful for mirroring. Two ways:
Option A — push twice (explicit):
git push origin main
git push backup main
Option B — configure a remote to push to multiple URLs at once:
git remote set-url --add --push origin git@github.com:me/repo.git
git remote set-url --add --push origin git@gitlab.com:me/repo.git
# now `git push origin main` sends to both

💡 Naming convention:
originis your primary remote (where you push),upstreamis the source of truth you pull from. Sticking to this convention makes any guide on the internet immediately readable.
4. Merge strategies
git merge
Combines another branch into your current one. If the branches have diverged, git creates a merge commit that records the combination.
git switch main
git merge feature # merge "feature" into main

git merge --ff-only
Only allow the merge if it can be done as a fast-forward — meaning your current branch can simply move its pointer forward without creating a merge commit. If the branches have diverged, the command fails (which is exactly what you want when enforcing clean linear history).
git merge --ff-only feature

💡 Rule of thumb:
--ff-onlyis what most teams want for the main branch — it keeps history linear and forces you to rebase your branch first if it has fallen behind.
5. Reset: soft vs hard
reset moves your branch pointer to an earlier commit. The flag controls what happens to your files and the staging area.
git reset --soft
Moves HEAD back, but leaves your working files alone AND keeps everything staged. Perfect for "I want to redo the last commit" — your changes are still there, ready to recommit.
git reset --soft HEAD~1 # undo last commit, keep work staged

git reset --hard
Moves HEAD back AND wipes your working files AND clears the staging area. Everything since that commit is gone (from your working copy — recoverable via reflog within ~90 days, but assume it's gone).
git reset --hard HEAD~1 # nuke the last commit and all its changes

⚠ Danger:
--hardcannot be undone through normal git commands. Never use it if you have uncommitted work you care about, and never reset commits you have already pushed to a shared branch.💡 Middle ground:
git reset(with no flag) is "mixed" — it unstages your changes but keeps them in your working files. It's the default if you forget the flag.
6. Rebase (quick explanation)
Rebase takes your branch commits and replays them on top of another branch, as if you had branched from there originally. The result is a clean, linear history with no merge commits.
git switch feature
git rebase main # replay feature commits on top of main

Compared to merge:
- Merge preserves the actual history (you can see when branches existed in parallel).
- Rebase rewrites history into a straight line (easier to read, but the commits get new SHAs).
⚠ Golden rule: never rebase commits you have already pushed to a shared branch. Rebasing changes the commit SHAs, which breaks everyone else's local copy.
7. Hidden tricks worth knowing
Jump back to the previous branch
Like cd - in your shell, but for branches. Super handy when bouncing between a feature branch and main.
git switch - # same as: git checkout -
Squash your last N commits before opening a PR
Interactive rebase lets you combine "wip", "fix typo", and "actually fix it" into a single clean commit. Replace 3 with however many commits you want to tidy up.
git rebase -i HEAD~3
In the editor that opens, leave the first commit as pick and change the rest to squash (or just s). Save, then write a clean combined commit message. Done.
Quickly stash and come back
You're mid-feature and need to switch branches to fix a bug:
git stash # park your work
git switch main
# ... do the fix ...
git switch -
git stash pop # bring your work back
Find which commit broke something
Binary search through history to find the culprit:
git bisect start
git bisect bad # current commit is broken
git bisect good <sha> # this old commit worked
# git checks out commits in between — test and mark each:
git bisect good # (or git bisect bad)
# ... repeat until git tells you the bad commit
git bisect reset # done
See what you just did (the reflog)
Even after a bad reset --hard, git remembers where HEAD has been. Reflog can save your life.
git reflog # show every HEAD move
git reset --hard HEAD@{2} # jump back to where you were 2 moves ago
Show who changed each line
git blame <file> # see commit + author per line
Search the entire repo
git grep "TODO" # like grep, but only tracked files
git log -S "functionName" # find commits that added/removed a string
Amend without changing the message
git commit --amend --no-edit # add staged changes to last commit, keep message
Delete a branch
git branch -d feature # delete merged branch (safe)
git branch -D feature # force delete (unmerged work will be lost)
git push origin --delete feature # delete the remote branch
git push origin :feature # same thing, older shorthand syntax
Clean up untracked files
git clean -n # dry run: show what would be removed
git clean -fd # actually delete untracked files + folders
8. One-page cheat sheet
Daily flow
git pull
git switch -c my-feature
# ... work ...
git add .
git commit -m "what I did"
git push -u origin my-feature
# open PR
Before opening a PR — clean up your branch
git rebase -i HEAD~N # squash messy commits
git pull --rebase origin main # rebase onto latest main
git push --force-with-lease # safely force-push after rebase
⚠ Force-pushing: always use
--force-with-leaseinstead of--force. It refuses to overwrite work others may have pushed while you weren't looking.
Oh no, I messed up
Discarded an edit before committing — restore the file:
git restore <file>
Committed too early — undo the commit, keep the changes:
git reset --soft HEAD~1
Already pushed and others have it — undo a pushed commit safely:
git revert <sha>
Used reset --hard and want it back — recover from a bad reset:
git reflog
git reset --hard HEAD@{1}
9. Deep dive: git reset --hard HEAD@{1}
This is the command that saves you after a panic. Worth understanding properly.
What HEAD@{1} actually means
HEAD@{N} is reflog syntax. It means "where HEAD was N moves ago." It is not the same as HEAD~N.
HEAD~1→ the parent commit of where you are now (walks the commit graph)HEAD@{1}→ wherever HEAD was pointing one move ago (walks the history of HEAD itself)
Git keeps a private journal called the reflog that records every time HEAD moves — every commit, checkout, merge, rebase, reset, pull. It's local to your machine and lives for ~90 days by default. Other people never see it.
A concrete example
You're on main with 3 good commits and you do something dumb:
git log --oneline
# a1b1c1d (HEAD -> main) Add the important feature
# 9f8e7d6 Fix the bug
# 5a4b3c2 Initial work
git reset --hard HEAD~2 # 😱 you meant HEAD~1
# Now you're back at "Initial work" and the other 2 commits look gone
Check the reflog:
git reflog
# 5a4b3c2 HEAD@{0} reset: moving to HEAD~2 ← where you are NOW
# a1b1c1d HEAD@{1} commit: Add the important feature ← where you were before the reset
# 9f8e7d6 HEAD@{2} commit: Fix the bug
# 5a4b3c2 HEAD@{3} commit: Initial work
Read top to bottom as "most recent move first." HEAD@{1} is whatever HEAD pointed to immediately before the current position. So:
git reset --hard HEAD@{1}
# You're back at a1b1c1d, all your work restored. Crisis averted.
The mental model
Think of the reflog as your browser history for git:
HEAD@{0}is "the current page"HEAD@{1}is "back one"HEAD@{2}is "back two"
Even after a reset --hard, the commits aren't deleted — they're just orphaned (no branch points to them). The reflog still knows their SHAs, so you can jump back.
When you'll need this
The classic disasters this rescues you from:
git reset --hardto the wrong commit- A
git rebasethat mangled your branch - Accidentally deleting a branch with
git branch -D - A
git pullthat pulled in a mess - Switching branches and somehow losing work
Two practical tips
1. Use timestamps when you can't count moves. If something went wrong earlier today:
git reset --hard HEAD@{"2 hours ago"}
git reset --hard main@{yesterday}
2. Look before you leap. Always run git reflog first and read the entries — then reset to the specific HEAD@{N} you want. Don't guess. The descriptions on the right (commit:, reset:, checkout:, pull:) tell you exactly what happened at each step.
⚠ The catch: the reflog is local to your machine. If you
reset --hardand then immediately re-clone the repo, the reflog is gone. And if a coworker is the one who lost work, your reflog can't help them — they need to check their own.