git checkout -B执行重置吗?

时间:2015-02-17 23:51:56

标签: git git-checkout git-reset

我有一个名为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分支机构的所有更改。我的问题是:

  1. 这是怎么发生的?

  2. 查看文档时,它表示在使用-B标志运行结帐时重置分支:

      

    如果给出-B,则创建<new_branch>(如果它不存在);否则,它会重置。

    上次检查时,在共享分支上运行重置是危险的。或者“重置”在这种情况下有不同的含义吗?

3 个答案:

答案 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 -bgit checkout -B用于从提交中签出(新)分支。 -B用于通过重置或以其他方式创建现有分支来重用现有分支,-b用于创建新分支,而无法重新使用现有分支。

git checkout -B shared

相同
git checkout -B shared HEAD

,这意味着

如果存在名为shared的分支,请将其检出并重置为HEAD(在您的情况下,HEADfeature的提示)。如果不存在名为shared的分支,请根据HEAD创建一个分支。

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

答案 2 :(得分:1)

简答

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

详细

  

为什么会这样呢?

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

  • 当前HEAD提交处指出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上的--hardreset选项都是可选的(有关详细信息,请参阅有关问题的评论)。这是我修改过的修复程序。

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.