Opscraft
Git · Lesson 2 of 8 · 3 min read

Everyday workflow and investigating history

Core commands

git switch -c feature/payments-retry      # create and switch (modern; older: git checkout -b)
git add -p                                 # stage hunks interactively
git commit -m "payments: retry on 503 with backoff"
git commit --amend                         # fix the last commit (only if not pushed/shared)
git log --oneline --graph --decorate --all # visual history
git log -p -- path/to/file                 # history of one file with diffs
git log -S "AWS_REGION"                    # commits that added/removed a string (pickaxe)
git show 3f9c2a1                           # one commit
git diff main...feature                    # changes on feature since it diverged from main
git blame -L 40,60 app.py                  # who last changed each line
git stash push -m "wip" ; git stash pop    # shelve uncommitted work
git tag -a v1.4.0 -m "Release 1.4.0" ; git push origin v1.4.0

Write good commit messages: a short imperative subject (≤ 50–72 chars), a blank line, then why the change was made. Many teams use Conventional Commits (feat:, fix:, chore:, feat!: for breaking) so changelogs and semantic versions can be generated automatically.

git bisect — find the commit that broke something

git bisect start
git bisect bad                 # current commit is broken
git bisect good v1.3.0         # this release was fine
# Git checks out a midpoint; test it, then:
git bisect good    # or: git bisect bad
# ...repeat (log2(N) steps) until Git names the first bad commit
git bisect run ./scripts/test.sh   # or automate it: exit 0 = good, non-zero = bad
git bisect reset

Binary search through history — 1,000 commits take about 10 steps. A great tool for "this broke sometime in the last two weeks".

Enlarge diagramAfter step 4 Git names commit 6 as the first bad commit. git bisect run ./test.sh does all the steps for you.step 1test → badstep 2test → goodstep 3test → badstep 4test → badgood (v1.3.0)bad (today)
Figure 1. git bisect is a binary search through history: each test halves the suspects, so 16 commits take 4 steps and 1,000 take about 10.
Swipe sideways to see all of it, or tap to open it full screen.

git reflog — your safety net

The reflog records every position HEAD and branches have been at locally (for ~90 days by default), even after resets and rebases:

git reflog
# 3f9c2a1 HEAD@{0}: reset: moving to HEAD~3
# 8d21be4 HEAD@{1}: commit: add retry logic      ← the "lost" commit
git branch rescue 8d21be4      # or: git reset --hard 8d21be4

Almost nothing committed is truly lost locally until garbage collection prunes unreachable objects.

Enlarge diagramABCDgit branch rescue Dgets them backmain (after reset)was mainC and D: no branch points here,but the reflog still lists themgit reset --hard HEAD~2 moved main from D back to B.
Figure 2. A hard reset moves the branch back, but the "lost" commits still exist. The reflog remembers where HEAD has been, so you can get them back.
Swipe sideways to see all of it, or tap to open it full screen.