Skip to content

Instantly share code, notes, and snippets.

@mearns
Last active March 22, 2023 15:44
Show Gist options
  • Save mearns/5f0304743beca93cf8c391978c805efb to your computer and use it in GitHub Desktop.
Save mearns/5f0304743beca93cf8c391978c805efb to your computer and use it in GitHub Desktop.
Git config
# This is Git's per-user configuration file.
[user]
name = Brian Mearns
email = bmearns@pagerduty.com
[core]
# Don't do paging; if you want to page, pipe the output into less (or your pager of choice).
pager = cat
# These are custom named "pretty formats" for printing information about commits, such as for `git log`.
[pretty]
bmearns-short = %C(yellow)%h %<(14)%C(black)%ar %<(22,mtrunc)%C(green)%aN %Creset%s
bmearns-medium = %C(yellow)%h %<(14)%C(black)%ar %<(22,mtrunc)%C(green)%aN %C(black)%d %Creset%s
[format]
# Set the default pretty format to use
pretty = bmearns-short
[alias]
br = branch
branch-name = rev-parse --abbrev-ref HEAD
tags = show-ref --tags
# Log only the changes on this branch since it diverged from main (or another branch if passed as first arg)
branch-log = "!f() { git log ${1:-main}..HEAD ; }; f"
# Similar to branch-log above, but this is meant to be copied and pasted, so it's just the subjects of the
# commit messages, and in chronological order from oldest to latest (reverse the normal log).
branch-summary = "!f() { git log --pretty=format:%s --reverse ${1:-main}..HEAD ; echo ; }; f"
# This does a squash of all the commits on this branch back to it's merge-base with main (or another
# branch if passed as the first arg). It uses a structured-ish format for the log message, which comes
# from the `branch-summary` command. In theory, the branch-summary could recurse into such commits
# and flatten them.
squash-branch = "!f() { \
SOURCE=${1:-main} ; \
BRANCH=$( git branch-name ) ; \
BASE=$( git merge-base HEAD ${SOURCE} ) ; \
SUMMARY=$( git branch-summary $SOURCE ) ; \
git reset --soft $BASE ; \
git commit -m \"${BRANCH} SQUASH - Squash all branch commits since branching from $SOURCE ($BASE)\n\n--SQUASH-SUMMARY\n${SUMMARY}\n--END-SQUASH-SUMMARY\n\" ; \
}; f"
co = checkout
st = status
aa = add --all
ci = commit
amend = commit --amend --no-edit
ammend = commit --amend --no-edit
llog = log --pretty=bmearns-medium
# When a new repository is created or cloned recursively copy everything from this path into the new `.git/` directory.
# This is useful for having standard hooks in all your repos.
# For an existing repo, you can do `git init` to copy files over. Not sure if it overwrites existing ones or not.
[init]
templatedir = ~/.git_template
defaultBranch = main
[push]
autoSetupRemote = true
#!/bin/bash
# Put this in ~/.git_template/hooks/prepare-commit-msg
#
# Looks for a jira ticket at the start of a branch name and if it's there, ensures the first line of
# the commit message contains it. If not, adds it at the beginning of the commit message.
#
# Based on https://gist.github.com/bartoszmajsak/1396344
# and this fork of it: https://gist.github.com/jonnyparris/0a10cc63af281de23a4fd34116fed3e6
#
# Don't forget to chmod it to executable.
#
# Customize which branches should be skipped when prepending commit message.
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(main)
fi
# Get the full branch name
BRANCH_NAME=$(git symbolic-ref --short HEAD)
# Strip anything after a first '/' in the branch name
BRANCH_PREFIX="${BRANCH_NAME%%/*}"
# Find the jira ticket in the branch name
JIRA_TICKET=$( echo "$BRANCH_NAME" | sed -E 's/^\s*(\([A-Z]+-[0-9]+\)|\[[A-Z]+-[0-9]+\]|[A-Z]+-[0-9]+).*/\1/' )
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_PREFIX$")
# Check if the ticket is already in the commit message's head
JIRA_TICKET_IN_COMMIT=$( head -n 1 "$1" | grep -c "\b$JIRA_TICKET\b" )
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $JIRA_TICKET_IN_COMMIT -ge 1 ]]; then
sed -i.bak -e "1s/^/$JIRA_TICKET /" $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment