Marcin
Marcin

Reputation: 221

git - how to exclude files from merging

I'm trying to keep 2 projects of website in one repository. This websites are mainly the same except template (html,css) files and few config files. The main site (which is my say Supersite) is in branch master. The second site is in branch secondarySite. Every time, when I develop some new feature in master branch I want to merge it to secondarySite but I want to exclude templates files from merging.

I found partial solution here How do I tell git to always select my local version for conflicted merges on a specific file? but it works only when I change template file in both branches and conflict occurs. When there is no conflict git just use newer remote version of file.

How can I tell git to always leave specified local files unchanged even if there is no conflict.

Or maybe I use totally wrong approach to the problem?

Thanks in advance for any help.

Upvotes: 22

Views: 19504

Answers (5)

mfathirirhas
mfathirirhas

Reputation: 2287

In each of your branches, add file .gitattributes in root.

For each branch, specified the files to be ignored at merging like this:

filename merge=ours

and dont forget to activate the driver for that:

git config --global merge.ours.driver true

Try the merging, you'll see that files specified in .gitattributes in each branches will be untouched while merging happen.

Upvotes: 10

Useless
Useless

Reputation: 67713

Or maybe I use totally wrong approach to the problem?

You're using totally the wrong approach to this problem :)

The issue is that branches, and the tooling around them, are intended for maintaining different versions of the same underlying thing.

But your two websites aren't really the same underlying thing, they're two different things with some commonality, and you're having to abuse git because you're lying to it about their relationship.

Your sane options are:

  1. make your repo represent the universe of all (ok, both) sites. So, they're both visible in the same directory structure and use different pathnames. They might have separate dev and release branches, but it's optional. If the two sites have some commonality this can just be in the form of shared files.

    eg.

    repo/supersite
    repo/secondsite
    repo/common
    
  2. make a separate repo for each site, and duplicate the common files. You can just copy the changes

  3. make a separate repo for each site, and a third repo for the common stuff, and use submodules to combine supersite+common in one place, and secondsite+common in another.

    eg.

    super-repo/site    # super site content
    super-repo/common  # common repo as submodule
    second-repo/site   # second site content
    second-repo/common # same common repo as submodule
    

    Note the submodules refer to the same repo, but each instance can be currently on a different branch or commit

To some extent the right choice depends on how much the two sites vary independently, and how often the shared bits change, and whether it's ok to force both sites to use the same version of the shared bits, and what fraction of the total content is shared vs. unique, etc. etc.

Upvotes: 3

Sireesh Yarlagadda
Sireesh Yarlagadda

Reputation: 13716

Here git-update-index - Register file contents in the working tree to the index.

git update-index --assume-unchanged <PATH_OF_THE_FILE>

Example:-

git update-index --assume-unchanged somelocation/pom.xml

or

Add file paths in .gitignore

Upvotes: 3

VonC
VonC

Reputation: 1323165

As mention in my answer on merge drivers, they are useful in case of conflict only.
That applies to a merge=ours in a .gitattributes file: see Merge strategies.

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 recent thread (2012) confirms it:

Is there a way to merge from branchA to branchB and from branchB to branchA while completely ignoring changes to a file that is tracked and exists in both branches?

No. Fundamentally, a commit object in git consists of a content state (i.e., a pointer to a tree object) and a pointer to all previous history (i.e., zero or more "parent" pointers to commit objects).
The semantics of a commit object can be thought of as "I have looked at all of the history in all of the parent commits, and the state contained in my tree pointer supersedes them all".

So you could make merge B into A, but keep A's copy of the file (e.g., using the "ours" strategy). But that is saying that you considered the state of both A and B, and decided that A's version supersedes what happened in B. If you later wanted to merge from A to B, B's version of the file would not even be considered as an outcome for the merge.

There isn't really a clever way to work around this via a different merge strategy; it's a fundamental aspect of git's data structure for storing history.

If those templates are in a sub-directory, it is best to isolate them in a git repo of their own, in order to include that repo as a submodule.

If not, then you need to revert the files incorrectly merged before committing (as in this thread):

git merge --no-commit other
git checkout HEAD XYZ  # or 'git rm XYZ' if XYZ does not exist on master
git commit 

Upvotes: 11

Sam Johnson
Sam Johnson

Reputation: 963

Have you tried using .gitignore? Details available in the git documentation here

Upvotes: 1

Related Questions