Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git - how to exclude files from merging

Tags:

git

merge

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.

like image 496
Marcin Avatar asked Jun 09 '11 14:06

Marcin


People also ask

How do I make git ignore a folder while merging?

git checkout staging # go to staging branch git checkout dev . # this checkout dev file changes into staging git reset HEAD build # this remove added file in build folder git clean -f # this drops untracked files we just reseted git checkout -- .


2 Answers

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 
like image 62
VonC Avatar answered Oct 03 '22 10:10

VonC


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

like image 29
Sireesh Yarlagadda Avatar answered Oct 03 '22 09:10

Sireesh Yarlagadda