I saw a weird type of git checkout command.
git checkout -
What does -
do here?
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.
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.
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.
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With