2

So I have a GitHub repo with a master branch and a staging branch, they both have separate config files for deploying to different places. I was hoping that when I perform a pull request on the staging branch back to master, that I could some how exclude the 2 config files that I want to remain different in both branches.

How can I achieve this in the easiest way? A permanent solution would be great, but from what I've read so far it would involve doing a "stash" and a "pop"? Or maybe just performing an "undo" / "revert" after the 2 config files are overwritten?

This isn't ideal for me as it will cause Travis-CI to kick off 2 separate compilation / deployment processes.

Nick.

Nick
  • 1,015
  • 12
  • 31

1 Answers1

5

You can do this using the ours strategy. Create a file named .gitattributes in your both branches and add the following content in it:

path/to/your/config.json merge=ours

Then commit your the .gitattributes files:

git add .
git commit -m 'Added gitattributes'
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • So just so I understand this correctly, that forces each branch to maintain its own version of that file? And lastly, stupid noob question before I get it wrong, if the file is in the root of my repo, would the path just be "config.json merge=ours" – Nick Oct 06 '15 at 08:54
  • @Nick *that forces each branch to maintain its own version of that file?*–exactly! This even works when you want to merge something, but don't want to override your files, for some reason: `git merge some-branch -s ours`. And yes, if your file is in the root, just put `config.json merge=ours`. – Ionică Bizău Oct 06 '15 at 08:57
  • @Nick Also, don't forget to mark the answer if it helps you (I see you have lot of questions without marked questions)... `:-)` – Ionică Bizău Oct 06 '15 at 08:59
  • First try hasn't worked as expected and the staging branch overwrote my master branch config when I accepted the pull request, will try again in a second. I must have done something wrong. – Nick Oct 06 '15 at 09:38
  • It worked the second time, phew! Thanks a million for your time. – Nick Oct 06 '15 at 10:01
  • @Nick Great! You're welcome! Please mark the answer then, by clicking the **`✔`** button (left side of my answer). – Ionică Bizău Oct 06 '15 at 10:04
  • Sorry I thought I had, I pressed the up vote button instead, all done now, thanks. – Nick Oct 06 '15 at 10:23
  • Does this mean that neither branch will ever perform a fast-forward merge with the other, now? If you merge staging into master, then master into staging, they would still be different because of the difference in the coming files? – NHDaly Oct 06 '15 at 15:46
  • *Does this mean that neither branch will ever perform a fast-forward merge with the other, now?*–Hmm, actually yes. I don't see any better solution here. Actually when you edit config files you should commit them without a merge. – Ionică Bizău Oct 06 '15 at 15:57
  • I know this comment comes considerably late, but we did get merge issues in the end. So I worked around it by putting all of my config data into a single file. – Nick Jan 08 '16 at 13:31
  • 1
    @IonicăBizău I have a rather long list of config files that I can't just combine into one. I've added each in a separate line in the format `relative/path/config merge=ours`. After committing the changes, the merge does overwrite. Is there a specific format the `.gitattributes` needs to be in for multiple files? – Roman Dec 06 '16 at 10:40