Some custom git commands that I use

July 30, 2015, revised December 4, 2015 git shell

Git has two internal layers of commands - the so-called “plumbing” and “porcelain” commands. The former are low-level structural management commands which we don’t typically use, and the latter form the core of the user interface, such as “push”, “pull”, and so forth.

It only makes sense to develop a third, personal, layer of git commands to help in your daily routine - call them “towel” or “toothbrush” commands, if you like.

You can configure custom git commands through git aliases, but I find it more convenient to set them up as shell scripts: to create git customcommand, put a git-customcommand shell script somewhere on your $PATH and make it executable. For example, I put such scripts into ~/bin.

git workon

When you are going to work on a branch that already exists on the upstream, save yourself some typing by setting the upstream.

Usage:

git workon feature/feature-branch

Source code:

#!/bin/bash
git fetch origin $1:$1
git checkout $1
git auto-upstream

git pr

Open a Github compare view between the current branch and the main branch (dev, if it exists, otherwise master). From there you can create a pull request or open the currently open pull request.

Usage:

git pr

Source code (fixed 2015-12-04):

#!/bin/bash
# Call with `git pr` to open a compare view for the current branch and a pull request from that
# Depends on https://hub.github.com/

git rev-parse --verify dev >/dev/null 2>/dev/null

if [ $? -eq 0 ]
then
  BASE_BRANCH="dev"
else
  BASE_BRANCH="master"
fi

hub compare $BASE_BRANCH...`git rev-parse --abbrev-ref HEAD | sed 's/\//%2F/g'`

Depends on hub - a suite of Github-related git commands, which I highly recommend.

git cleanup

Delete all branches that are already merged into the current one. I run it from time to time on the master branch for housekeeping.

Usage:

git cleanup

Source code:

#!/bin/bash
git branch --merged | grep -v \* | grep -v master | xargs git branch -D

git timesheet

Produce a timestamped list of your commits for accounting purposes.

Usage:

git timesheet --since 2015-07-01

Source code: git-timesheet on Github

Buy Me a Coffee at ko-fi.com