iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Git Examples

A handful of git commands you reach for in real situations - bisect, cherry-pick, reflog, rerere, rebase --autosquash.

Git in real situations

EXAMPLE
# 1. Bisect to find a regression
git bisect start
git bisect bad                 # current HEAD is broken
git bisect good v1.4.0         # known good
# git checks out a midpoint; test it, then:
git bisect good                # or 'git bisect bad'
# repeat until it lands on the offending commit
git bisect reset


# 2. Cherry-pick a fix into a release branch
git checkout release/1.5
git cherry-pick abc123          # if it conflicts, resolve + 'git cherry-pick --continue'
# To preserve the original commit message AND add a note about the source:
git cherry-pick -x abc123       # adds '(cherry picked from commit abc123)'


# 3. Reflog - undo almost anything
git reflog                      # lists every HEAD move for ~90 days
git reset --hard HEAD@{4}       # back to that state
# Lost a commit? git reflog | grep <subject> -> git cherry-pick that sha


# 4. Rerere - reuse recorded resolutions
git config --global rerere.enabled true
# Long-lived feature branch + frequent rebases stop re-asking the same conflicts.


# 5. rebase --autosquash - clean up history before PR
git commit --fixup=abc123       # marks as fixup of an earlier commit
# ... more work ...
git rebase -i --autosquash HEAD~10    # combines fixups into target commits


# 6. Worktree - work on a hotfix without disturbing your branch
git worktree add ../hotfix main
cd ../hotfix
# ... do hotfix work ...
cd -
git worktree remove ../hotfix


# 7. Sparse checkout - check out only part of a giant repo
git clone --filter=blob:none --no-checkout https://example.com/monorepo
cd monorepo
git sparse-checkout init --cone
git sparse-checkout set apps/web packages/shared
git checkout main


# 8. Stash with a name
git stash push -m 'wip: dashboard skeleton'
git stash list
git stash apply stash@{0}


# 9. Compare branches
git log --left-right --oneline main...feature
git diff main...feature -- src/


# 10. Show who touched a line and when
git blame -L 100,120 src/server.ts
git log -p -L 100,120:src/server.ts        # follows the line through history


# 11. Find the commit that introduced a string
git log -S 'forbiddenWord' --all --source --pretty=oneline
git log -G 'regex.*pattern' --all          # by regex


# 12. Recover after a bad merge
git reflog                                  # find the merge SHA
git reset --hard <sha-before-merge>

Why it matters

Bisect for regressions, reflog for mistakes, rerere for long rebases, cherry-pick for hotfix backports, sparse checkout for monorepos. These are the commands that earn back their setup time the first day you need them.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# Common workflows — see lesson body.
git log --oneline --graph --decorate --all
Try it Yourself »

Discussion

Loading…