Even Easier Git Rebasing

Rebasing your feature branch on main regularly is a good idea. I used to do it like this: # update local copy of main git fetch origin main:main # rebase on local main git rebase main This always felt a litle off because I was worried I’d typo main:main and mess it up. This works just as well and feels much better to me: # update local copy of `origin/main` git fetch # rebase on origin/main git rebase origin/main

2025-08-28

Configuring a Git pre-push hook to run unit tests

A coworker turned me onto this lovely technique the other day. You can use a git pre-push hook to run all of your Golang unit tests before pushing. To do this, make a the following file: $YOUR_REPO/.git/hooks/pre-push The file must be executable. The file’s contents should be: #!/bin/sh if ! go test ./... ; then echo echo "Rejecting commit. Unit tests failed." echo exit 1 fi Easy peasy.

2024-06-26

Using git worktrees

Have you ever been developing on a feature branch and needed to look at a separate branch on the same repo? I have. When this happens, I normally do one of two things: git stash my changes and change branches This is annoying because it’s a lot of steps. I also have to remember which stash to pop when I come back to this branch. Stage all my changes and store them in a work-in-progress commit This is better than #1 but doesn’t let you view both branches simultaneously. But it’s still annoying if you have files you haven’t staged yet and don’t want to stage yet. Both these have drawbacks. For example, they don’t let me look at both branches at the same time in my text editor. ...

2024-03-27

Using 'git commit --fixup'

I learned something new from Julia Evans today. Git has a --fixup argument when committing files which makes it easy to create fixup commits which can get automatically squashed when rebasing. This is fantastic! I’ve known about git commit --amend for years, but this allows you to fixup commits which are several commits back without manually moving a bunch of lines around while interactively rebasing… Let’s assume you have a repo which looks like shown below. 696792e9 is our “bad” commit, which we need to fixup. ...

2024-02-20

git

Branching and Checkout: Clean up local banches: git remote prune --dry-run origin git remote prune origin Update local branch without it being checked out: git fetch origin src_branch:local_branch Show which remote branch local branches are tracking: git branch -vv Display branches by last commit date: git for-each-ref --sort=-committerdate refs/heads/ Copy files/directories from another branch to current branch: git checkout source_branch -- path/to/dir/ path/to/file.txt Reset to a particular commit without losing changes: Reset to one commit past most recent: git reset HEAD^ Reset to particular commit: git reset COMMIT_ID Diffing branches: ...