Software Development

Custom Git subcommands

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.

Published on Java Code Geeks with permission by Sebastian Daschner, partner at our JCG program. See the original article here: Custom Git subcommands

Opinions expressed by Java Code Geeks contributors are their own.

Sebastian Daschner

Sebastian Daschner is a self-employed Java consultant and trainer. He is the author of the book 'Architecting Modern Java EE Applications'. Sebastian is a Java Champion, Oracle Developer Champion and JavaOne Rockstar.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button