git 在分支合并后修复主题的正确方法

问题描述 投票:0回答:3

我是 Git 新手,目前我只使用本地存储库,但我最终将与其他开发人员分享我的工作。我试图弄清楚在合并回主分支后修复主题分支上的代码的正确方法是什么。这是我所拥有的:

enter image description here

我的主题是在主题分支的 C2 中实现的,然后合并到 master 分支上。 master 上还有另一个针对不同主题的提交 C5。现在我刚刚发现 C2 中实现的功能存在问题。我应该在主题分支中将其修复为 C6,然后在主分支上再次合并吗?或者我应该删除主题分支,因为它已经合并并直接在主分支中修复它?

理想情况下,我希望将 C2 和 C6 保留在一次提交中,以便当我们实现远程存储库时我可以推送干净的历史记录。但我不认为我可以因为合并而压垮 C2 和 C6。有办法做到这一点吗?

合并主题后修复主题的良好做法是什么?

git merge git-rewrite-history
3个回答
1
投票

了解

git rebase
对您管理当地历史会有帮助。例如,考虑这个命令历史记录,我认为它大致重新创建了您的情况,以及使用
git rebase -i
重新排列您的本地历史记录的可能解决方案:

mkdir tmpfoo
cd tmpfoo/
git init
touch bar
git add .
git commit -am 'c1'
git checkout -b topic
touch baz
git add .
git commit -am 'c2'
git checkout master
touch foo 
git add .
git commit -am 'c3'
git merge topic
touch quux
git add .
git commit -am 'c5'
git checkout topic 
echo b >> baz 
git commit -am 'c6'
git checkout master
git merge topic
git log --oneline --graph --decorate --all
*   577f974 (HEAD, master) Merge branch 'topic'
|\
| * 6750b0d (topic) c6
* | 08ebbf2 c5
* |   0cef647 Merge branch 'topic'
|\ \
| |/
| * f1e6882 c2
* | 0e19228 c3
|/
* 90e6149 c1
19933  git rebase -i 90e6149
>>edit the sequence of commits to something like the following:
pick 0e19228 c3
pick 08ebbf2 c5
pick f1e6882 c2
s 6750b0d c6
19934  git log --oneline --graph --decorate --all
* 0e3afe0 (HEAD, master) c2
* 7611fb9 c5
* 0e19228 c3
| * 6750b0d (topic) c6
| * f1e6882 c2
|/
* 90e6149 c1
git diff ORIG_HEAD # no output here, the rebase didn't change the working tree's state

1
投票

嗯,我可以想出一个办法来做到这一点。仅当主分支包含在远程存储库中并且您始终可以从那里提取它时,它才有效。然后您应该在本地存储库中执行以下步骤:

  1. git checkout master
  2. git reset --hard <C3>
    - 重置为合并提交之前的提交
  3. git checkout topic
  4. 在C6中做你想做的事
  5. git commit --amend
    - 这实际上允许您将 C6 保存在 C2 中
  6. git checkout master
  7. git pull origin master
    - 从远程存储库中提取您重置的所有提交
  8. git merge topic

请注意,只有当 master 分支不是您的本地分支,而是存储在远程存储库中时,您才能执行此操作。此外,仅当您尚未将工作推送到远程存储库时才有效。


0
投票

“C6-C7”场景最清楚地反映了所发生事件的历史(即使并不理想)。

当您单独工作并了解所有存在的分支时,您也可以将修复添加到

Master
分支(它还显示历史记录中发生的情况)。

只要您不考虑从

Topic
“撤消”
Master
,两种解决方案都应该是等效的。

您还可以将文件保存为

C5
提交的文件,然后“硬重置”到
C3
,修复
Topic
,再次合并
Topic
(几乎是第一次),然后放回保存的文件并在提交
git diff
之前进行手动合并(使用
C5'
评估差异)。 这就是“篡改历史”。 我认为这就是 https://stackoverflow.com/a/26474391/6607497 使用不同命令的建议。

一般来说,我会小心使用变基命令。

© www.soinside.com 2019 - 2024. All rights reserved.