Created
October 5, 2016 15:27
-
-
Save wsargent/826a19e65999840c71dbacefffa32051 to your computer and use it in GitHub Desktop.
Git worktree script
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
export WORKDIR=$HOME/work | |
# Creates a working tree to check out a branch locally. | |
# This is useful when you have multiple branches on origin, | |
# and want to pull work work with them | |
function worktree() { | |
# We assume we're always under $WORKDIR somewhere. | |
# Go down the tree until we know what project we're in. | |
while [[ "$(dirname "$PWD")" != $WORKDIR ]] ; do | |
find "$PWD"/ -maxdepth 1 "$@" | |
cd .. | |
done | |
BASEDIR=$PWD | |
BRANCHNAME=$1 | |
cd $BASEDIR/master; | |
# Ensure we don't have stale trees around... | |
git worktree prune; | |
# Get fresh data from origin... | |
git fetch origin; | |
# Check for the presense of a local branch... | |
git rev-parse --verify $BRANCHNAME | |
# $? == 0 means local branch with <branch-name> exists. | |
if [[ $? == 0 ]]; then | |
# Create a worktree from the local branch. If it already exists | |
# in another worktree, then this will die with error. | |
git worktree add $BASEDIR/$BRANCHNAME $BRANCHNAME; | |
else | |
# Create a local branch with the same name as origin, then create | |
# a worktree from it. | |
git worktree add -b $BRANCHNAME $BASEDIR/$BRANCHNAME master; | |
fi | |
cd $BASEDIR/$BRANCHNAME; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment