git - how to exclude files from merging

后端 未结 5 1001
萌比男神i
萌比男神i 2020-12-28 19:28

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

相关标签:
5条回答
  • 2020-12-28 20:09

    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.

    0 讨论(0)
  • 2020-12-28 20:24

    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

    0 讨论(0)
  • 2020-12-28 20:26

    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 
    
    0 讨论(0)
  • 2020-12-28 20:29

    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.

    0 讨论(0)
  • 2020-12-28 20:30

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

    0 讨论(0)
提交回复
热议问题