Skip to content

Instantly share code, notes, and snippets.

@stilist
Last active October 13, 2015 12:58
Show Gist options
  • Save stilist/4199738 to your computer and use it in GitHub Desktop.
Save stilist/4199738 to your computer and use it in GitHub Desktop.
Natural-language git commands
# [...]
function parse_git_branch () {
local git_status="`git status -unormal 2>&1`"
# fatal: Not a git repository (or any of the parent directories): .git
if [[ $git_status == fatal* ]] ; then
echo ""
# On branch master
#
# Initial commit
#
# Untracked files:
# […]
elif [[ $git_status == *Initial* ]] ; then
echo "[new repo]"
else
# http://stackoverflow.com/a/11958481/672403
git rev-parse --symbolic-full-name --abbrev-ref HEAD
fi
}
# Automatically select appropriate git remote for push.
#
# Assumes branches are named with git-flow semantics, and that development
# work is submitted through pull requests: if there’s a remote named
# `"upstream", and `$branch` starts with `develop` or `release/`, `$remote`
# will be `"upstream"`. If the remote doesn’t exist, or the branch has any
# other name, `$remote` is `"origin"`.
function autopush () {
local branch="$(parse_git_branch)"
local remote="origin"
# `$branch` will be `""` if not in a repo
if [ ! "$branch" ] ; then
# send to stderr
echo "You’re not in a git repository" >&2
return 1
fi
# check for remote named `"upstream"`
git remote | grep upstream > /dev/null
# upstream exists
if [ "$?" -eq "0" ] ; then :
# check if branch is named `"develop"` or starts with `"release/"`
if [[ "$branch" = "develop" || "$branch" =~ ^release\/.+$ ]] ; then
remote="upstream"
fi
fi
echo "Pushing $branch to $remote"
git push $remote $branch
}
# `git push`
function push () {
# `push to h_prod` (uses current branch)
if [ "$1" = "to" ] ; then :
local branch="$(parse_git_branch)"
local remote=$2
# `push master to origin`
# `push production:master to origin` (for cross-branch push)
else
local branch=$1
local remote=$3
fi
if [ -n "$remote" ] ; then :
git push $remote $branch
fi
}
# `git push h_prod master`
# `git push h_prod bar:master`
function deploy () {
# `deploy to h_prod` (uses current branch)
if [ "$1" = "to" ] ; then :
local branch="$(parse_git_branch)"
local remote=$2
else
# `deploy foo to h_prod`
if [ "$2" = "to" ] ; then :
local branch=$1
local remote=$3
fi
fi
if [ -n "$remote" ] ; then :
git push $remote $branch:master
fi
}
# `git pull`
function pull () {
# `pull from h_prod` (uses current branch)
if [ "$1" = "from" ] ; then :
local branch="$(parse_git_branch)"
local remote=$2
# `pull master from origin`
else
local branch=$1
local remote=$3
fi
if [ -n "$remote" ] ; then :
git pull $remote $branch
fi
}
# `git checkout`
function switch () {
# `switch to master` (uses current branch)
if [ "$1" = "to" ] ; then :
git checkout $2
fi
}
# create branch if it doesn’t exist, and automatically switch to it (unlike `git branch some_new_branch`)
# `git checkout` / `git checkout -b`
function switch_f () {
# http://stackoverflow.com/q/5167957/672403
git show-ref --verify --quiet "refs/heads/$1"
if [ "$?" -eq "0" ] ; then :
git checkout $1
else
git checkout -b $1
fi
}
# [...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment