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. |