What does "git checkout -" do?

Viewed 5551

I saw a weird type of git checkout command.

git checkout -

What does - do here?

3 Answers

It is the same as doing cd -. So you go back to the last branch. These three sequences do the same, if you start from master:

# Way 1
git checkout mybranch
git checkout master

# Way 2 (same result)
git checkout mybranch
git checkout -

# Way 3 (same result)
git checkout mybranch
git checkout @{-1}

As chepner mentioned, you can go back to the nth previously checked out branch by using @{-N}

It switches back to the branch you were on previously. If you run it again, you’re toggled back to the first branch. A useful comparison is cd -, which as you may know takes you back to your most recent directory.

Have a look in this source

It does a checkout out to the last branch (I think).

Keenens-MacBook:testdir keenencates$ git branch mybranch
Keenens-MacBook:testdir keenencates$ ls
hello.py
Keenens-MacBook:testdir keenencates$ git checkout mybranch
Switched to branch 'mybranch'
Keenens-MacBook:testdir keenencates$ ls
hello.py
Keenens-MacBook:testdir keenencates$ git checkout -
Switched to branch 'master'
Keenens-MacBook:testdir keenencates$ 

You can view below to see my own idiocy.

I don't think it does anything.

Keenens-MacBook:sentiment-rnn keenencates$ cd testdir/
Keenens-MacBook:testdir keenencates$ ls
Keenens-MacBook:testdir keenencates$ git init
Initialized empty Git repository in /Users/keenencates/Documents/Udacity/DLND/master_projects/sentiment-rnn/testdir/.git/
Keenens-MacBook:testdir keenencates$ vim hello.py
Keenens-MacBook:testdir keenencates$ ls
hello.py
Keenens-MacBook:testdir keenencates$ git add .
Keenens-MacBook:testdir keenencates$ git commit
[master (root-commit) 2ffa8de] init
 1 file changed, 1 insertion(+)
 create mode 100644 hello.py
Keenens-MacBook:testdir keenencates$ ls
hello.py
Keenens-MacBook:testdir keenencates$ git checkout -
error: pathspec '-' did not match any file(s) known to git.
Keenens-MacBook:testdir keenencates$ 

Related