GIT merge -keep certain parts of one branch and everything else of another

Here is the script

Master branch

-File name: xxx-master.txt

-File content:

code code ID=01 code code code

branch dev

-File name: xxx-dev.txt

-File content:

code code ID=02 code code code

When combining master with dev, I would like to keep xxx-dev.txt as the file name and ID = 02, but everything else is from master. And vice versa, when you unite in a master. Is this something I can make git understand?

+5
source share
1 answer

This is usually the case when you need to save (for a given file, here xxx-dev.txt ) various content based on branches, one of the ways:

  • version only xxx-dev.tpl template file
  • version value files named after branches: xxx-dev.dev , xxx-dev.master : since they are different, there is no merging problem.

To do this, you must register (in the .gitattributes ad ) in your filter submodule repository . .

smudge (image from Git Settings - Git Attributes , from the " Pro Git book ")

smudge script associated with the template file ( *-dev.txt ) will generate (automatically on git checkout ) the actual xxx-dev.txt file by looking at the values ​​in the xxx-dev.<branch> value file. The generated actual xxx-dev.txt remains ignored (using .gitignore ).

See the full example in the git smudge / clean filter section between branches .

Your smudge script can determine the name of the highlighted branch using:

 branch=$(git rev-parse --symbolic --abbrev-ref HEAD) 
+3
source

Source: https://habr.com/ru/post/1274817/


All Articles