Matt Briggs
Matt Briggs

Reputation: 42158

Is there any way to git checkout previous branch?

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?

Upvotes: 998

Views: 147345

Answers (10)

Meet Bhalodiya
Meet Bhalodiya

Reputation: 906

See git chekout and git switch have different purpose. Its more like typescript and javascript. See javascript is a part of typescript similarly git switch has some of the capabilities of git checkout.

git checkout is majorly used for switch between commit and travelling in timeline. It can also do all the things which git switch can.

git switch was specifically made for switch between branches and do all the work related to the branches.

So better to use

git switch -

Upvotes: 0

Karl Bielefeldt
Karl Bielefeldt

Reputation: 48998

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 for git checkout @{-1}.

To see the list of previous checkouts:

i=0; while [ $? -eq 0 ]; do i=$((i+1)); echo -n "$i. "; git rev-parse --symbolic-full-name @{-$i} 2> /dev/null; done

This Bash one-liner script is not perfect but it should work for most cases. Note that sometimes the number may skip.

Tip: You can add it to .bashrc as a function.

Upvotes: 1690

Mike
Mike

Reputation: 794

Git version 2.23 introduced the git switch command which you can use to do that (and more). Quoting the official documentation:

Switch to a specified branch. The working tree and the index are updated to match the branch. All new commits will be added to the tip of this branch.

In your specific case you can issue git switch - to go back to the branch you were previously on. You can execute the same command again to return to the first branch.

This command is less confusing and friendly-to-beginners as it addresses a common confusion that arises when using git checkout.

Upvotes: 46

bb.elene
bb.elene

Reputation: 1

in my case I had switched from master to gh-pages which cause all my components to disappear and get replaced by a file names "static" and others.

git checkout -m master helped

Upvotes: -3

Venkat.R
Venkat.R

Reputation: 7736

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 -

Upvotes: 15

Rory O'Kane
Rory O'Kane

Reputation: 30388

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 for git checkout <branch> reiterates: “You can use the @{-N} syntax to refer to the N-th last branch/commit checked out using git checkout operation.”

  • For the <branch> argument of git checkout, “you may also specify ‘-’ which is synonymous to ‘@{-1}’.”

Upvotes: 2

Jackkobec
Jackkobec

Reputation: 6705

The most popular solution is:

git checkout @{-N}

Where N - step count of the branches to move back on the checkout history.

Upvotes: 1

ddd
ddd

Reputation: 111

Just adding some more detail to the previous answers to understand the mechanism by which git checkout @{-N} works. It walks the reflog to inspect the checkout history, so if you wanted to implement something similar on your own you should be able to parse the output of git reflog looking for checkout: lines. You can check the implementation in the git source sha1_name.c, specifically the function interpret_nth_prior_checkout.

Upvotes: 11

marcgg
marcgg

Reputation: 66436

The simplest way of doing this nowadays is:

git checkout -

... which is an alias of:

git checkout @{-1}

git checkout minus

If you want to know more about this, I wrote an entire article about it here: Checkout The Previous Branch In Git.

Upvotes: 339

manojlds
manojlds

Reputation: 301037

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

Upvotes: 33

Related Questions