英文:
Git Branch Management - Pulling reverted commits
问题
我有一个分支,名为branch-1
,其中包含了36个文件的更改。
branch-1
已合并到master
。
出现了一个错误,所以我们进行了回滚。
我们意识到这个错误是一个拼写错误,所以我想创建一个新分支,名为branch-2
,拉取所有已有的工作,修复拼写错误,然后推送到新分支,以便我可以在GitHub上创建一个PR。
我尝试过的方法:
我在本地有branch-1
,所以我检出了它,然后尝试了git checkout -b branch-2
,但那只复制了master
的内容。我想我应该从旧分支创建一个新分支,修复拼写错误,拉取master
以获取此后的所有更改,然后推送,以便我可以创建一个PR,但这并没有起作用。
英文:
I had a branch, branch-1
that contained changes in 36 files
branch-1
was merged to master
there was a bug, so we reverted.
We realized the bug was a typo, so I'd like to create a new branch, branch-2
pull all the existing work, fix the typo, and then push to that new branch so I can create a PR in github.
What I've tried:
I have branch-1
locally, so I checked it out, then tried to git checkout -b branch-2
but that only copies off of master
. I figured I'd create a new branch from that old one, fix the typo, pull in master to get all the changes since then, and then push so I could create a PR but that isn't working.
答案1
得分: 0
这不是那么简单的... 你需要在旧分支的基础上创建 新的 提交,这样 git 才会 认为 那些提交从未被合并过... 可以这样做... 假设 branch-1
在合并到 master
时有2个连续的提交。那么... 操作如下:
git checkout -b branch-2 branch-1~2
git cherry-pick HEAD..branch-1 # 从 branch-1 中重新应用相同的提交
# 这些将是 _新的_ 提交,与已经合并的提交无关
# 修复拼写错误并创建一个新的提交(或者重新基于并修复拼写错误,无论你认为哪种方式是正确的处理方式)
当你的分支已经包含了修复提交,这个分支可以合并到 `master` 中,git 不会因为之前的提交已经合并而抱怨。
<details>
<summary>英文:</summary>
It's not that simple.... you need to work on _new_ commits built from the old branch so that git _thinks_ that those commits have not been merged ever.... it could be done like this... suppose that `branch-1` has 2 straight commits when it was merged in `master`. So... do this:
git checkout -b branch-2 branch-1~2
git cherry-pick HEAD..branch-1 # reapply the same commits from branch-1
# they will be _new_ commits, not related to the ones that are already merged
# fix the typo and create a new commit (or rebase and fix the typo, whatever you think is the right way to handle)
When you have your branch with the fix already committed, this branch can be merged into `master` and git won't complain about the previous commits already merged.
</details>
# 答案2
**得分**: 0
以下是您提供的内容的翻译:
假设历史如下:
```plaintext
X--X---X---X--M---RM---X <--- 主分支
\ /
\ /
F---F---F <--- 特性分支
其中:
M
- 特性分支的合并RM
- 该合并的反向操作F
- 特性分支的提交X
- 其他提交
现在,如果您想继续特性分支:
X--X---X---X--M---RM---X---M2 <--- 主分支
\ / /
\ / /
F---F---F -----修正错误 <--- 特性分支
第二次尝试合并 M2
将失败,因为 RM
不仅抵消了 M
提交,还抵消了 F
提交。
在这种情况下,如果您想避免重写历史(如重播特性分支),我建议创建一个包含反向操作的帮助分支。导致历史如下:
X----X----X----X----M---RM---X--- <--- 主分支
\ / \
\ / \
\ / RRM <-- 帮助分支
\ / \
\ / \
F---F---F---修正错误---MRRM <--- 特性分支
这个 RRM
提交撤销了 RM
提交,保留了旧历史,以一种不会与 主分支
中的 RM
提交发生冲突的方式。
之后,将特性分支合并到主分支将不会成为问题。
另一种方法是在最新的 主分支
之上挑选 F
提交(这在备选答案中有涵盖)。
英文:
Lest assume history:
X--X---X---X--M---RM---X <--- master
\ /
\ /
F---F---F <--- feature
Where
M
- merge of featureRM
- reveres of that mergeF
- feature branch commitsX
- other commits
Now if you like continue feature branch:
X--X---X---X--M---RM---X---M2 <--- master
\ / /
\ / /
F---F---F -----TypoFix <--- feature
Second attempt of merge M2
will fail since RM
is counter acting not only M
commit but also F
commits.
In such cases if you like avoid rewriting history (like rebate of feature branch), I prefer create helper branch which contains revert of revert. Leading to history like that:
X----X----X----X----M---RM---X--- <--- master
\ / \
\ / \
\ / RRM <-- helper branch
\ / \
\ / \
F---F---F---TypoFix---MRRM <--- feature
This RRM
commit reverts the RM
and lets keep old history intact in form which will not interact with RM
commit in master
branch.
After that merge feature
branch into a master will not be a problem.
Other way is to cherry pick F
commits on top of latest master
(this is covered by alternative answer).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论