Day 10: Git in the terminal
Goal: Use Git so that history, branches and debugging are fast without a GUI. · Duration: approx. 90 min · Prerequisite: Day 6
Briefing
- All drills use the lab configuration
~/term-lab/gitconfigviaGIT_CONFIG_GLOBAL. Your real~/.gitconfigstays untouched. switchandrestorereplace the overloaded variants ofcheckout.- Fixup commits plus
--autosquashclean up the history before others see it. - Worktrees give you several branches checked out at the same time,
without
stash. bisect runfinds the guilty commit automatically.
main ──A──B──C──D──E feature/sub ──F──G──fixup!F
rebase -i --autosquash => F'──G'
The outputs come from Git 2.56 with a German locale. With
LANG=C you see English.
Drill 1: Lab configuration and practice repo
Work in the practice zsh (~/term-lab/lab, Day 0). Its
.zshenv already sets
GIT_CONFIG_GLOBAL=~/term-lab/gitconfig. Replace the minimal
config with the full one:
echo $GIT_CONFIG_GLOBAL
cp $BUCH/git/gitconfig ~/term-lab/gitconfig
git config --global --get-regexp '^(user|core|rebase)\.'Expected:
/Users/<du>/term-lab/gitconfig
user.name Lab User
user.email [email protected]
core.editor nvim
core.pager delta --navigate --line-numbers
rebase.autosquash true
rebase.autostash true
rebase.updaterefs true
Warning: If
GIT_CONFIG_GLOBALis empty, you’re not in the lab zsh. Thengit config --globalwrites to your real~/.gitconfig.
Now a throwaway repo with some history:
mkdir -p ~/term-lab/tag10 && cd ~/term-lab/tag10 && git init -q
cat > calc.py <<'EOF'
def add(a, b):
return a + b
def mul(a, b):
return a * b
EOF
git add calc.py && git commit -qm "feat: add und mul"
for i in 1 2 3 4 5 6 7 8; do
echo "# Schritt $i" >> NOTES.md
git add NOTES.md && git commit -qm "docs: Schritt $i"
done
git ll | head -3
git aliases | head -4Expected (hashes will differ):
6008dd4 docs: Schritt 8
510cbe9 docs: Schritt 7
6bfef76 docs: Schritt 6
s status -sb
co switch
cob switch -c
unstage restore --staged
The aliases are short, but meaningful. git aliases lists
them for you at any time.
Save your progress? Sign up for free
Drill 2: switch and restore
git cob feature/sub
git co -
git co -
git branch --show-current
echo "kaputt" >> calc.py
git s
git restore calc.py
git sExpected:
Zu neuem Branch 'feature/sub' gewechselt
Zu Branch 'main' gewechselt
Zu Branch 'feature/sub' gewechselt
feature/sub
## feature/sub
M calc.py
## feature/sub
git switch - jumps to the previous branch, just like
cd -. git restore discards changes in the
working tree. git restore --staged (alias
unstage) only takes them out of the index.
| Old (checkout) | New | Effect |
|---|---|---|
git checkout -b x |
git switch -c x |
create branch and switch |
git checkout x |
git switch x |
switch branch |
git checkout -- f |
git restore f |
discard change |
git reset HEAD f |
git restore --staged f |
take out of the index |
Save your progress? Sign up for free
Drill 3: Interactive staging
You changed two things: a debug output (shouldn’t go into the commit) and a new function.
cat > calc.py <<'EOF'
def add(a, b):
print("DEBUG add", a, b)
return a + b
def mul(a, b):
return a * b
def sub(a, b):
return a - b
EOF
git add -pGit shows a single hunk. Type s (split), then
n for the debug block and y for
sub. Sequence (shortened, diff lines omitted):
(1/1) Stage this hunk [y,n,q,a,d,s,e,p,P,?]? s
In 2 Patch-Block aufgeteilt.
+ print("DEBUG add", a, b)
(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]? n
+def sub(a, b):
+ return a - b
(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,p,P,?]? y
git diff --cached --stat
git diff --stat
git commit -qm "feat: sub"
git restore calc.pyExpected:
calc.py | 4 ++++
1 file changed, 4 insertions(+)
calc.py | 1 +
1 file changed, 1 insertion(+)
The debug print stays unstaged and gets thrown out with
restore. If a hunk can’t be split because the lines are
directly next to each other, use e and edit the patch in
Neovim.
Tip: Because
interactive.diffFilter = delta --color-onlyis set,add -pis syntax-colored too.
Save your progress? Sign up for free
Drill 4: fixup and autosquash
You keep committing and then notice that sub needs a
docstring. In terms of content it belongs in the old commit, not in a
new one.
printf '\n\ndef div(a, b):\n return a / b\n' >> calc.py
git commit -qam "feat: div"
perl -pi -e 's/^( return a - b)/ """a minus b"""\n$1/' calc.py
git add calc.py
git fixup ":/feat: sub"
git ll | head -4Expected:
[feature/sub 373eeea] fixup! feat: sub
1 file changed, 1 insertion(+)
373eeea fixup! feat: sub
89fe008 feat: div
784c66d feat: sub
6008dd4 docs: Schritt 8
:/feat: sub addresses the youngest commit whose message
matches the pattern. You don’t have to copy a hash. Now display the todo
list without opening an editor:
GIT_SEQUENCE_EDITOR=cat git ri main
git ll | head -3Expected (shortened):
pick 784c66d # feat: sub
fixup 373eeea # fixup! feat: sub
pick 89fe008 # feat: div
Erfolgreich Rebase ausgeführt und refs/heads/feature/sub aktualisiert.
a933236 feat: div
4ca2126 feat: sub
6008dd4 docs: Schritt 8
Why? --autosquash sorts fixup! commits
behind their target. GIT_SEQUENCE_EDITOR=cat only prints
the list and takes it over unchanged. In everyday use you leave out the
variable and edit the list in Neovim.
| Command in the todo list | Effect |
|---|---|
pick / p |
keep the commit |
reword / r |
change the message |
edit / e |
stop, e.g. to split it |
fixup / f |
fold into the predecessor, discard the message |
squash / s |
fold, combine the messages |
drop / d |
remove the commit |
Tip: If a rebase goes wrong,
git reflogbrings back any old state.git rebase --abortcancels a running rebase.
Save your progress? Sign up for free
Drill 5: Worktrees
A hotfix comes in while your feature branch is half done. Instead of
stash:
git co main
git worktree add ../tag10-hotfix -b hotfix/mul
git wt
(cd ../tag10-hotfix && git s)Expected:
Zu Branch 'main' gewechselt
Bereite Arbeitsverzeichnis vor (neuer Branch 'hotfix/mul')
HEAD ist jetzt bei 6008dd4 docs: Schritt 8
/Users/<du>/term-lab/tag10 6008dd4 [main]
/Users/<du>/term-lab/tag10-hotfix 6008dd4 [hotfix/mul]
## hotfix/mul
A branch can only be checked out in one worktree:
git switch hotfix/mulExpected (wrapped):
Schwerwiegend: 'hotfix/mul' wird bereits von Arbeitsverzeichnis in
'/Users/<du>/term-lab/tag10-hotfix' verwendet
Clean up:
git worktree remove ../tag10-hotfix
git branch -D hotfix/mul
git wtExpected:
Branch hotfix/mul entfernt (war 6008dd4).
/Users/<du>/term-lab/tag10 6008dd4 [main]
Tip: One tmux session per worktree (Day 9) – that way tests run in the hotfix while the editor stays in the feature.
Save your progress? Sign up for free
Drill 6: bisect run
Twelve refactoring commits, one of which breaks mul. You
plant the bug on purpose:
git cob bug/hunt
for i in $(seq 1 12); do
echo "# Refactor $i" >> calc.py
[ $i -eq 7 ] && perl -pi -e 's/a \* b/a ** b/' calc.py
git commit -qam "refactor: Schritt $i"
done
cat > test_mul.sh <<'EOF'
#!/bin/sh
python3 -c 'import calc; assert calc.mul(3, 4) == 12'
EOF
chmod +x test_mul.shbisect run evaluates the exit code: 0 means good, 1–127
means bad, 125 means skip.
git bisect start HEAD main
git bisect run ./test_mul.sh 2>/dev/null
git bisect resetExpected (shortened):
Binäre Suche: danach noch 5 Commits zum Testen übrig (ungefähr 3 Schritte)
Ausführen von './test_mul.sh'
Binäre Suche: danach noch 2 Commits zum Testen übrig (ungefähr 2 Schritte)
...
64a7aed91353974128d7f6f71fe37e04ab8b6548 is the first 'bad' commit
refactor: Schritt 7
bisect found first 'bad' commit
Zu Branch 'bug/hunt' gewechselt
Four test runs instead of twelve. With 1000 commits it would be about ten. The test script sits untracked in the repo and therefore survives every checkout.
Save your progress? Sign up for free
Drill 7: delta, lazygit and gh
delta is already the pager. Look at the guilty commit from Drill 6:
git show ":/refactor: Schritt 7"Expected (excerpt, in color in the terminal):
Δ calc.py
────────────────────────────────────────────────────────────
5 ⋮ 5 │def mul(a, b):
6 ⋮ │ return a * b
⋮ 6 │ return a ** b
With n/N you jump to the next/previous
file, with q you get out. For side by side, a one-off
git -c core.pager='delta -s' show HEAD is enough.
Warning: delta reads its
[delta]section only from~/.gitconfig, not fromGIT_CONFIG_GLOBAL. That’s why in the lab the flags are directly incore.pager.
lazygit is the TUI for everything that’s cumbersome in the CLI: staging hunks, reordering commits, resolving conflicts.
lazygit --version | cut -d, -f4
lazygitExpected:
version=0.64.1
| Key in lazygit | Effect |
|---|---|
1–5 |
select panel (Status, Files, Branches, Commits, Stash) |
space |
stage file/hunk or check out branch |
Enter |
jump into the file, stage individual lines there |
c |
write a commit |
F (in Commits) |
fixup commit for the selected commit |
? |
all keys in the current panel |
gh brings GitHub into the terminal. So that the lab
doesn’t touch your real gh configuration, it gets its own config
directory:
export GH_CONFIG_DIR=~/term-lab/gh
gh alias set prs 'pr list --author "@me"'
gh alias listExpected (co is a built-in default alias):
co: pr checkout
prs: pr list --author "@me"
Typical commands in a real repo (login via gh auth login
required):
gh pr create --fill --draft # create PR from commits
gh pr checks --watch # follow CI live
gh pr checkout 123 # check out PR locally
gh run view --log-failed # only the red job logsSave your progress? Sign up for free
Shortcut table
| Key/command | Effect | Mnemonic |
|---|---|---|
git co - |
back to the previous branch | like cd - |
y / n / s / e in
add -p |
stage / skip / split / edit | yes, no, split, edit |
git fixup :/muster |
fixup for the commit with a matching message | :/ = search |
git ri main |
rebase with autosquash | rebase interactive |
n / N in delta |
next / previous file | next |
F in lazygit |
fixup commit | Fixup |
git reflog |
find any earlier HEAD | life preserver |
Checkpoint
cd ~/term-lab/tag10
ok() { if eval "$1" >/dev/null 2>&1; then echo "✔ $2"; else echo "✘ $2"; fi; }
ok '[ "$GIT_CONFIG_GLOBAL" = "$HOME/term-lab/gitconfig" ]' "Lab-Gitconfig aktiv"
ok '[ "$(git config rebase.autosquash)" = true ]' "autosquash an"
ok '! git log --format=%s main..feature/sub | grep -q "^fixup!"' "keine fixup!-Commits"
ok '[ $(git rev-list --count main..feature/sub) -eq 2 ]' "feature/sub: 2 Commits"
ok 'git show feature/sub:calc.py | grep -q "a minus b"' "Docstring in sub"
ok '[ "$(git worktree list | wc -l)" -eq 1 ]' "kein verwaister Worktree"
ok '! git bisect log' "kein laufender Bisect"Expected:
✔ Lab-Gitconfig aktiv
✔ autosquash an
✔ keine fixup!-Commits
✔ feature/sub: 2 Commits
✔ Docstring in sub
✔ kein verwaister Worktree
✔ kein laufender Bisect
Challenge
Rename the commit feat: div on feature/sub
to feat: div (ohne Nullprüfung) (“without zero check”). No
copying a hash, no interactive editor.
Hint: git commit --fixup knows the form
reword:<commit>, and GIT_EDITOR can be
any command.
Solution
git co feature/sub
GIT_EDITOR="perl -pi -e 's/^feat: div\$/feat: div (ohne Nullprüfung)/'" \
git commit -q --fixup=reword:":/feat: div"
git ll | head -2
GIT_SEQUENCE_EDITOR=: git ri main
git ll | head -3Expected (shortened):
897dc3a amend! feat: div
8eef217 feat: div
Erfolgreich Rebase ausgeführt und refs/heads/feature/sub aktualisiert.
ba61ded feat: div (ohne Nullprüfung)
4ca2126 feat: sub
6008dd4 docs: Schritt 8
--fixup=reword: creates an empty amend!
commit with a new message. Autosquash folds it in like any fixup.
Save your progress? Sign up for free
Enterprise Note
- Separate name, email and signature per context:
includeIf "gitdir:~/work/"loads a client config. - Sign commits (
gpg.format = ssh,commit.gpgSign = true) – often mandatory for protected branches. - Never force-push to shared branches; if necessary, only
git push --force-with-lease. fixup/rebaseonly on your own branches that haven’t been merged yet. Check the repo’s squash-merge policies.ghin CI viaGH_TOKENfrom the secret store, never via a personal login.