gpt4 book ai didi

git - git checkout -B 是否执行重置?

转载 作者:太空狗 更新时间:2023-10-29 14:45:44 33 4
gpt4 key购买 nike

我有一个名为 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)

我 pull 并解决了冲突,突然意识到我的 shared分支拥有我所有的更改 feature分支。我的问题是:

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

  2. 查看文档,它说在使用 -B 运行 checkout 时会重置分支。标志:

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

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

最佳答案

简答

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

详情

Why did this happen, exactly?

如您所述,git checkout -B <shared>重置 sharedHEAD提交并 checkout shared .这究竟是做什么的?

  • shared在您的当前 HEAD 提交(这回答了您的第一个问题)。
  • 清空您最近从索引中添加的所有内容。
  • checkout 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 个与您不同的提交。

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 的评论提出了一个很好的解决方案。尽管fetch--hard reset 上的选项是可选的(有关详细信息,请参阅对该问题的评论)。这是我修改后的修复。

git checkout shared 
git reset origin/shared

这应该让您处于这个位置,您可以从中 merge 到您的 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.

关于git - git checkout -B 是否执行重置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28573481/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com