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....

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....

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:...