简体   繁体   中英

Git checkout difference git checkout origin/<branch-name> and git checkout <branch-name>?

When I do git checkout origin/bugfix/NTP-183-datefns git shows

Note: checking out 'origin/bugfix/NTP-183-datefns'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 6fd089d.

But when I try git checkout bugfix/NTP-183-datefns

Switched to branch 'bugfix/NTP-183-datefns'
Your branch is up-to-date with 'origin/bugfix/NTP-183-datefns'.

What is happening here?

Git has an automatism for working with branches. The default remote is called origin on your repository. If you take the remote in the command line, git will only checkout the commit of this branch without a branch. You are in a detached HEAD state . The text explains what happens here and the complications for you.

If you have only one remote in your repository configured and you want to checkout a branch comes from this remote than it will automatically create a local branch for you.

You can checkout at any commit (hash), including commits "pointed" to by remote branches. But if you checkout a commit pointed to by a remote branch, locally you are going to be pointing to a raw commit.

This is what the warning is telling you. To be able to do anything useful you need to be on a local branch. Checking out a remote branch will not automatically generate a local branch to work on - it will just move you to that commit. To do what you want in one go:

git checkout -t -b bugfix/NTP-183-datefns origin/bugfix/NTP-183-datefns

Where:

  • "-t" makes this a tracking branch (this is a nice optional)
  • "-b bugfix/NTP-183-datefns" creates a local branch called bugfix/NTP-183-datefns.

This is basically doing the two steps that you did in one step, so it is equivalent to (without the -t option):

git checkout origin/bugfix/NTP-183-datefns
git branch bugfix/NTP-183-datefns
git checkout bugfix/NTP-183-datefns

which is the same as:

git checkout origin/bugfix/NTP-183-datefns
git checkout -b bugfix/NTP-183-datefns

origin/<branch-name> is a remote branch reference. It cannot be modified.
So when you checkout this reference, git cannot move you to this branch but it moves you to the commit referenced by the branch. You are then in a detached HEAD state, which means you're not on a branch but directly on a commit (implications are explained in the output of the command).

<branch-name> is simply a local branch, so you can work on it.
So when you checkout this reference, git moves you to the branch.

There is a little hint: if the local branch <branch-name> doesn't exists but there exists exactly one remote branch with the same name over all the remotes, git will automatically create the local branch tracking the remote branch and checkout it (in this case git checkout <branch-name> is equivalent to git checkout --track -b <branch-name> any_remote/<branch-name> )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM