Skip to content

Instantly share code, notes, and snippets.

@j-po
Last active August 7, 2022 09:55
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j-po/732875e5f4b925e0bfff to your computer and use it in GitHub Desktop.
Save j-po/732875e5f4b925e0bfff to your computer and use it in GitHub Desktop.
`pushd` and `popd`-style commands for git branches (in the place of directories)
# `pushd` and `popd`-style commands for git branches (in the place of directories)
# USAGE: `pushb <branch>; SOME_WORK; popb`
GITSTACK=() # stack of branches–just an array that we add to and access from the tail
GITSTASHCK=() # stack of stashes
function pushb {
GITSTACK+=`git symbolic-ref --short HEAD`
GITSTASHCK+=`git stash create`
git reset --hard # `git stash create` doesn't do this the way `git stash` normally does
git checkout $1
echo ${GITSTACK[@]}
}
function popb {
git checkout ${GITSTACK[-1]}
unset GITSTACK[-1]
git stash apply ${GITSTASHCK[-1]}
unset ${GITSTASHCK[-1]}
echo ${GITSTACK[@]}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment