如何正确使用rebase合并git分支,考虑到我问题中列出的错误?

huangapple go评论63阅读模式
英文:

How to properly merge a git branch with rebase, given the errors below in my question?

问题

git 再次遇到了一些问题。我想使用 Bitbucket 将一个分支合并到 master,但是自动的 merge 按钮又没有起作用。

错误提示如下:

无法自动完成合并。请克隆 'MyRepo',切换到 'master',并手动合并 'mybranch'(或提交 'd58f55892e3e70cdbc1d4f2d71fdf57639fa420c'),解决任何冲突,然后推送结果。

然后我运行了以下命令:

git checkout master
git pull
git checkout mybranch
git pull
git rebase master

当然,我遇到了冲突,并根据我所知解决了它们。在解决冲突后,我运行了以下命令:

git commit --amend --no-edit

因为我不想创建一个新的提交。之后,我尝试了

git push --force

结果出现了错误:

fatal: You are not currently on a branch.
state now, use
git push origin HEAD:<name-of-remote-branch>

我不知道这是什么意思。但我使用了建议的命令,并执行了

git push origin HEAD:master

这并没有起作用,然后再次执行

git push --force origin HEAD:master

好像这样可以,但提交消息是错误的。现在它说

 Merge branch 'master' of ssh://...

在哪个阶段我应该有哪些不同的操作来创建一个正确的合并呢?

英文:

Using git I ran again in some issues. I wanted to merge a branch to master using bitbucket, but the automatic merge button again did not work.

The error says

The merge could not be completed automatically. Please clone 'MyRepo', checkout 'master' and merge 'mybranch' (or commit 'd58f55892e3e70cdbc1d4f2d71fdf57639fa420c') manually, resolving any conflicts, and push the result.

So I ran the following commands:

git checkout master
git pull
git checkout mybranch
git pull
git rebase master

Of course I get conflicts and resolved them to the best of my knowledge. After having resolved the conflicts I ran the command

git commit --amend --no-edit

because I do not want to create a new commit. After that I tried a

git push --force

which resulted in an error

fatal: You are not currently on a branch.
state now, use
git push origin HEAD:<name-of-remote-branch>

which I have no idea what it means. But I used the suggested command and did

git push origin HEAD:master

which did not work and then again

git push --force origin HEAD:master

That seem to have worked, but the commit message is wrong. It says now

 Merge branch 'master' of ssh://...

What should I have done differently at what stage to create a proper merge?

答案1

得分: 1

根据我阅读的内容,您在这一步遇到了困难:

"当然,我遇到了冲突,并尽我所知解决了它们。"

重新基准操作的原理是将您的 mybranch 分支回溯到与 master 共同的祖先提交。然后,它应用了从那个共同提交以后在 master 分支中发生的任何 提交。最后,它 重新应用 了您在 mybranch 分支自那个共同提交以后的提交。如果您遇到了合并冲突,您应该会收到提示来解决所有冲突,然后键入以下命令:

git rebase --continue

如果每个提交都导致合并冲突,您 可能 需要为每个重新应用的提交执行此操作。您 不应该 需要手动执行任何提交操作,因为 Git 会在重新基准过程的一部分自动重新应用所有提交。

您应该按照以下工作流程进行操作:

git checkout mybranch
git rebase origin/master
# Git 将分支回溯,应用新的 master 分支提交
# 然后 Git 逐个重新应用您的 mybranch 提交
# 如果出现任何冲突,手动解决后,然后输入
git rebase --continue```

完成这些步骤后,重新基准应该完成。

<details>
<summary>英文:</summary>

From what I read, you got lost at this step:

&gt; Of course I get conflicts and resolved them to the best of my knowledge.

The way a rebase works is that it rewinds your `mybranch` to the ancestor commit which is in common with `master`.  Then it applies any _new_ commits occurring in `master` from that common commit.  Finally, it _reapplies_ your commits from the `mybranch` branch since that common commit.  If you did encounter any merge conflicts, you should have been prompted to resolve all conflicts and then type the following:

    git rebase --continue

You _might_ have to do this for each reapplied commit, assuming each commit resulted in merge conflicts.  You should _not_ have to manually do any commit, as Git will reapply all commits automatically as part of the rebase process.

The workflow you should have followed would look something like this:

    git fetch origin
    git checkout mybranch
    git rebase origin/master
    # Git rewinds the branch, applying new master commits
    # Then Git reapplies your mybranch commits one by one
    # if any conflicts, resolve manually, and then type
    git rebase --continue

After this, the rebase should be complete.

</details>



# 答案2
**得分**: 1

以下是翻译好的部分:

这个序列只是很多浪费时间的废话:

git checkout master
git pull
git checkout mybranch
git pull
git rebase master


所有这些`pull`都只是愚蠢的。更新一切的方法是使用`git fetch`。所以,假设你在`mybranch`上,你应该这样说:

git fetch origin master:master
git rebase master


除非你不应该这样做,因为解决前向合并冲突的方法是首先执行反向合并并在那里解决冲突。所以正确的操作实际上是:

git fetch
git merge origin/master


这将触发合并。你解决冲突,每次编辑和修复冲突文件后,使用`git add <FileIJustResolved>`,然后最后使用`git merge --continue`完成合并。

然后推送,现在在Bitbucket上,合并将工作,因为冲突已经解决。

注意,我说的是`git merge origin/master`。这是我告诉你的一个线索,你根本不应该有一个本地的`master`分支。它只是增加了额外的步骤并使你困惑。我建议你只是删除它。

<details>
<summary>英文:</summary>

This sequence is just a lot of wasted blather:

git checkout master
git pull
git checkout mybranch
git pull
git rebase master


All those pulls are just silly. The way to update everything is to say `git fetch`. So, assuming you are on `mybranch`, you should have said:

git fetch origin master:master
git rebase master


Except that you shouldn&#39;t, because the way to fix a forward merge conflict is to first do a reverse merge and fix the conflict there. So the correct incantation was in fact

git fetch
git merge origin/master


That would have set off the merge. You resolve the conflicts, saying `git add &lt;FileIJustResolved&gt;` after editing and fixing each conflicted file, and then end up with `git merge --continue` to finish off.

You then push, and now, back on Bitbucket, the merge will work, because the conflicts have been resolved.

Notice that I said `git merge origin/master`. That is the thin edge of the wedge of me telling you that you _should not have a local `master` branch at all_. All it&#39;s doing is adding extra steps and confusing you. I would suggest that you just delete it.


</details>



huangapple
  • 本文由 发表于 2023年2月23日 23:29:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定