远程分支上的 Rebase 提交

发布于 2024-12-10 14:24:01 字数 1006 浏览 0 评论 0原文

我在 github 上分叉了一个项目,创建了一个新分支并进行了一些更改(提交 B、C 和 D),因此我得到了以下树:

... <-- A                     (master)
         \
          B <-- C <-- D       (branch2)

我错误地合并并删除了 branch2 所以现在我有了这棵树:

... <-- A <-- B <-- C <-- D   (master)

然而,我意识到我想维护一个反映上游更改的主分支,并创建了 myproject-master 来完成我的工作。我通过将提交重新定位到新分支来移动它们。变基后,我通过 git reset --hard "A" 从 master 的历史记录中删除了 <-- B <-- C <-- D 部分。我还合并了 CD,从现在开始我将将该提交称为 CD。我的问题是我无法将更改推送到主服务器上,并且本地和远程有不同的树:

这是本地树(我想要在两侧都有):

... <-- A                     (master)
         \
          B <-- CD            (myproject-master)

这是远程树:

... <-- A <-- B <-- C <-- D   (master)
         \
          B <-- CD            (myproject-master)

当我尝试推送时在 master 上,git 拒绝推送,这样我就不会丢失数据。这很好,但我已经在另一个分支上有了这些提交,并且想从主分支中清除它们。我该怎么做?

I forked a project on github, made a new branch and made some changes (commits B, C and D) so I had the following tree:

... <-- A                     (master)
         \
          B <-- C <-- D       (branch2)

I mistakenly merged and deleted branch2 so now I had this tree:

... <-- A <-- B <-- C <-- D   (master)

However, I realized that I want to maintain a master branch that reflects the upstream changes and created myproject-master to have my work on that. I moved my commits by rebasing them to that new branch. After rebasing I deleted the <-- B <-- C <-- D part from the history on master by git reset --hard "A". I also squased C and D and I'll refer to that commit as CD from now on. My problem is that I can't push my changes on master and I have different trees in local and remote:

This is the local tree (what I want to have on both sides):

... <-- A                     (master)
         \
          B <-- CD            (myproject-master)

This is the remote tree:

... <-- A <-- B <-- C <-- D   (master)
         \
          B <-- CD            (myproject-master)

When I try to push on master, git rejects the push so I don't lose data. Which is fine, but I already have those commits on the other branch and would like to clean them from the master. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执笔绘流年 2024-12-17 14:24:01

如果您是唯一在存储库上工作的人,则可以安全地将主分支“强制推送”到远程存储库,例如:

git checkout master
git push -f myrepo master

...但由于这会重写主分支的历史记录,因此您必须如果您有任何协作者在 A 之前已经拉取了您的 master,请务必小心。

If you are the only person working on the repository, it's safe to do a "force push" of your master branch to the remote repository, e.g. with:

git checkout master
git push -f myrepo master

... but since that's rewriting the history of the master branch, you will have to be more careful if you have any collaborators who have already pulled your master when it was ahead of A.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文