cs.oldnet

All sheets / CI/CD & Release

Git

Commands for daily work and the release/debugging tasks interviewers ask about.

Everyday

git switch -c feat/x           # new branch
git add -p                     # stage hunks interactively
git commit -m "feat: add x"
git commit --amend --no-edit   # add to last commit (not pushed)
git push -u origin feat/x
git pull --rebase
git stash push -m wip; git stash pop

Inspect

git log --oneline --graph --decorate --all
git log -p -- path/file        # history of a file
git log -S "funcName"          # commits that added/removed a string
git blame -L 10,20 file
git diff main...feat/x         # changes on branch since it diverged
git show HEAD~1
git reflog                     # find "lost" commits

Merge vs rebase

mergerebase
Historypreserves, adds merge commitlinear, rewrites commits
Safe on shared branchesyesno (never rebase public history)
Useintegrate PRsupdate feature branch on main

git merge --squash / "Squash and merge" collapses a branch into one commit.

Undo

GoalCommand
Unstagegit restore --staged file
Discard local editsgit restore file
Undo last commit, keep changesgit reset --soft HEAD~1
Drop last commit entirelygit reset --hard HEAD~1
Undo a pushed commit safelygit revert <sha>
Recover after bad resetgit reflog then git reset --hard <sha>

Release tasks

git tag -a v1.4.0 -m "v1.4.0"; git push origin v1.4.0
git cherry-pick <sha>              # hotfix onto release branch
git switch -c release/1.4 v1.4.0
git describe --tags                # nearest tag
git log v1.3.0..v1.4.0 --oneline   # changelog range

Find a regression

git bisect start
git bisect bad                 # current is broken
git bisect good v1.3.0         # last known good
# test, then mark: git bisect good | bad
git bisect run npm test        # automate
git bisect reset

Interactive rebase

git rebase -i HEAD~5    # pick / reword / squash / fixup / drop / edit
git commit --fixup <sha>; git rebase -i --autosquash main
git push --force-with-lease   # safer than --force

Concepts

  • Commit = snapshot + parent pointer(s); branch = movable pointer; HEAD = current pointer.
  • Objects: blob (file), tree (dir), commit, tag. Content-addressed by SHA.
  • Detached HEAD: checked out a commit, not a branch; create a branch to keep work.
  • fetch downloads; pull = fetch + merge (or rebase).