开篇:
之前一直用hg进行代码仓库管理,hg merge是其一大重要功能。在此把自己的实例在此记录,希望对大家有帮助吧!!!!!
仓库介绍:
仓库很简单,只有两个branch,即default和home。可以从下面的hg log信息查看到。
hg merge 实例操作:
操作一:
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ hg branch //当前为home分支
home
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ vim test.txt //在测试文件中添加信息:“test home branch:I will push from home branch.”
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ hg commit -u RobinLau -m "test hg merge command" //提交本地仓库
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ hg push //上传远端仓库
pushing to https://[email protected]/RobinLau/rtags
searching for changes
http authorization required
realm: Bitbucket.org HTTP
user: RobinLau
password:
remote: adding changesets
remote: adding manifests
remote: adding file changes
remote: added 1 changesets with 1 changes to 1 files
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ hg sum //上传后,在home下最新为12
parent: 12:c9633976244a tip
test hg merge command
branch: home
commit: 1 unknown (clean)
update: (current)
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$
操作二:
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ hg update default //切换到default分支
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ hg branch //当前分支为default
default
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ cat test.txt //检查test.txt文件,没有”test home branch:I will push from home branch“
test home branch, hg tig and hg sum
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ vim test.txt //在测试文件中添加信息“test home branch:I will push from default branch”
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ hg commit -u RobinLau -m "test hg merge command(default)" //提交本地仓库
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ hg push //上传远端仓库
pushing to https://[email protected]/RobinLau/rtags
searching for changes
http authorization required
realm: Bitbucket.org HTTP
user: RobinLau
password:
remote: adding changesets
remote: adding manifests
remote: adding file changes
remote: added 1 changesets with 1 changes to 1 files (+1 heads) //因为我们在不同分支做了提交,这里产生了两个heads。
//可以在两个分支上分别提交内容,他们相互之间没有影响。
//如果是同一分支产生多个heads,在提交时要先merge,否则会覆盖掉别人的提交。
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ hg sum //上传后,default最新为13
parent: 13:139c7c16f34a tip
test hg merge command(default)
branch: default
commit: 1 unknown (clean)
update: (current)
操作三:
操作一和二,分别在home和default分支提交了不同的内容,现在对两个分支上的内容进行merge:
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ hg heads //我们可以看到,现在有两个heads
changeset: 13:139c7c16f34a
tag: tip
parent: 10:cf880547ca56
user: RobinLau
date: Wed Jan 01 18:19:10 2014 +0800
summary: test hg merge command(default)
changeset: 12:c9633976244a
branch: home
user: RobinLau
date: Wed Jan 01 17:24:13 2014 +0800
summary: test hg merge command
我们当前在default分支,现在把home分支的12 merge到当前分支(default分支)13:
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ hg merge -r c9633976244a
merging test.txt
会弹出下面的界面,由于两个分支分别提交不同内容,需要把merge的内容根据需求合并。
从上图可以可看出,中间列是两个branch的相同内容,左面是当前分支(default)的内容,右边是上面要被merge分支(home)的内容。我们只需把右边的内容拷贝,然后粘贴到左边,最后保存。
保存之后会出现提示:
0 files updated, 1 files merged, 0 files removed, 0 files unresolved
(branch merge, don't forget to commit)
按照提示commit,push:
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ hg commit -u RobinLau -m "merge 12 to 13(current branch)"
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ hg push
pushing to https://[email protected]/RobinLau/rtags
searching for changes
http authorization required
realm: Bitbucket.org HTTP
user: RobinLau
password:
remote: adding changesets
remote: adding manifests
remote: adding file changes
remote: added 1 changesets with 1 changes to 1 files (-1 heads)
我们再次查看default分支下的test.txt的内容,已经把home分支的merge进去了。
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ cat test.txt
test home branch, hg tig and hg sum
test home branch:I will push from default branch
test home branch:I will push from home branch.
操作四:
更新到home分支,并查看test.txt的内容,并没有default分支上提交的内容,如下:
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ hg update home
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ cat test.txt
test home branch, hg tig and hg sum
test home branch:I will push from home branch.
和操作三一样,我们可以把default分支的内容merge到home分支,在此只列出log信息:
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ hg heads
changeset: 14:342e4fb14402
tag: tip
parent: 13:139c7c16f34a
parent: 12:c9633976244a
user: RobinLau
date: Wed Jan 01 18:53:35 2014 +0800
summary: merge 12 to 13(current branch)
changeset: 12:c9633976244a
branch: home
user: RobinLau
date: Wed Jan 01 17:24:13 2014 +0800
summary: test hg merge command
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ hg merge -r 342e4fb14402
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
(branch merge, don't forget to commit)
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ hg commit -u RobinLau -m "merge 14 to 12(current)"
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ hg push
pushing to https://[email protected]/RobinLau/rtags
searching for changes
http authorization required
realm: Bitbucket.org HTTP
user: RobinLau
password:
remote: adding changesets
remote: adding manifests
remote: adding file changes
remote: added 1 changesets with 0 changes to 0 files
robin@ubuntu:/media/2ndDisk/workspace/ctags_wspace/rtags$ cat test.txt
test home branch, hg tig and hg sum
test home branch:I will push from default branch
test home branch:I will push from home branch.
到此为止,我们的两个分支内容merge成功,现在内容相同。
实例分析:
结论:
hg merge -r + 版本号————表示把指定的”版本号“merge到你当前的版本