“结帐"与“结帐"之间的区别和"checkout -b"什么时候存在? [英] Difference between "checkout" and "checkout -b" when remote exists?

查看:75
本文介绍了“结帐"与“结帐"之间的区别和"checkout -b"什么时候存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个远程分支,则可以在本地执行git checkout MyRemoteBranch,它将正常工作.通过正常工作,我的意思是它将创建一个名为MyRemoteBranch的本地分支并切换到该分支,该本地分支将跟踪远程服务器.

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.

在这种情况下,何时必须传递-b参数?遥控器确实存在时,以下内容之间有什么区别?

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

推荐答案

Caleb的答案是正确的(我已经对其进行了投票),但此处使用的是更精确的表示法:

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

  • git checkout -b name尝试使用当前(HEAD)提交作为新分支的哈希ID创建新的分支名称​​ name .如果分支名称已经存在,则失败.

  • 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尝试使用名称​​ name 检出现有分支.

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

  • 如果由于 name 不存在而失败,则继续进行第二步,Git文档有时将其称为"DWIM"模式: .

DWIM步骤检查所有的远程跟踪名称:origin/nameupstream/name(如果您有名为 upstream 的远程对象),依此类推.如果完全匹配,它将执行以下操作:

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

您可以看到,它的参数比原始的-b选项多一个.

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

git checkout -b name commit-specifier(您可以根据需要手动调用,但由DWIM模式为您调用)尝试使用给定的名称创建新的分支名称​​ name commit-specifier 作为新分支的哈希ID.如果名称存在(如果您现在不能git checkout)该特定的提交哈希ID,则此操作将失败. (请参阅当前分支上有未提交的更改时签出另一个分支,详细了解何时可以切换到另一个提交或不能切换到另一个提交.)

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.)

为完整起见,我们还应提及git checkout --track remote-tracking name,例如git checkout --track upstream/develop.这将查找远程跟踪名称,然后使用它使用相同的名称创建一个新的(本地)分支:在这种情况下,它将创建一个新的分支develop,就像您要运行git checkout -b develop upstream/develop一样.如果您现在暂时没有 develop,但是 do 同时具有 origin/develop . DWIM模式不起作用,因为git checkout develop不知道是使用origin/develop还是upstream/develop.

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.

这篇关于“结帐"与“结帐"之间的区别和"checkout -b"什么时候存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆