Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GIT merge -keep specific parts of one branch and everything else of the other

Tags:

git

git-merge

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?

like image 679
Traceur Avatar asked Oct 17 '22 00:10

Traceur


1 Answers

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.

smudge (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)
like image 187
VonC Avatar answered Oct 22 '22 02:10

VonC