43

I have a repository with two branches: live and stage. The repository holds code for a web based application. I would like to maintain two different .htaccess files for the stage and live branches, but still keep them version controlled, since the nature of serving a stage site is a little bit different (right now for instance, I want the caches to be timed differently).

I'm a novice with git so there is probably an easy explanation, but I'd like to have a workflow where when I was in live and I pulled changes (simple git merge stage), I didn't overwrite the existing .htaccess in the live branch. Is there any way to configure this? Am I missing some big concept in git?

Likewise, there is also a chunk of a (but possibly many) .html files where I would like to keep track of changes but not merge them back into live. Just to be clear I want live and stage to got keep track of changes in these files, but know not to merge those certain changes when doing merges.

3 Answers 3

41

I found an answer on another question on Stack Overflow, credit goes to fcurella:

Let's say you want to exclude the file config.php

On branch A:

  1. Create a file named .gitattributes in the same dir, with this line: config.php merge=ours. This tells git what strategy to use when merging the file. In this case it always keep your version, ie. the version on the branch you are merging into.

  2. Add the .gitattributes file and commit

On branch B: repeat steps 1-2

Try merging now. Your file should be left untouched.

This seems like a more scalable solution.

4
  • 45
    It works only in case there is conflict in config.php while merging otherwise it doesn't work and overwrites the file. Sep 4, 2013 at 6:58
  • yes but this is the most important : in my case , i want to sync a fork from parent repository without my css files to be modified. Apr 19, 2016 at 10:06
  • 3
    @Charles-AntoineFournel if someone modifies something in live but you haven't touched it in stage, your files in stage will be overwritten, regardless of .gitattributes above
    – Otheus
    May 22, 2018 at 22:24
  • In case of composer.lock, I want it to be indicated as conflicting for manual regeneration after unsuccessful merge.
    – AnrDaemon
    Oct 13, 2023 at 9:30
10

Ankur, try this:

Assuming you're checked out on the live branch, run:

git merge --no-commit --no-ff stage

This will merge the two branches, but instead of making a new commit, will fail and leave these changes in your index file and working directory. Now since you want to keep your .htaccess file intact on the live branch, you can checkout this file from live:

git checkout live .htaccess

Now your working tree has all the changes from stage with the exception of .htaccess, and so you can finalize the merge commit:

git commit -m "Pulled changes from stage" -a
2
  • 1
    that's the general idea, but this doesn't scale well if i have 5-10 more files that eventually have differences. i would have to track those on my own.
    – Ankur
    Dec 23, 2010 at 5:18
  • If you have many more files that you'd like to preserve across these merges, then perhaps you can put these in their own empty branches (one for the live site, one for the stage site). That way, you can proceed as normal when doing merge commits between the live and stage branches, but still have separate branches for tracking changes to .htaccess and the rest of your static html files. Dec 23, 2010 at 5:39
3

To avoid overwriting problem encountered with @ankur's answer. Start by defining a merge driver that would always favor our current version of the file, by making use of the existing true command. We’ll call this driver ours, to keep in line with similar merge strategies:

git config --global merge.ours.driver true

After that we can implement the @ankur solution:

Let's say you want to exclude the file .env

On branch A (e.g. Master):

  1. Create a file named .gitattributes in the same dir, with this line:

    .env merge=ours 
    

    This tells git what strategy to use when merging the file. In this case it always keep your version, ie. the version on the branch you are merging into.

  2. Add the .gitattributes file and commit

On branch B (e.g. Develop): repeat steps 1-2

Try merging now. Your file should be left untouched.

Read this for further explanations

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.