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 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,} | |
} |
Btw you can switch between branches with git checkout -
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Script is originally by skensell from this StackOverflow answer but has been modified to cache previous branch per-project under the project's
.git
folder in aPREVIOUS_HEAD
file. It also works from any subfolder in the project.