Created
January 27, 2018 05:59
-
-
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
This file contains 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
#! /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" |
This file contains 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
#! /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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Read background and usage: https://magicsnippet.wordpress.com/2018/01/27/git-checkout-a-branch-with-a-dirty-workspace/