Skip to content

Instantly share code, notes, and snippets.

@jonathantneal
Last active August 29, 2015 14:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathantneal/78bc58adb2b2f0691770 to your computer and use it in GitHub Desktop.
Save jonathantneal/78bc58adb2b2f0691770 to your computer and use it in GitHub Desktop.
Handy GIT ZSH shortcuts

Handy GIT ZSH shortcuts

Here are some handy git shortcuts I use with oh-my-zsh to both speed up and protect my workflow.

All merges are handled without a fast-forward. All merges use the default messaging. Whenever possible, autocompletion is provided.

gmto

Merges the current branch into another branch and prompts you to automatically push changes.

gmto [branch]

gmfr

Merges another branch into the current branch and prompts you to automatically push changes.

gmfr [branch]

gar

Archives the current branch and prompts you to automatically push changes.

gar

gsre

Rebases the svn branch, prompts you to merge it into master, and then prompts you to automatically push changes.

gsre
# return whether directory is a git repository
function _dir_is_git {
if [ -d .git ] || git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
return 0
fi
return 1
}
# print current branch
function _git_current_branch {
if _dir_is_git; then
echo $(git symbolic-ref HEAD | sed -e 's,refs/heads/\(.*\),\1,')
return 0
fi
return 1
}
# merge current branch into $1 branch
function gmto {
# require branches
if ! _dir_is_git || [ -z "$@" ]; then
echo "Usage: gmto [branch]"
return 1
fi
# set branch to merge from as current
from=$(_git_current_branch)
# set branch to merge into
to=$1
# merge branch
echo "Merging '$from' into '$to'"
{
# checkout and pull $1 branch
git checkout $to
git pull origin $to
# merge current branch into $1 branch
git merge --no-edit --no-ff $from
} &> /dev/null
# print status
git status
# confirm push
read "R?Would you like to push any changes to origin? [y/n] "
echo
if [[ "$R" =~ ^[Yy] ]]; then
# push changes
git push origin $to
fi
return 0
}
compdef _git gmto=git-merge
# merge $1 branch into current branch
function gmfr {
# require branches
if ! _dir_is_git || [ -z "$@" ]; then
echo "Usage: gmfr [branch]"
return 1
fi
# set branch to merge to as current
to=$(_git_current_branch)
# set branch to merge from
from=$1
echo "Merging '$from' into '$to'"
{
# pull current branch
git pull origin $to
# merge $1 branch into current branch
git merge --no-edit --no-ff $from
} &> /dev/null
# print status
git status
# confirm push
read "R?Would you like to push any changes to origin? [y/n] "
echo
if [[ "$R" =~ ^[Yy] ]]; then
# push changes
git push origin $to
fi
}
compdef _git gmto=git-merge
# archive current branch
function gar {
# require branch
if ! _dir_is_git; then
echo "Usage: gar"
return 1
fi
# set branch as current
branch=$(_git_current_branch)
# confirm archival
read "R?Would you like to archive '$branch'? [y/n] "
echo
if [[ "$R" =~ ^[Yy] ]]; then
# checkout and pull master
git checkout master
git pull origin master
# tag create archive branch from original branch
git tag archive/$branch $branch
# delete original branch
git branch -d $branch
# print status
git status
# confirm push
read "R?Would you like to push any changes to origin? [y/n] "
echo
if [[ "$R" =~ ^[Yy] ]]; then
# push changes
git push origin archive/$branch
git push origin --delete $branch
fi
fi
}
# rebase svn branch
function gsre {
# require branch
if ! _dir_is_git; then
echo "Usage: gsre"
return 1
fi
# confirm rebase
read "R?Would you like to rebase 'svn'? [y/n] "
echo
if [[ "$R" =~ ^[Yy] ]]; then
echo "Rebasing 'svn'..."
{
# checkout and rebase svn
git checkout svn
git svn rebase
} &> /dev/null
# print status
git status
# confirm merge
read -q "R?Would you like to merge 'svn' to 'master'? [y/n] "
echo
if [[ $R =~ ^[Yy]$ ]]; then
echo "Merging 'svn' to 'master'..."
{
# checkout and pull master
git checkout master
git pull origin master
# merge svn
git merge svn
} &> /dev/null
# print status
git status
# confirm push
read -q "R?Would you like to push any changes to origin? [y/n] "
echo
if [[ $R =~ ^[Yy]$ ]]; then
# push changes
git push origin master
fi
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment