git checkout -B 是否执行重置?

Does git checkout -B perform a reset?

我有一个名为 shared 的 b运行ch,与其他开发人员共享。我的工作是 feature b运行ch。在我的 feature b运行ch 上,我 运行 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 b运行ch 拥有我的 feature b运行ch 的所有更改。我的问题是:

  1. 这到底是怎么发生的?

  2. 查看文档,它说当 运行 使用 -B 标志检出时 b运行ch 被重置:

    If -B is given, <new_branch> is created if it doesn’t exist; otherwise, it is reset.

    我上次检查时,运行 在共享 b运行ch 上重置是危险的。或者 'reset' 在这种情况下有不同的含义吗?

新分支(git checkout -b newbr)的默认起始位置是HEAD

"create-or-reset"git checkout -B oldbr的复位动作也是如此。因此,由于您在 feature 上,HEAD 表示 feature 解析为分支 feature 上最尖端提交的 ID,这使您现有的分支标签 shared 指向相同的提交。

幸运的是 shared@{1} 将有它以前的位置,所以你可以简单地 git reset shared@{1} (如果你使用 csh 或 tcsh,你必须引用左大括号,例如 shared@\{1}'shared@{1}';不确定其他一些 shell)。

git checkout -bgit checkout -B 用于从提交中签出(新)分支。 -B 用于通过重置或以其他方式创建它来可能重用现有分支,而 -b 用于创建新分支而不可能重新使用现有分支。

git checkout -B shared

相同
git checkout -B shared HEAD

这意味着

如果存在名为 shared 的分支,请检查它并将其重置为 HEAD(在您的情况下,HEADfeature 的尖端)。如果不存在名为 shared 的分支,则创建一个基于 HEAD.

的分支

发生了什么:您将 shared 重置为 feature,然后拉入 origin/shared(并解决了冲突)

简答

有时。如果 branch_name 已经存在,git checkout -B <branch_name> 执行重置。

详情

Why did this happen, exactly?

如您所述,git checkout -B <shared>shared 重置为 HEAD 提交并签出 shared。这究竟是做什么的?

  • shared 指向您的 当前 HEAD 提交 (这回答了你的第一个问题)。
  • 清空您最近从索引中添加的所有内容。
  • 结帐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 thatorigin/shared`,同样,它将有 126 个与你的不同的提交。

Does 'reset' have a different meaning, in this context?

重置与往常一样具有相同的含义。您基本上执行了以下操作:

git checkout shared
git reset --mixed feature

一个很好的修复

All I wanted to do was pull down shared and merge in feature. Is there a way to undo this?

@kdopen 的评论提出了一个很好的解决方案。尽管 fetchreset 上的 --hard 选项都是可选的(有关详细信息,请参阅问题的评论)。这是我修改后的修复。

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.