Created
June 5, 2016 22:47
-
-
Save chx/d4e30aaf8e3d9a6fccad9e89bb8d73b8 to your computer and use it in GitHub Desktop.
Git helpers, put it in .bashrc / .zshrc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function git() { | |
# Path to the `git` binary | |
GIT="/usr/bin/git" | |
# Sanity check | |
if [ ! -f ${GIT} ] | |
then | |
echo "Error: git binary not found" >&2 | |
return 255 | |
fi | |
# Command to be executed | |
command=$1 | |
# Remove command from $@ array | |
shift 1 | |
# Check command against list of supported commands | |
case $command in | |
"cd") | |
cd $(git rev-parse --show-toplevel)/${1} | |
;; | |
"push") | |
if [ -z "$1" ] | |
then | |
$GIT push || $GIT push -u origin $($GIT rev-parse --abbrev-ref @) | |
else | |
$GIT ${command} "$@" | |
fi | |
;; | |
"reset") | |
if [ "$1" = "--hard" ] | |
then | |
$GIT add -u | |
if $GIT commit -m "Saving state before hard reset"; then | |
# only create a backup ref & perform a reset if | |
# a commit was created (otherwise there's nothing | |
# to reset!) | |
backupRef="refs/reset-backups/`date +%s`" | |
$GIT update-ref $backupRef HEAD | |
$GIT reset --hard HEAD~1 | |
echo "Created backup ref: $backupRef" | |
fi | |
else | |
$GIT ${command} "$@" | |
fi | |
;; | |
*) | |
# Execute the git binary | |
$GIT ${command} "$@" | |
;; | |
esac | |
# Return something | |
return $? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment