Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "git checkout -" do?

I saw a weird type of git checkout command.

git checkout -

What does - do here?

like image 685
msc Avatar asked Nov 09 '17 14:11

msc


People also ask

What does git checkout file do?

The git checkout command is used to update the state of the repository to a specific point in the projects history. When passed with a branch name, it lets you switch between branches. Internally, all the above command does is move HEAD to a different branch and update the working directory to match.

What happens when you checkout a commit?

Checking out a commit hash will put you into "detached head" state. A commit (hash) can belong to many branches, and many branch HEADs can point to the same commit hash. Checking out a commit (regardless if it is the HEAD of any branch) will put you to the detached HEAD state.

What is git checkout and git pull?

The git pull command is used to fetch and download content from a remote repository and immediately update the local repository to match that content. Merging remote upstream changes into your local repository is a common task in Git-based collaboration work flows.


1 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}

like image 140
Mario F Avatar answered Oct 05 '22 23:10

Mario F