合并两个本地文件

3

我有两个本地文件需要合并。这两个文件都没有在git中进行版本控制。有没有Linux命令可以合并文件?我尝试了merge,但是没有得到有用的输出。合并后的文件似乎显示第一个文件被删除了,只剩下第二个文件的内容。

$ 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

编辑

为了澄清,我想创建一个类似于Git的合并文件,内联显示文件之间的差异,而不是简单地将两个文件末尾粘在一起(这对代码也没有用)。

1个回答

4

如果您不介意使用图形化应用程序,我强烈推荐KDiff3,这是一个非常好的合并工具。在您的情况下,请运行

kdiff3 -o output.txt m1 m2

你将得到:

kdiff3 截图

针对每个合并冲突,按下 A 或 B 按钮来选择要选择哪一行(或者选择两者)。在底部区域中可以自由编辑。


更新:如果你只想在终端中使用文本模式工作,建立一个临时的 git 仓库非常容易。学习使用 git 需要花费一些努力,但是它是可行的。对于你的情况,以下命令可以完成工作:

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
$

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接