Jay Taylor's notes
back to listing indexIs there any way to git checkout previous branch?
[web search]- Home
-
- Public
- Stack Overflow
- Tags
- Users
- Jobs
-
-
Teams
-
Q&A for Work
-
I sort of want the equivalent of cd -
for git. If I am in branch master
and I checkout foo
, I would love to be able to type something like git checkout -
to go back to master
, and be able to type it again to return to foo
.
Does anything like this exist? Would it be hard to implement?
gc-
or was that shorthand for git checkout -
– jewbix.cube
Mar 5 '18 at 23:31
From the release notes for 1.6.2
@{-1}
is a way to refer to the last branch you were on. This is
accepted not only where an object name is expected, but anywhere a branch name is expected and acts as if you typed the branch name.
E.g.git branch --track mybranch @{-1}
,git merge @{-1}
, and
git rev-parse --symbolic-full-name @{-1}
would work as expected.
and
git checkout -
is a shorthand forgit checkout @{-1}
.
error: pathspec '@-' did not match any file(s) known to git. error: pathspec '@1' did not match any file(s) known to git.
– Murphy Randle
Feb 7 '14 at 23:12
@{u}
, which is the upstream branch of the current branch. It's very useful for e.g. git log @{u}..
which lists upstream commits that aren't pulled yet. And the converse git log ..@{u}
which is just the local commits that aren't pushed yet.
– Benjohn
Nov 23 '16 at 9:03
The simplest way of doing this nowadays is:
git checkout -
... which is an alias of:
git checkout @{-1}
for i in{1..10}; do git rev-parse --symbolic-full-name @{-$i}; done
– Bonsaigin
Sep 20 '17 at 14:50
git reflog | ? { $_ -match ': checkout: moving from (.*) to (.*)'} | % { $Matches[1] }
. In bash you have to write something equivalent (get reflog and filter it to lines containing ": checkout:" string and then extract branch name). This is actually what git does
– Mariusz Pawelski
Nov 4 '18 at 0:46
As @Karl points out and from git checkout
manual:
As a special case, the "@{-N}" syntax for the N-th last branch checks out the branch (instead of detaching). You may also specify - which is synonymous with "@{-1}".
So both git checkout -
and git checkout @{-1}
would work in this case
Closest I believe is using the git reflog
and parse the latest moving from branch1 to branch2
and git checkout branch1
I landed to this question with the same thought to checkout my previous branch. I'm using ohmyz in Mac
. Below command helped me.
$ gco -
$ git checkout -
gco
as a short way of writing git checkout
. So gco -
just calls git checkout -
.
– Rory O'Kane
Jan 16 at 23:12
git checkout -
. for failsafe updated.
– Venkat.R
Jan 17 at 2:05
Here are pointers to the parts of Git’s documentation that describe the git checkout -
and git checkout @{-1}
solutions given by the other answers:
When specifying a Git revision for any command,
@{-<n>}
, e.g.@{-1}
means “the nth branch/commit checked out before the current one.” The documentation forgit checkout <branch>
reiterates: “You can use the@{-N}
syntax to refer to the N-th last branch/commit checked out usinggit checkout
operation.”For the
<branch>
argument ofgit checkout
, “you may also specify ‘-
’ which is synonymous to ‘@{-1}
’.”
site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required. rev 2019.7.9.34258