问题
Here's the scenario
Master branch
-File name: xxx-master.txt
-File content:
code
code
ID=01
code
code
code
Dev branch
-File name: xxx-dev.txt
-File content:
code
code
ID=02
code
code
code
When merging master into dev, I'd like to keep xxx-dev.txt as the file name and ID=02, but everything else from master. And vice versa when merging dev into master. Is that something I can make GIT understand?
回答1:
This is typically a case where you need to keep (for a given file, here xxx-dev.txt
) different content based on branches, one way is to:
- version only a template file
xxx-dev.tpl
- version value files named after the branches:
xxx-dev.dev
,xxx-dev.master
: since they are different, there is no merge issue.
For that, you would register (in a .gitattributes declaration) in your submodule repo a content filter driver.
(image from "Customizing Git - Git Attributes", from "Pro Git book")
The smudge
script, associate to the template file (*-dev.txt
), would generate (automatically on git checkout
) the actual xxx-dev.txt
file by looking values in the right xxx-dev.<branch>
value file.
The generated actual xxx-dev.txt
file remains ignored (by the .gitignore
).
See a complete example at "git smudge/clean filter between branches".
Your smudge
script can determine the name of the checked out branch with:
branch=$(git rev-parse --symbolic --abbrev-ref HEAD)
来源:https://stackoverflow.com/questions/48344301/git-merge-keep-specific-parts-of-one-branch-and-everything-else-of-the-other