Skip to content

Instantly share code, notes, and snippets.

@chronologicaldot
Created January 27, 2018 05:59
Show Gist options
  • Save chronologicaldot/f17c5edddb5e9b5a121e0f185427c61a to your computer and use it in GitHub Desktop.
Save chronologicaldot/f17c5edddb5e9b5a121e0f185427c61a to your computer and use it in GitHub Desktop.
git-switch : Bash scripts for switching between Git branches with dirty workspaces. Save path: /usr/local/bin/git-switch and /usr/local/bin/git-switch-reset
#! /bin/sh
BRCH=$1 ;
f() {
if [ -z "$(ls ./.git/refs/heads)" ] ; then
printf "ERROR: No head. Please make a commit first for a reference point.\n"
fi
BV=$(git rev-parse --abbrev-ref --verify --quiet "$BRCH")
if [ -n "$BV" ] && [ "$BRCH" != "HEAD" ] ; then
CURR=$(git rev-parse --abbrev-ref HEAD) ;
TRACK="_WS_ADDED_$CURR.txt" ;
if [ "$BRCH" = "$CURR" ] ; then
printf "Checking for $TRACK\n" ;
if [ -f "$TRACK" ] ; then
echo "Rebuilding workspace..." ;
rm "$TRACK" ;
git reset --soft HEAD^ ;
git reset HEAD -- ;
git reset --soft HEAD^ ;
git reset -- "$TRACK" ;
fi
else
printf "Creating $TRACK\n" ;
printf "1" > $TRACK ;
git add "$TRACK" ;
git commit -m "_WS_SWITCH_COMMIT_STAGED" ;
git add -A ;
printf "2" > "$TRACK" ;
git add "$TRACK" ;
git commit -m "_WS_SWITCH_COMMIT_WKSPC" ;
git checkout $BV ;
TRACKB="_WS_ADDED_$BV.txt" ;
printf "Checking for $TRACKB\n" ;
if [ -f "$TRACKB" ] ; then
echo "Rebuilding workspace..." ;
rm "$TRACKB" ;
git reset --soft HEAD^ ;
git reset HEAD -- ;
git reset --soft HEAD^ ;
git reset -- "$TRACKB" ;
fi
fi
else
echo "Given branch, $BRCH, does not exist." ;
fi
}; f
printf "Operation finished.\n"
#! /bin/sh
BRCH=$1 ;
f() {
CURR=$(git rev-parse --abbrev-ref HEAD) ;
TRACK="_WS_ADDED_$CURR.txt" ;
if [ -f "$TRACK" ] ; then
echo "Rebuilding workspace..." ;
rm "$TRACK" ;
git reset --soft HEAD^ ;
git reset HEAD -- ;
git reset --soft HEAD^ ;
git reset -- "$TRACK" ;
fi
}; f
printf "Operation finished.\n"
@chronologicaldot
Copy link
Author

chronologicaldot commented Jan 27, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment