3

Do you know how could I have two different copies of the same file in two different branches in git?

Let assume I have a file called config.
What I'm trying to achieve is have a copy of this file in the dev branch different in the master branch.

The important thing is that file can't be ignored in the gitignored.

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
Mazzy
  • 13,354
  • 43
  • 126
  • 207
  • you mean the file config is not staged? – Lynch Apr 21 '16 at 19:24
  • yes exactly, it should not be staged – Mazzy Apr 21 '16 at 19:26
  • If I understand correctly, you don't want the file tobbe merged back into master and vice versa? – rubenvb Apr 21 '16 at 19:53
  • correct. i don't want – Mazzy Apr 21 '16 at 19:53
  • So, you *do* want git to switch the contents of the file automatically for you when you switch branches, but you *don't* want git to store and manage the contents of the file for either branch? That seems like a self-contradictory set of requirements. – torek Apr 21 '16 at 20:09
  • ok once again. I haven't been clear. I'd like that in the dev branch the file has a set of configurations and in the master has a different set of configuration. This file need to be deployed and usually before deploying I merge dev to master. If I do this now the dev config file (coming from dev) will be merged with the config file in the master and this means that in the master I'll have dev configs – Mazzy Apr 21 '16 at 20:11
  • 1
    Is [this](https://stackoverflow.com/questions/332528/is-it-possible-to-exclude-specific-commits-when-doing-a-git-merge/3970442#3970442) what you want? – xuhdev Apr 21 '16 at 20:30

1 Answers1

1

You can checkout the 2 branches. modify the file and commit them to each branch.

If you want to merge the branches and to have 2 separate files (different content) in each branch, commit the changed to the branches and then set the desired file tin your .gitattributed to always grab the ours file - which will result in this file to never be overwritten

Read all about it and to set it up here:
https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes#Merge-Strategies

You can also use Git attributes to tell Git to use different merge strategies for specific files in your project.

One very useful option is to tell Git to not try to merge specific files when they have conflicts, but rather to use your side of the merge over someone else’s.

This is helpful if a branch in your project has diverged or is specialized, but you want to be able to merge changes back in from it, and you want to ignore certain files.

config_file merge=ours

As @torec mentioned in his comment:

Beware of what I consider a bug in git: if git is able to trivially merge the file it will not run your custom merge driver. Git always tries a simple 2-way merge first, before attempting the 3-way merge

So another option is to use smudge/clear filter to make sure you have the right file.

Smudge

Read all about it and to set it up here:
https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes

It turns out that you can write your own filters for doing substitutions in files on commit/checkout.

These are called clean and smudge filters.

In the .gitattributes file, you can set a filter for particular paths and then set up scripts that will process files just before they’re checked out (“smudge”, see Figure 8-2) and just before they’re staged (“clean”, see Figure 8-3).

These filters can be set to do all sorts of fun things.

enter image description here

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • Actually this doesn't work for my scenario. In my scenario the config file is always unchanged and so no merge action is applied to it. that is the same thing recommended to the question referenced above but the users have reported that it doesn't work. – Mazzy Apr 21 '16 at 21:16
  • So how do you have different files? you had to change it on each branch so they are changed. do i miss something? – CodeWizard Apr 21 '16 at 21:17
  • You have different content of the file on each branch. This is what i understood from your question – CodeWizard Apr 21 '16 at 21:18
  • Exactly different content in the file. Ok I'm going to try again this strategy. Let see if it works – Mazzy Apr 21 '16 at 21:19
  • Different content = change. isnt it? – CodeWizard Apr 21 '16 at 21:21
  • 2
    Beware of what I consider a bug in git: if git is able to *trivially* merge the file it will not run your custom merge driver. Git always tries a simple 2-way merge first, before attempting the 3-way merge. – torek Apr 21 '16 at 21:49
  • I agree and i would have use smudge/clear in this case. – CodeWizard Apr 21 '16 at 21:52