I’ll be honest. I’ve been around tech for quite some time now – 25 years since really starting playing with coding and running services, and 16 years of that in paid employment. Sometimes I need to actually stay up-to-date and I’m not always the best at that! I’ve recently been coming back to Python after a while away doing mostly Rails and frontend work at Citizens Advice, and things have changed a lot! Everything’s typed now (yay! honestly when I started using TypeScript I realised how much I loved types), and everything is FastAPI nowadays, not Flask.
But when I spotted my wife using git switch -c to make a new branch locally (vs my git checkout -b) I realised that Git’s probably changed a bit too. And indeed it has! But a lot of the nice new things aren’t actually defaults (at least until Git 3.0), so I’ve made a little Gist which sets some best practices for my Git config, which I can run when I provision a new machine (some of these I already had, I’ve just put them together).
# --- Branching, Pushing & Pulling ---
git config --global init.defaultBranch main
git config --global checkout.defaultRemote origin
git config --global push.autoSetupRemote true
git config --global pull.rebase true
git config --global fetch.prune true
git config --global branch.sort -committerdate
git config --global tag.sort version:refname
# --- Rebasing & Conflict Resolution ---
git config --global rebase.updateRefs true
git config --global rebase.autoSquash true
git config --global rebase.autoStash true
git config --global rerere.enabled true
git config --global rerere.autoUpdate true
git config --global merge.conflictstyle zdiff3
# --- Committing ---
git config --global commit.verbose true
# --- Cleaner Diffs ---
git config --global diff.algorithm histogram
git config --global diff.colorMoved default
git config --global diff.colorMovedWS allow-indentation-change
# --- Branch Cleanup Alias (macOS & Linux safe) ---
git config --global alias.cleanup '!git for-each-ref --merged="$(git rev-parse -q --verify origin/HEAD || echo HEAD)" --format="%(if)%(HEAD)%(then)%(else)%(refname:short)%(end)" refs/heads | grep -vxE "(develop|main|master|)" | while IFS= read -r b; do git branch -d "$b"; done'
Annoyingly one thing Git still doesn’t have is a nice way to remove local branches that have been merged, which is what my git cleanup alias does (on Mac for work, and in WSL at home), combined with the fetch.prune setting which removes references to removed remote branches. But let’s go through the others in case, like me, you learnt Git once when you migrated from SVN, got your head around its graph structure, and have missed more recent things go by.
The other things the script does are set main to be the default branch name (matching GitHub), and sets origin as the remote to use (in case you have multiple) if you’re switching to a local branch which exists on multiple remotes. autoSetupRemote means a push on a new branch automatically goes to origin with the matching branch name without having to be explicit or use -u on first push, and pull.rebase sets the default for a pull to be a rebase (this is my preferred workflow, I find lots of merge commits in history messy) whilst rebase.updateRefs means it then sorts out branches stacked on the one you’re rebasing. rebase.autoStash also stashes when pulling if there are uncommitted changes. The rerere feature works so that if you’re resolving a conflict, and the same conflict happens multiple times, then it takes how you resolved it and automatically applies and stages that during a merge or rebase if the same conflict happens again.
It makes git branch list your most recent one first, and applies a natural sort (so v1.9 then v1.10) to tag lists, as well as switching to better algorithms both for showing merge conflicts (merge.conflictstyle = zdiff3 shows a common ancestor) and the diff settings produces more readable diffs with reindentations of blocks of code.
Another nice new command line tool (for me to learn alongside git switch) is git commit --fixup <sha> if you want to go back and fix a typo at PR review time in a doc that’s a few commits back and have a nice clean set of commits that are easier to review in future. You still have to rebase it on by running git rebase -i main with rebase.autoSquash adding it in for you when you do. commit.verbose also shows the actual diff in your editor when writing your commit message which is nice to have all on one screen.
I’ve only been using this for a day, but it’s so much nicer! Hopefully it helps you too.