git git部分合并,而不是整个分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4315948/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:34:38  来源:igfitidea点击:

git partial merge, not whole branch

gitmerge

提问by olegtaranenko

I've read about some tricks with merge in Git: merging public and private branches while while keeping certain files intact in both branchesand others and not found a solution.

我读过一些关于在Git 中合并的技巧:合并公共和私有分支,同时在两个分支和其他分支中保持某些文件完好无损,但没有找到解决方案。

In my case I'm feeling need to do opposite merge strategy. In parallel development I have to keep some files same across the arbitrary branches. From other side I do not want to do squash or no-commit merge, while difference are significant and could break current state of testing branch.

就我而言,我觉得需要采取相反的合并策略。在并行开发中,我必须在任意分支中保持一些文件相同。另一方面,我不想进行挤压或无提交合并,而差异很大并且可能会破坏测试分支的当前状态。

What I want something like

我想要的是什么

git checkout testing

git checkout testing

git merge config.xml -b developmentor git merge config\*.xml -b development

git merge config.xml -b development或者 git merge config\*.xml -b development

I guess this is like git merge-files ...command, but second file delivered from the branch, not from the filesystem. Is it possible? or may be there is a kind of workaround? submodules? attributes?

我想这就像git merge-files ...命令,但是从分支而不是从文件系统传递的第二个文件。是否可以?或者可能有一种解决方法?子模块?属性?

Thanks

谢谢

采纳答案by mipadi

There are a couple things you can do.

您可以做几件事。

One, you can cherry-pickthe changes you want, which applies only a single commit. For example, if there's a change that only touches config.xml, you can cherry-pick it with

一,你可以挑选你想要的更改,它只应用一次提交。例如,如果有一个只涉及到的变化config.xml,你可以选择它

$ git cherry-pick $COMMIT_ID_YOU_WANT

You could also just grab config.xmlfrom the development branch:

您也可以config.xml从开发分支中获取:

$ git checkout testing
$ git checkout development -- config.xml

That'll get you the same version of config.xmlthat exists in the development branch, but note that it won't pull in the history of changes to the file.

config.xml将使您获得与开发分支中存在的版本相同的版本,但请注意,它不会拉入文件更改的历史记录。

回答by lumpidu

Basically you can all read after here: http://www.gelato.unsw.edu.au/archives/git/0701/37964.html

基本上你可以在这里阅读:http: //www.gelato.unsw.edu.au/archives/git/0701/37964.html

In short, if you just want to apply changes made by a certain range of commits (this can as well be only a single commit), made to only a subset of files then do:

简而言之,如果您只想应用由特定范围的提交(这也可以是单个提交)所做的更改,仅对文件的一个子集进行更改,请执行以下操作:

git diff commit1..commit2 filepattern | git apply --index && git commit

回答by bimlas

Here's a repository with a step-by-step documentation to clarify the dangers of partial merge and show the right way.

这是一个带有分步文档的存储库,用于阐明部分合并的危险并展示正确的方法。

https://gitlab.com/bimlas/learning-by-testing-git-partial-merge/tree/doc

https://gitlab.com/bimlas/learning-by-testing-git-partial-merge/tree/doc

TL;DR:

特尔;博士:

Merge is has to be what it is: union of branches. If you doing a merge commit without merging all the files, and you will try to merge the same branches later, than Git thinks that you merged everything in the first merge commit, thus the earlier commits does not matter, it will skip the unmerged changes before the first merge.

合并必须是它的样子:分支的联合。如果您在没有合并所有文件的情况下进行合并提交,并且您稍后会尝试合并相同的分支,那么 Git 认为您在第一次合并提交中合并了所有内容,因此较早的提交无关紧要,它将跳过未合并的更改在第一次合并之前。

Use checkoutto copy files from one branch to another:

使用checkout从一个分支复制到另一个文件:

git checkout BRANCH -- FILE
git commit -m "Partial merge"

回答by Hugues

It is possible to merge against a file directly picked from the git-tree.
What I suggest is to do something like :
$ git ls-tree development -- config.xml
$ git show <blob-hash> > config.xml.development

可以对直接从 git-tree 中选取的文件进行合并。
我的建议是做类似的事情:
$ git ls-tree development -- config.xml
$ git show <blob-hash> > config.xml.development

Then get the common base :

然后得到共同的基础:

$ git ls-tree $(git merge-base HEAD development) -- config.xml
$ git show <blob-hash> > config.xml.base

$ git ls-tree $(git merge-base HEAD development) -- config.xml
$ git show <blob-hash> > config.xml.base

And finally :

最后:

$ git merge-file config.xml config.xml.base config.xml.development

$ git merge-file config.xml config.xml.base config.xml.development

I did not test that, though.

不过,我没有对此进行测试。

With a shell like zsh, you can avoid saving the blob into a temporary file with this :

使用像 zsh 这样的 shell,您可以避免将 blob 保存到临时文件中:

$ git merge-file config.xml =(git show <base-blob-hash>) =(git show <dev-blob-hash>)

$ git merge-file config.xml =(git show <base-blob-hash>) =(git show <dev-blob-hash>)