5

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?

Traceur
  • 33
  • 11
  • I think you would have to write your own merge-script to do that kind of thing. But may I ask why you want to do that? There is probably a better solution for your problem. – Fabian S. Jan 19 '18 at 15:54
  • @FabianH. hard to explain, I simplified the scenario for the purpose of the question. But basically each branch is its own 'project' in our deployment portal and it needs its own ID and File Name which are stored in a specific file that also contain other information which need to be merged conventionally. – Traceur Jan 19 '18 at 17:07
  • then this logic should probably not be in your code, but part of you deployment portal. Your portal should set the right environment variables and your code should compile accordingly. – Fabian S. Jan 22 '18 at 15:05
  • @FabianH. that doesn't really make sense for my situation, but I probably explained it wrong – Traceur Jan 22 '18 at 16:38
  • Is this a one-off operation or is this something you expect to do many times in the future? – Code-Apprentice Jan 25 '18 at 18:39
  • @Code-Apprentice many times – Traceur Jan 25 '18 at 20:51
  • @Traceur It seems to me like there is a better solution to solve this task. If some value differs between development and production, it should not be hardcoded. Instead, you should use environment variables or configuration files so that the value can be set more dynamically. – Code-Apprentice Jan 25 '18 at 21:23

1 Answers1

3

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)
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for your answer, it's promising. I'll look into it when I have some time, congrats on the +50 rep! :P – Traceur Feb 01 '18 at 15:07