Git:将旧提交合并到当前的头部版本中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8877231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git: Merge old commit into current head version
提问by Noel
Got a file that has two commits of interest, both on the Master
branch, both only modifying a single file foo
: a previous commit AA
, and the current version in HEAD
. I would like to merge the two versions of the file, keeping bits of both, into HEAD
on Master
.
有一个文件有两个感兴趣的提交,都在Master
分支上,都只修改了一个文件foo
:以前的提交AA
,以及HEAD
. 我想将文件的两个版本合并到HEAD
on 中,同时保留两者的位Master
。
I did the simplest thing that I thought would work:
我做了我认为可行的最简单的事情:
git checkout -b merge-purgatory AA
git commit -m "pulled foo back from previous commit for merging into HEAD."
git checkout master
git merge merge-purgatory
Which simply overwrites the current HEAD
version of foo
with AA
version.
Tried the more verbose git checkout -m
as well, same result: a dumb overwrite.
这只是覆盖了当前HEAD
版本的foo
withAA
版本。也尝试了更详细的git checkout -m
方法,结果相同:愚蠢的覆盖。
How do I force git to treat the AA
version of foo
as a conflicting merge with the current HEAD
version?
如何强制 git 将 的AA
版本foo
视为与当前HEAD
版本的冲突合并?
采纳答案by Mark Longair
If git's merge isn't doing what you want, you could do the following instead:
如果 git 的合并没有做你想要的,你可以做以下事情:
- Make sure that there are no uncommitted changes in the file
foo
, e.g. by making sure thatgit status
is clean. - Overwrite
foo
with the version fromAA
using:git show AA:foo > foo
- Selectively stage only the changes from
foo
that you want with:git add -p foo
- Discard all the other changes to
foo
withgit checkout -- foo
- Commit the staged changes:
git commit
- 确保文件中没有未提交的更改
foo
,例如确保文件git status
是干净的。 foo
使用以下版本覆盖AA
:git show AA:foo > foo
- 有选择地仅暂存
foo
您想要的更改:git add -p foo
- 放弃所有的其他更改
foo
与git checkout -- foo
- 提交阶段性更改:
git commit
Alternatively, if you'd rather use a graphical diff tool (such as meld
), you could just do:
或者,如果您更愿意使用图形差异工具(例如meld
),您可以这样做:
git show AA:foo > old-foo
meld old-foo foo
回答by alexmogavero
The previous answers do not keep the story of the change, so the fact that you used a previous commit to generate the new commit. The following procedure will keep this story.
以前的答案没有保留更改的故事,因此您使用以前的提交来生成新提交的事实。以下过程将保留这个故事。
git checkout -b merge-purgatory AA
here you need to slightly modify your file, for example you can add an empty line. Then
在这里您需要稍微修改您的文件,例如您可以添加一个空行。然后
git commit "pulled foo back from previous commit for merging into HEAD."
git checkout master
git merge --no-commit merge-purgatory
In this way the merge will fail and then all you need to do is to solve the conflict. This worked for me.
这样合并将失败,然后您需要做的就是解决冲突。这对我有用。
回答by VonC
git merge --no-commit merge-purgatory
would at least give you the opportunity to review:change the merge before committing it.
See also techniques proposed in "How do you merge selective files with git-merge?", based on cherry-picking or checkout.
至少会让您有机会:在提交之前更改合并。
另请参阅“如何使用 git-merge 合并选择性文件?”中提出的技术,基于cherry-picking 或checkout。
Regarding forcing the manual merge, you could declare in a .gitatributes
file, for that specific file, a merge policy set to unset.
关于强制手动合并,您可以在.gitatributes
文件中声明,对于该特定文件,将合并策略设置为 unset。
Performing a three-way merge
merge
The attribute merge affects how three versions of a file is merged when a file-level merge is necessary during git merge, and other commands such as git revert and git cherry-pick.
执行三路合并
合并
The attribute merge affects how three versions of a file is merged when a file-level merge is necessary during git merge, and other commands such as git revert and git cherry-pick.
Unset
Take the version from the current branch as the tentative merge result, and declare that the merge has conflicts. This is suitable for binary files that does not have a well-defined merge semantics.
取当前分支的版本作为暂定合并结果,声明合并有冲突。这适用于没有明确定义的合并语义的二进制文件。
回答by Mahieddine M. Ichir
I get rid of it using:
我摆脱它使用:
git checkout -f b855a13754fabf5cef6ff93ab00558608a839377 -- .
git checkout -f b855a13754fabf5cef6ff93ab00558608a839377 -- .
which forces the changes of the commit id into my current branch (master), then I created a new branch (checkout -b
) from these applied changes and created a merge request from the latter.
这迫使提交 ID 的更改进入我当前的分支 (master),然后我checkout -b
从这些应用的更改创建了一个新分支 ( ),并从后者创建了一个合并请求。
None of the above worked for me :-(
以上都不适合我:-(