Last active
March 22, 2016 16:40
-
-
Save hunan-rostomyan/0552a57f2b9b8dc00fc4 to your computer and use it in GitHub Desktop.
Toggle between git 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
# 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