/Git/ting A Workflow

This isn’t a post about how your git workflow should be. Everyone’s is different and to try and force one on someone may actually make them less productive.

Here are just some simple tools and scripts I use to make working with git simple.


Git Hooks

.git/hooks/commit-msg - Useful if you use Jira and get tried of manually typing the task name in every commit message. This only works if you name your branches CS-32-something where CS is the project shortname and 32 is the task number.

#!/usr/bin/env ruby

# The commit-msg hook takes one parameter which is the path to the temp
# file that contains the commit message.
# See https://git-scm.com/book/gr/v2/Customizing-Git-Git-Hooks
commit_msg_file = ARGV[0]

# Read in current commit msg
message = File.read(commit_msg_file)
exit 1 if message.empty?

# Get branch name
branch = `git symbolic-ref --short HEAD`.strip

# If branch name follows JIRA naming patterns, prepend it to the commit msg
matches = branch.scan(/(\w{1,}\-[0-9]{1,})\-/)
exit 0 unless matches.any?

# Write out the new message
message = "#{matches[0][0]} #{message}"
File.write(commit_msg_file, message)

.git/hooks/post-checkout - Pulls in the latest from origin as you checkout branches.

#!/usr/bin/env ruby

# pull branch in
branch = `git symbolic-ref --short HEAD`.strip
puts "Pullling in #{branch}"
`git pull origin #{branch}`

puts "Done!"

Git Config Aliases

    # Prints current branches name
    name = rev-parse --abbrev-ref HEAD

    # Copies current branches name to clipboard on mac
    cpb = !printf $(git name) | pbcopy

    # Prints out the current branches parent branch
    parent = !git show-branch \
        | grep '*' \
        | grep -v "$(git name)" \
        | head -n1 \
        | gsed 's/.*\\[\\(.*\\)\\].*/\\1/' \
        | gsed 's/[\\^~].*//'

    # Displays your all of your commit messages for the current
    # branches against its parent branch
    blast = !git log \
        --pretty=format:'%<|(10)%C(yellow)%h%<(14)%C(green)%ar%Creset%s' \
        --author="$(git config --local --get user.email)" \
        --no-merges "$(git parent)"..HEAD

    # Displays your last 10 commit messages for the current
    # branches against its parent branch
    blast10 = !git log \
        --pretty=format:'%<|(10)%C(yellow)%h%<(14)%C(green)%ar%Creset%s' -10 \
        --author="$(git config --local --get user.email)" \
        --no-merges "$(git parent)"..HEAD