gpt4 book ai didi

git - merge 时避免在另一个 Git 分支中还原提交的影响

转载 作者:太空狗 更新时间:2023-10-29 12:45:36 26 4
gpt4 key购买 nike

使用 git 流。我们有一个不熟悉Git的同事昨天不小心把develop merge 成了master。

Develop 有很多功能将在我们的下一个版本中推出,需要在 merge 时恢复。这创建了一个撤消所有更改的提交。当我们将 master merge 回 develop 时,还原提交会删除我们的功能生成的代码。

在保留新功能的同时能够与 master 的修补程序同步开发的最佳方法是什么?

-- 编辑--澄清一下,还原是一个还原。 IE。 git revert -m 1 <sha> ,因为提交已经被推送到远程存储库。

自从发布这篇文章后,我想出了一个可能的修复方法,通过分支 master 并还原还原,但是我很好奇是否还有其他可能最大限度地减少冲突的可能性。

最佳答案

选项 1:硬重置和强制推送

如果可以对您的 master 分支进行非快进强制更新在您的上游存储库中,而不是恢复 develop 的 merge 进入master,你可以简单地硬重置master:

# On master branch, do a hard reset back to the commit before the merge
git reset --hard <commit of master before the merge>

# Force push to upstream ONLY IF IT'S OK WITH OTHER DEVELOPERS
git push <remote> master --force

进行硬重置和强制推送的一个可能的缺点是,如果其他开发人员已经基于 merge 提交进行工作(即已经制作在它上面提交),然后他们需要在上面重做同样的工作重置 master 的负责人。这可能是也可能不是一项困难/昂贵的任务他们。

选项 2:还原还原

我用一个快速测试库对此进行了测试。我必须强调,它可能工作,我不是 100% 确信没有任何情况我没有考虑。因此,请务必使用您的存储库的备份克隆在本地进行测试第一的。如果您选择在您的实际 repo 中使用它,请自行使用风险。

此外,这可能不是最简单/最简单的解决方案。它的优势在于然而,硬重置选项是它不会强制开发人员必须重做在重置的 master 分支上工作。

好的,所有这些都已解决,您可以尝试做的一件事是 merge masterdevelop,然后 revert 从 develop merge 的 revert 到master,然后在准备好后将 develop merge 到 master 中。在命令中:

# Coworker accidentally merges develop into master before it's ready
git merge --no-ff develop

# You revert the merge in the master branch (this creates commit "ABCDEFG"
git revert -m 1 <sha of merge commit>

# You want to merge fixes from master into develop
git checkout develop
git merge --no-ff master

# But now all that work in develop is reverted, so revert the revert "ABCDEFG"
git revert ABCDEFG

# When you're ready to merge develop into master...
git checkout master
git merge --no-ff develop

这是我用来在测试库中对此进行测试的一系列命令:

mkdir practice
cd practice/
git init

touch readme.txt
git add practice.txt
git commit -m "Add practice.txt"

git checkout -b develop

touch feature1.txt
git add feature1.txt
git commit -m "Add feature 1"

touch feature2.txt
git add feature2.txt
git commit -m "Add feature 2"

git checkout master

touch hotfix1.txt
git add hotfix1.txt
git commit -m "Fix issue 1"

git merge --no-ff develop

# Creates commit "ABCDEFG" that reverts the merge
git revert -m 1 head
git checkout develop
git merge --no-ff master
git revert ABCDEFG
git checkout master
git merge --no-ff develop

您可以在 official LinuxKernel Git documentation for git revert 阅读更多关于“Reverting Revert”技术的信息。 :

-m parent-number

--mainline parent-number

Usually you cannot revert a merge because you do not know which side of the merge should be considered the mainline. This option specifies the parent number (starting from 1) of the mainline and allows revert to reverse the change relative to the specified parent.

Reverting a merge commit declares that you will never want the tree changes brought in by the merge. As a result, later merges will only bring in tree changes introduced by commits that are not ancestors of the previously reverted merge. This may or may not be what you want.

See the revert-a-faulty-merge How-To for more details.

How to revert a faulty merge 的链接如果你强烈推荐完全想了解这项技术是如何工作的,这并不难理解,它实际上有点有趣和迷人。

关于git - merge 时避免在另一个 Git 分支中还原提交的影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17845192/

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