Irbisa · Cheatsheet
Git & Team Workflow
Merge vs rebase, conflicts, reset and revert, cherry-pick, stash, reflog and bisect, branching strategies, pull requests and code review, commit conventions, secrets in history, go.mod and go.sum
What are a commit, a branch and
HEADin Git, and what does "detached HEAD" mean?What is the difference between
git fetchandgit pull, and what doesgit pull --rebasechange?git fetch origingit log --oneline main..origin/main # what others pushedgit log --oneline origin/main..main # what only you have git pull --rebase # fetch, then replay your commits on topgit config --global pull.rebase true # the default from now onWhat is the difference between merge and rebase, and why should you never rebase a branch other people work on?
# feature/cart was cut from main, and main has moved sincegit switch feature/cartgit rebase main # your commits replayed on top: new hashesgit push --force-with-lease # only because the branch is yours alone git switch main…How do you resolve a merge conflict step by step, and what must you never do?
git merge main# CONFLICT (content): Merge conflict in cart/cart.gogit status --short # UU cart/cart.gogit log --merge --oneline -- cart/cart.go # who changed it on each side # edit it: keep both intentions, delete <<<<<<< ||||||| ======= >>>>>>>…What do
git stashandgit cherry-pickdo, and what are the traps of each?How does
.gitignorework, and why does Git keep tracking a file after you add it there?echo "coverage.out" >> .gitignoregit status --short # M coverage.out: the pattern changes nothing git rm --cached coverage.outgit add .gitignoregit commit -m "chore: stop tracking coverage.out"…After a merge,
go.modhas conflict markers in itsrequireblock. How do you resolve it, and why is "take one side, thengo mod tidy" wrong?# on main, after `git merge feature/sync`: require block resolved by handrequire ( github.com/google/go-cmp v0.7.0 // from main github.com/google/uuid v1.6.0 // both sides golang.org/x/sync v0.10.0 // from feature/sync)…Each command below runs on its own from the same pushed history. What do
git logandgit statusshow after each, and which is safe on a pushed branch?# main, already pushed:# c3 Add retry <- HEAD# b2 Add client# a1 Init# c3 changed client/client.go; the working tree is clean. git reset --soft HEAD~1 # Agit reset HEAD~1 # B (--mixed is the default)git reset --hard HEAD~1 # Cgit revert HEAD # DHow do you clean up your branch with an interactive rebase before merging, and why push with
--force-with-leaseinstead of--force?git fetch origingit rebase -i --autosquash origin/main # the todo list Git opens, after your edits# (--autosquash already moved the fixup! commit under its target):pick 3f9c2e1 # feat: cart badge…After a
git reset --hardor a rebase gone wrong, your commits seem lost. How do you get them back, and when can you not?$ git reset --hard HEAD~2 # oops: two commits "gone"$ git refloga872130 HEAD@{0}: reset: moving to HEAD~2d6eb664 HEAD@{1}: commit: Debounce search7a7b367 HEAD@{2}: commit: Add search fielda872130 HEAD@{3}: checkout: moving from main to feature/search…How does
git bisectfind the commit that broke something, and how do you automate it withgit bisect run?git bisect startgit bisect bad # HEAD: Total is wronggit bisect good v1.4.0 # the last release where it was right# Bisecting: 5 revisions left to test after this (roughly 3 steps) # cart/total_regression_test.go is new and untracked…What makes a good commit message, and how do Conventional Commits turn the history into version numbers and a changelog?
feat(cart): show a discount badge on eligible items The badge reads the rule from /v2/discounts instead of theclient-side list, so marketing can change rules without a release. Closes #418…What makes a good pull request, and how do you review someone else's?
What is the difference between GitFlow, GitHub flow and trunk-based development, and which would you choose?
What belongs in a pre-commit hook, and why must CI run the same checks anyway?
# lefthook.yml: committed; `lefthook install` wires .git/hooks after a clonepre-commit: commands: gofmt: glob: "*.go" run: gofmt -w {staged_files}…You committed a secret and pushed it. What do you do, and why is deleting it in a new commit not enough?
# 0) Revoke the secret at the provider and ship the new one. Only then:git clone git@github.com:acme/shop.git shop-clean && cd shop-clean # drop the file from every commitgit filter-repo --sensitive-data-removal --invert-paths --path .env# or replace the value wherever it appears ("old==>new" per line):…PR #42 broke checkout, so its merge was reverted on
main. The author fixed the branch and merged it again, yetmainstill has no discounts. Why?git switch maingit merge --no-ff feature/discounts # PR #42: two feature commitsgit revert -m 1 HEAD # checkout broke: undo the mergegit push git switch feature/discounts# add a guard in cart/cart.go, away from the lines the feature changedgit commit -am "fix: skip checkout for an empty cart"git switch maingit merge feature/discounts # merges cleanlyHow does a Git tag become a Go module version, and what breaks if you move or delete a tag that was already published?
# release: the tag is the versiongit tag v1.4.0 && git push origin v1.4.0GOPROXY=proxy.golang.org go list -m example.com/lib@v1.4.0 # the proxy caches it # v1.4.0 has a bug: never move the tag; ship v1.4.1 that retracts it# go.mod of v1.4.1:…