Created
December 7, 2016 14:06
-
-
Save spoike/3c84325b2dc35d18bbdf630606c7d034 to your computer and use it in GitHub Desktop.
Git Checkout that remembers previous branch and lets you quickly switch between previous and current branches.
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
function gch() { | |
local currentBranch=$(git rev-parse --abbrev-ref HEAD) | |
local previousFile="$(git rev-parse --show-toplevel)/.git/PREVIOUS_HEAD" | |
if [ -n "$1" ]; then | |
echo "$currentBranch" >> $previousFile | |
git checkout "$@" | |
else | |
if [ ! -f "$previousFile" ]; then echo >&2 "ERROR: Missing PREVIOUS_HEAD. Please run gch with 1 argument first." | |
else | |
git checkout "$(cat $previousFile | tail --lines=1)" | |
echo "$currentBranch" >> $previousFile | |
fi | |
fi | |
# truncate the file | |
tail -n10 $previousFile > "$previousFile.TEMP" | |
mv -f "$previousFile"{.TEMP,} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Btw you can switch between branches with
git checkout -
.