sebastiandaschner blog


Custom Git subcommands

#git #commandline friday, april 20, 2018

Heavy users of Git often find themselves writing their own Git shortcuts or scripts which combine multiple Git commands for frequently used features. The possibilities to shortcut your way around Git include Git aliases, shell aliases, or custom scripts that reside in your $PATH.

For the latter, there is an interesting feature in the Git command line that I just recently discovered: Git automatically resolved subcommands from executables in the PATH that follow the naming convention git-<subcmd>. These subcommands can be executed with git <subcmd>.

For my own projects, I wrote a script git-update which does a commit on all files with default or custom message, rebase-pull, and push:

#!/bin/zsh
set -eu

message=${1:-updated}
branch=$(git symbolic-ref -q HEAD | sed -e 's|^refs/heads/||')

pushd $(git rev-parse --show-toplevel || echo ".")
    git add --all .
    git commit -m "${message}" || true
    git pull --rebase origin ${branch} || true
    git push origin ${branch}
popd

The executable file git-update resides in my $PATH and can be called with $> git update [commit-message]. No other configuration was required.

This post was reposted from my newsletter issue 022.

 

Found the post useful? Subscribe to my newsletter for more free content, tips and tricks on IT & Java: