run git merge algorithm on two individual files

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

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

Just to add on to manojlds's answer, here's a nice, complete function you can add to your .bashrc that does the job. It has the benefit of properly identifying your two files' names in the "merge conflict" blocks.

merge() {
  local ext
  [ $# -ne 2 ] && echo "Error: Need exactly two args." && return 1
  [[ ! -r $1 || ! -r $2 ]] && echo "Error: One of the files is not readable." && return 1
  if [[ ${1##*/} =~ '.' || ${2##*/} =~ '.' ]]; then
    [ ${1##*.} != ${2##*.} ] && echo "Error: Files must have same extension." && return 1
     ext=.${1##*.}
  fi
  touch tmp$ext # use empty file as the 'root' of the merge
  cp $1 backup$ext
  git merge-file $1 tmp$ext $2 # will write to file 1
  mv $1 merge$ext
  mv backup$ext $1
  rm tmp$ext
  echo "Files merged into \"merge$ext\"."
}

You can also use KDiff3 for this.

  • Select file one
  • Select file two

And merge :)

http://kdiff3.sourceforge.net/