spoike
12/7/2016 - 2:06 PM

Git Checkout that remembers previous branch and lets you quickly switch between previous and current branches.

Git Checkout that remembers previous branch and lets you quickly switch between previous and current branches.

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,}
}