Question

I have 2 local files that I need to merge. Neither is versioned in the git. Is there a linux command to merge files? I tried merge, but didn't get any useful output. The mergefile seemed to show that the entire first file was deleted and the second file is the whole output:

$ cat m1
hello there
my friend
how are you
$
$ cat m2
hello there
my chum
how are you
$
$ merge merge m1 m2
$ cat merge
<<<<<<< merge
=======
hello there
my chum
how are you
>>>>>>> m2

EDIT

to clarify, I am looking to create a git-style mergefile, showing differences between the files inline, rather than simply gluing 2 files together end to end (which doesn't work with code anyway)

Was it helpful?

Solution

If you do not mind using a graphical application, I can really recommend KDiff3 which is a wonderful merge tool. In your case, run

kdiff3 -o output.txt m1 m2

and you'll get

kdiff3 screenshot

For each merge conflict, press the A or B button to select which line to choose (or both). You are free to edit as you like in the bottom area.


Update: If you want to just work in text mode in a terminal, setting up a temporarily git repository is super cheap. Learning to use git requires some effort, but it is doable. For your case, the following commands gets the work done:

$ mkdir /tmp/test
$ cd /tmp/test
$ git init
Initialized empty Git repository in /tmp/test/.git/
$ touch gitfile
$ git add gitfile
$ git commit -m "initial commit"
[master (root-commit) 31efd12] initial commit
 0 files changed
 create mode 100644 gitfile
$ git checkout -b m1-branch
Switched to a new branch 'm1-branch'
$ cat m1 >> gitfile
$ git add gitfile
$ git commit -m "m1 content"
[m1-branch 420b423] m1 content
 1 file changed, 3 insertions(+)
$ git checkout -b m2-branch master
Switched to a new branch 'm2-branch'
$ cat m2 >> gitfile
$ git add gitfile
$ git commit -m "m2 content"
[m2-branch c4c525a] m2 content
 1 file changed, 3 insertions(+)
$ git checkout master
Switched to branch 'master'
$ git merge m1-branch m2-branch
Fast-forwarding to: m1-branch
Trying simple merge with m2-branch
Simple merge did not work, trying automatic merge.
Auto-merging gitfile
ERROR: content conflict in gitfile
fatal: merge program failed
Automatic merge failed; fix conflicts and then commit the result.
$ cat gitfile
hello there
<<<<<<< .merge_file_qRmZJF
my friend
=======
my chum
>>>>>>> .merge_file_BUlXHH
how are you
$
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top