Difference between “checkout” and “checkout -b” when remote exists?

半腔热情 提交于 2019-12-13 05:29:40

问题


If I have a remote branch, I can locally do git checkout MyRemoteBranch and it will work fine. By work fine I mean it will create a local branch called MyRemoteBranch and switch to it, and this local will track the remote.

In this case, when do I have to pass the -b parameter? What is the difference between the below when the remote does exist:

git checkout MyRemoteBranch

Vs

git checkout -b MyRemoteBranch

回答1:


Caleb's answer is correct (and I've upvoted it) but here it is in more precise notation:

  • git checkout -b name tries to create a new branch name name, using the current (HEAD) commit as the hash ID for the new branch. This fails if the branch name already exists.

  • git checkout name tries to check out an existing branch using the name name.

    • If that fails because name does not exist, it goes on to a second step, which Git documentation sometimes calls the "DWIM" mode: Do What I Mean.

    • The DWIM step checks all of your remote-tracking names: origin/name, upstream/name if you have a remote named upstream, and so on. If exactly one matches, it performs the equivalent of:

      git checkout -b name remote-tracking-name

      which as you can see has one more argument than the original -b option.

  • git checkout -b name commit-specifier—which you can invoke manually if you like, but is invoked for you by the DWIM mode—tries to create a new branch name name, using the given commit-specifier as the hash ID for the new branch. This fails if the name exists or if you can't git checkout that particular commit hash ID right now. (See Checkout another branch when there are uncommitted changes on the current branch for more about when you can or cannot switch to another commit.)

  • For completeness, we should also mention git checkout --track remote-tracking name, e.g., git checkout --track upstream/develop. This looks up the remote-tracking name, then uses it to create a new (local) branch using the same name: in this case it would create a new branch develop, as if you'd run git checkout -b develop upstream/develop. It's useful if you have no develop right now, but do have both origin/develop and upstream/develop. The DWIM mode won't work because git checkout develop doesn't know whether to use origin/develop or upstream/develop.




回答2:


In this case, when do I have to pass the -b parameter? What is the difference between the below when the remote does exist

The difference is that if you pass the -b flag, git creates a new branch with the name you give and based on the branch you were in when you created that branch. Without the flag, git will look for an existing branch, including one in any remote repos that you're tracking, and switch to one of those if if finds one.

So, let's say: 1) you're currently in your own branch called foo; 2) you've got a remote called upstream; and 3) that remote has a branch called bar. If you say:

git checkout bar

then you'll switch to a local copy of the foo branch on upstream (assuming your repo already knows about foo because you've recently done a git fetch). In other words, if you do:

git diff upstream/bar

then git will report no differences.

But, if you give the -b flag:

git checkout -b bar

then git will create a new branch that happens to also be named bar, but that will have the same content as the foo branch that you were just in. In other words:

git diff upstream/bar

will report the differences between upstream's bar branch and your local bar branch (which, again, got its content from your foo branch).

It's easy to try this yourself. Just pick two existing remote branches that don't exist locally, say upstream/branch1 and upstream/branch2 on your remote that you know have some differences. Verify that there are differences with git diff upstream/branch1 upstream/branch2. Now checkout the first branch without the -b flag:

 git checkout branch1

Now you should have a local branch called branch1. Again, check that this matches the upstream version: git diff upstream/branch1 branch1. There should be no differences. Next, try creating a branch2 using the -b flag:

 git checkout -b branch2

Now you should have a local branch2, but it should match what's in branch1 and also upstream/branch1. Verify:

 git diff upstream/branch1 branch2      # should be no diffs
 git diff upstream/branch2 branch2      # should be like diffing branch1 and upstream/branch2



回答3:


git checkout MyRemoteBranch is when is already this branch in the remote repository

git checkout -b MyRemoteBranch with this -b you create a new branch locally no matter if is not in the remote




回答4:


There is a plenty of difference between those two commands. when you use

git checkout myRemoteBrench

It only switch your branch if the branch is already existing.

But when you use

git checkout -b myRemoteBranch

First it check whether the mentioned branch is existing, if not it creates a new branch with the given name and switch to it.



来源:https://stackoverflow.com/questions/52709377/difference-between-checkout-and-checkout-b-when-remote-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!