Skip to content

Team git bash shortcuts

Jeremy Elbourn edited this page Oct 17, 2016 · 9 revisions

Team git / bash shortcuts

This is a collection of team members' git and bash shortcuts that other may (or may not) find useful.

@jelbourn's .gitconfig aliases

Replace jelbourn with the name of your github fork. Assumes you have an https remote for upstream called upstream-http.

[alias]
	co = checkout
	br = branch
	df = diff
	dif = diff
	ca = commit -a
	ri = git rebase -i upstream/master
	added = diff --cached --name-only
	cam = commit -a --amend --no-edit
	br-name = "!git rev-parse --abbrev-ref HEAD"
	url = "!f() { URL=$(git config --get remote.upstream-http.url); echo ${URL/.git/}; }; f"
  sync = !git fetch upstream && git rebase upstream/master
  export = "!sh ~/scripts/github-pr-export.sh"
  submit = "!git push upstream $(git br-name):master"
  patch = "!f() { URL=$(git config --get remote.upstream-http.url); curl -L ${URL/.git//pull/$1.patch} | git am; }; f"

@jelbourn's git export script:

(assumed you've set up your remote to fetch pull requests)

#!/usr/bin/env bash

git push -f jelbourn $(git br-name)
git fetch upstream refs/pull/*/head:refs/remotes/upstream/pr/* > /dev/null
SHA=$(git rev-parse HEAD)
PR_BRANCH=$(git branch -ra --contains ${SHA} | grep 'pr/')
if [ "${PR_BRANCH}" = "" ]; then
  echo 'Create PR:'
  echo $(git url)/compare/master...jelbourn:$(git br-name)?expand=1
else
  PR_NUMBER=$(echo ${PR_BRANCH} | cut -d'/' -f4)
  PR_URL=$(git url)/pull/${PR_NUMBER}
  echo "Exported to ${PR_URL}"
fi

Pull request merge function

Bash function to patch a pull request into your local. Assumes the main repo is upstream. Originally written by @pkozlowski-opensource, modified by @jelbourn

# Merge locally a pull request from GitHub; amends the commit message to include PR number
# Usage: `ghpr 1234`
function ghpr() {
    pr=${1:?"Pull request number is mandatory"}
    remote_url=$(git config --get remote.upstream.url)
    pr_patch_url=${remote_url/.git//pull/$1.patch}
    echo ${pr_patch_url}
    curl -kL ${pr_patch_url} | git am -3 && git commit --amend -m "$(git show --format=%B HEAD -s)

Closes #${pr}"
}
Clone this wiki locally