Post

Git functions and aliases

Shell functions

zshrc file on GitHub

Branch cleanup

This function will remove all local branches that don’t exist on the remote. I work with GitHub repos that automatically delete branches once a PR is merged, so I use it to keep my local branches list clean.

1
2
3
4
5
function git-cleanup {
  git fetch -p && git branch -vv |\
    awk '/: gone]/{print $1}' |\
    xargs git branch -D
}

Identity management

Set up a git repository to use Sigstore gitsign and company identity store for work repositories.

1
2
3
4
5
6
7
8
# Config git repo for work
function setup_gitsign () {
  git config --local commit.gpgsign true
  git config --local tag.gpgsign true
  git config --local gpg.x509.program gitsign
  git config --local gpg.format x509
  git config --local gitsign.connectorID https://accounts.google.com
}

Set up a git repository to use SSH commit signing using the specified key.

1
2
3
4
5
6
7
8
# Config git repo for personal
function setup_gitssh () {
  git config --local user.signingKey = /Users/natalie/.ssh/github-signing.pub
  git config --local commit.gpgsign true
  git config --local tag.gpgsign true
  git config --local gpg.x509.program ssh
  git config --local gpg.format ssh
}

Nuke it from orbit

Reset the working directory to the last commit, remove any untracked files, and abort any rebase in progress. (source)

1
2
3
4
5
6
7
function git-nah () {
  git reset --hard
  git clean -df
  if [ -d ".git/rebase-apply" ] || [ -d ".git/rebase-merge" ]; then
    git rebase --abort
  fi
}

One-liners

List all branches not tracked with a remote branch.

1
git branch -vv | cut -c 3- | awk '$3 !~/\[/ { print $1 }'

Print a list of files in a repo by number of commits touching them, useful for finding the most changed files.

1
git log --pretty=format: --name-only | sort | uniq -c | sort

Print number of commits per day, sorted by date.

1
git log --date=short --pretty=format:%ad | sort | uniq -c

Git aliases

gitconfig files on GitHub

List branches

List all the branches

1
br = branch --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(contents:subject) %(color:green)(%(committerdate:relative)) [%(authorname)]' --sort=-committerdate

Here’s what it looks like in practice:

git-br

History, but pretty

Show the history of the current branch, but pretty

1
graph = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %Cblue%cn%Creset committed %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative

Here’s what it looks like in practice:

git-graph

This post is licensed under CC BY 4.0 by the author.