git partial merge, not whole branch

GitMerge

Git Problem Overview


I've read about some tricks with merge in https://stackoverflow.com/questions/1807036/git-merging-public-and-private-branches-while-while-keeping-certain-files-intact and others and not found a solution.

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 merge config.xml -b development or 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?

Thanks

Git Solutions


Solution 1 - Git

There are a couple things you can do.

One, you can cherry-pick the 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

$ git cherry-pick $COMMIT_ID_YOU_WANT

You could also just grab config.xml from the development branch:

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

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

Solution 2 - Git

Basically you can all read after here: 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

Solution 3 - Git

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

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.

Use checkout to copy files from one branch to another:

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

Solution 4 - Git

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

Then get the common 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

I did not test that, though.

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionolegtaranenkoView Question on Stackoverflow
Solution 1 - GitmipadiView Answer on Stackoverflow
Solution 2 - GitlumpiduView Answer on Stackoverflow
Solution 3 - GitbimlasView Answer on Stackoverflow
Solution 4 - GitHuguesView Answer on Stackoverflow