Skip to content

Instantly share code, notes, and snippets.

@andrewle
Created July 15, 2013 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewle/6002304 to your computer and use it in GitHub Desktop.
Save andrewle/6002304 to your computer and use it in GitHub Desktop.
#!/bin/bash
function gitroot {
if [ "$(pwd)" != "$(git rev-parse --show-toplevel)" ]; then
go-to-git-root
elif [ $(cd .. && git rev-parse --is-inside-work-tree &> /dev/null; echo $?) -eq 0 ]; then
cd .. && go-to-git-root
fi
}
function go-to-git-root {
cd $(git rev-parse --show-toplevel)
}
function git-last-branch {
current_branch=$(git branch | grep -v '(no branch)' | grep "\*" |
awk '{print $2}')
# support when running this from a detached head
if [ "${current_branch}" == "" ]; then
current_branch=$(git rev-list --max-count=1 HEAD)
fi;
git reflog | \
grep -m 1 "checkout:.*to ${current_branch}$" | \
sed 's/.*from \([^ ]*\).*/\1/'
}
function git-checkout-last-branch {
git checkout $(git-last-branch)
}
function git-merge-last-branch {
git merge --no-ff $(git-last-branch)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment