Is there an easy way to switch to a previously active branch?

I am using git (actually, msysgit) 1.6.4 on Windows. Most of the time I do some branches. From time to time, I want to return to the wizard to cherry-pick one specific commit that I made in my function branch - usually because it is a useful bugfix that makes sense even without this function. My workflow looks like this: if it is unnecessarily complicated, tell me :-):

  git checkout -b mycoolfeaturebranch
 // hack away, implementing a feature and one bugfix (while I'm at it)

 git add file_with_bugfix.cpp
 git commit -m "Fixed bug 12345 // commit the bugfix
 git checkout master // hop over to master
 git cherry-pick // bring the bugfix into master

At this point, I usually want to return to my function branch to continue working on this function. Unfortunately, my branch names tend to get a little long (like "mycoolfeaturebranch"), and I don't have the assurance of the git name tab on Windows.

Maybe something like cd - in Unix shells (which jumps to the previous directory is useful for switching between two directories)? A git checkout - it would be great. :-)

+4
source share
2 answers

From $ GIT / Documentation / RelNotes-1.6.2.txt:

  • "git checkout -" is short for "git checkout @ {- 1}".

Have you tried

+4
source

Try:

  git checkout @{-1} 

From git rev-parse :

The special construction @{-<n>} means that the first branch is checked before the current one.


As mentioned by Stefan Nรคwe in his answer :

" git checkout - " is short for " git checkout @{-1} ".

Despite the fact that the syntax @{-1} was about 1.6.2, only since 1.6.2 is it fully effective, as Junio โ€‹โ€‹S. Hamano comments back in February 2009 (my attention):

The syntax @ {- 1} was added long before you began to get hyperactive this round, but it will be in 1.6.2, and is advertised as "useful where you can use the branch name", but in fact it is not .

I corrected various places to fit reality with demand.
Now I am doing " git merge @{-1} ".


(Note: this is different from @{<n>}

A link followed by the @ suffix with an ordinal specification enclosed in a pair of brackets (for example, {1} , {15} ) to indicate the nth previous value of this ref.
For example, master@ {1} is the immediate previous value of the wizard, and master@ {5} is the fifth previous value of the wizard.
This suffix can only be used immediately after the name ref, and ref must have an existing log ($ GIT_DIR / logs /).

You can use the @ construct with the empty ref part to get the current branch in the reflog.
For example, if you are on the blabla branch, then @{1} means the same as blabla@ {1} .

)

+4
source

All Articles