Skip to content

Instantly share code, notes, and snippets.

@hunan-rostomyan
Last active March 22, 2016 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hunan-rostomyan/0552a57f2b9b8dc00fc4 to your computer and use it in GitHub Desktop.
Save hunan-rostomyan/0552a57f2b9b8dc00fc4 to your computer and use it in GitHub Desktop.
Toggle between git branches
# 0. MOTIVATION
# If you're working with two git branches, it might be convenient
# to have a way of quickly toggling between them.
# Update: Eric D. Wang has informed me of the built-in
# `git checkout -` command (analogous to the usual `cd -`)
# that provides the same functionality. Please use that
# instead.
# 1. DEFINITION
# -------------
# 1a. Add the following method to your shell rc file (e.g. ~/.zshrc)
function gitback() {
git checkout $(git reflog | awk '/checkout/ {print $6}' | sed -n '1p')
}
# 1b. Source the rc file (replace '~/.zshrc' with yours)
$ . ~/.zshrc
# USAGE
# -----
# Suppose you're on `master`. Check out `feature-branch`:
$ git checkout feature-branch
(feature-branch) $ ...
# Get back to the last branch:
$ gitback
(master) $ ..
# Wanna go back to `feature-branch`?
$ gitback
(feature-branch) $ ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment