Lab Notebook

Better Git Commit Messages

Are you tired of your git commit history looking like this:

img

That's why (A)I came up with wonderful git push script. Instead of pushing the same word over and over for your commit, you can have a little story unfold every time you push a change.

function gp {
  local branchName=$(git branch 2>/dev/null | grep '^*' | colrm 1 2)

  if [ "$branchName" = "master" ] || [ "$branchName" = "main" ]; then
    echo "Don't push to master/main"
    return
  fi

  # Load story from ~/.storygit, or seed a default one
  local story_file=~/.storygit
  if [ ! -f "$story_file" ]; then
    echo "Once upon a time in a land far far away there lived a brave developer who pushed code every single day through rain and snow and bugs galore they never stopped exploring more the functions grew the tests all passed and every deploy was built to last so commit by commit the tale was spun a never ending story of code well done" > "$story_file"
  fi
  local story=$(cat "$story_file")
  local total=$(echo "$story" | wc -w | tr -d ' ')

  # Detect base branch
  local base_branch="master"
  git show-ref --verify --quiet refs/heads/main && base_branch="main"

  # Dynamically count commits on this branch since it diverged from base
  local count=$(git rev-list --count "${base_branch}"..HEAD 2>/dev/null || echo 0)

  # Pick the next word (wraps around if story is shorter than commit count)
  local index=$(( (count % total) + 1 ))
  local word=$(echo "$story" | tr -s ' ' '\n' | sed -n "${index}p")

  git commit -am "$word"
  echo "📖 word ${index}/${total}: \"$word\""
  git push --set-upstream origin "${branchName}"
}