我有一个名为shared
的分支,与其他开发人员共享。我在feature
分支机构工作。在我的feature
分支上,我运行git checkout -B shared
,并收到以下消息:
Switched to and reset branch 'shared'
Your branch and 'origin/shared' have diverged,
and have 6 and 126 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
我拉出并解决了冲突,突然意识到我的shared
分支机构已经完成了feature
分支机构的所有更改。我的问题是:
这是怎么发生的?
查看文档时,它表示在使用-B
标志运行结帐时重置分支:
如果给出
-B
,则创建<new_branch>
(如果它不存在);否则,它会重置。
上次检查时,在共享分支上运行重置是危险的。或者“重置”在这种情况下有不同的含义吗?
答案 0 :(得分:1)
新分支(git checkout -b newbr
)的默认起始位置为HEAD
。
“create-or-reset”git checkout -B oldbr
的重置操作也是如此。因此,当您在feature
时,HEAD
表示feature
已解析为分支feature
上提示最多提交的ID,这使您的现有分支标签{{1}指向同一个提交。
幸运的是shared
将具有其先前的位置,因此您可以简单shared@{1}
(如果您使用csh或tcsh,则必须引用开括号,例如git reset shared@{1}
或{{1不确定其他一些shell)。
答案 1 :(得分:1)
git checkout -b
和git checkout -B
用于从提交中签出(新)分支。 -B
用于通过重置或以其他方式创建现有分支来重用现有分支,-b
用于创建新分支,而无法重新使用现有分支。
git checkout -B shared
与
相同git checkout -B shared HEAD
,这意味着
如果存在名为shared
的分支,请将其检出并重置为HEAD
(在您的情况下,HEAD
是feature
的提示)。如果不存在名为shared
的分支,请根据HEAD
创建一个分支。
发生了什么:您将shared
重置为feature
,然后提入origin/shared
(并解决了冲突)
答案 2 :(得分:1)
<强>有时即可。如果git checkout -B <branch_name>
已存在,branch_name
会执行重置。
为什么会这样呢?
如您所示,git checkout -B <shared>
将shared
重置为HEAD
提交并检出shared
。这究竟是做什么的?
shared
。 (这回答了您的第一个问题)。 shared
。换句话说,您重置shared
以匹配feature
。这是一个示例,显示您在checkout -B
之前的情况:
> git log --oneline --decorate --all --graph
* d0d0d0d (HEAD, feature) Some commit message.
* e0e0e0e Some commit message.
* f0f0f0f Some commit message.
* g0g0g0g Some commit message.
* h0h0h0h Some commit message.
* i0i0i0i Some commit message.
| * z0z0z0z (origin/shared) Some commit message.
| * y0y0y0y (origin/shared) Some commit message.
| * x0x0x0x (origin/shared) Some commit message.
| ---- 120 more commits ----
| * w0w0w0w (origin/shared) Some commit message.
| * v0v0v0v (origin/shared) Some commit message.
| * u0u0u0u (origin/shared) Some commit message.
| /
| /
* a0a0a0a (shared) Some commit message.
* b0b0b0b Some commit message.
* c0c0c0c Some commit message.
以下是checkout -B
:
> git checkout -B shared
> git log --oneline --decorate --all --graph
* d0d0d0d (HEAD, feature, shared) Some commit message.
* e0e0e0e Some commit message.
* f0f0f0f Some commit message.
* g0g0g0g Some commit message.
* h0h0h0h Some commit message.
* i0i0i0i Some commit message.
| * z0z0z0z (origin/shared) Some commit message.
| * y0y0y0y (origin/shared) Some commit message.
| * x0x0x0x (origin/shared) Some commit message.
| ---- 120 more commits ----
| * w0w0w0w (origin/shared) Some commit message.
| * v0v0v0v (origin/shared) Some commit message.
| * u0u0u0u (origin/shared) Some commit message.
| /
| /
* a0a0a0a Some commit message.
* b0b0b0b Some commit message.
* c0c0c0c Some commit message.
shared
现在将包含feature
,您的shared will have 6 different commits that
origin / shared`的所有更改,同样会有126次不同的提交。
重置&#39;在这种情况下有不同的含义吗?
重置具有与以往相同的含义。你基本上做了以下事情:
git checkout shared
git reset --mixed feature
我想做的就是拉下共享和合并功能。有没有办法撤消这个?
@ kdopen的评论提示了一个很好的解决方案。虽然fetch
上的--hard
和reset
选项都是可选的(有关详细信息,请参阅有关问题的评论)。这是我修改过的修复程序。
git checkout shared
git reset origin/shared
这应该让你处于这个位置,你可以按照预期在feature
分支中合并。
* d0d0d0d (feature) Some commit message.
* e0e0e0e Some commit message.
* f0f0f0f Some commit message.
* g0g0g0g Some commit message.
* h0h0h0h Some commit message.
* i0i0i0i Some commit message.
| * z0z0z0z (HEAD, shared, origin/shared) Some commit message.
| * y0y0y0y (origin/shared) Some commit message.
| * x0x0x0x (origin/shared) Some commit message.
| ---- 120 more commits ----
| * w0w0w0w (origin/shared) Some commit message.
| * v0v0v0v (origin/shared) Some commit message.
| * u0u0u0u (origin/shared) Some commit message.
| /
| /
* a0a0a0a Some commit message.
* b0b0b0b Some commit message.
* c0c0c0c Some commit message.