Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run git merge algorithm on two individual files

Tags:

git

I want to take advantage of the git-merge algorithm on two arbitrary files in a git repo. Here is my working directory:

folder/     file1     file2 

file1 and file2 are similar, but I want to see how git would merge them as if they were different versions of the same file. In other words, I want something like this:

git merge-files file1 file2 > merge_of_file1_file2 

Is there a way to do this?

like image 704
Alexander Bird Avatar asked Feb 03 '12 02:02

Alexander Bird


People also ask

Can git merge two files?

Git can handle most merges on its own with automatic merging features. A conflict arises when two separate branches have made edits to the same line in a file, or when a file has been deleted in one branch but edited in the other. Conflicts will most likely happen when working in a team environment.

How do I merge individual files in git?

We can use git checkout for far more than simply changing branches. If we supply it with a branch name and a file, we can replace a corrupted or broken file. Instead, if we want to pass some of the changed content we can use the --patch flag to manually merge an individual file.

Can you merge the same branch twice?

Merging a branch multiple times into another works fine if there were changes to merge. Show activity on this post. Actually yes you totally can, when you merge again it will bring over any commits that don't exist on production.


2 Answers

This doesn't really make sense, because you're not providing a common ancestor. If you do have one, however, you can use:

git merge-file <current-version> <common-ancestor> <other-version> 

This places the results in the current version file; if you want them elsewhere, use:

git merge-file -p <current> <common> <other> > <dest> 

It needs the common ancestor to provide something to consider changes relative to. You could hack it, by providing an empty file or a copy of an older version of one of them from the history of your repository, but the quality of results will depend on how well you select a common ancestor, since it's merging the two diffs, between that and each of the new versions. An empty file will only work well if the two are very similar (many runs of at least three identical lines).

Without that, all you can really do is look at the differences:

git diff --no-index file1 file2 
like image 159
Cascabel Avatar answered Oct 14 '22 13:10

Cascabel


For what you are trying to do:

touch file3  #empty git merge-file file1 file3 file2 

This will do a three-way merge of file1 and file2 with file3 ( empty file ) as the base.

Note that this writes to file1. You can of course do the below if that is not desired:

touch file3 cp file1 merge_of_file1_file2 git merge-file merge_of_file1_file2 file3 file2 
like image 38
manojlds Avatar answered Oct 14 '22 11:10

manojlds