灵感来自Git初学者:权威的实用指南。

这是一个关于初学者实际使用Mercurial的信息汇编。

初学者——一个接触过源代码控制却没有很好地理解它的程序员。

实用——涵盖大多数用户经常遇到的情况——创建存储库、分支、合并、从远程存储库拉/推到远程存储库等等。

注: 解释如何完成某件事,而不是解释某件事是怎样的 实现的。 回答一个问题。 回答清楚,尽可能简洁。 上编辑/扩展现有答案,而不是创建新答案 同样的话题。 请为想了解更多的人提供Mercurial wiki或HG Book的链接。

问题:

安装/设置

如何安装Mercurial? 如何设置Mercurial? 如何创建一个新的项目/存储库? 如何将其配置为忽略文件?

使用代码

How do you get the latest code? How do you check out code? How do you commit changes? How do you see what's uncommitted, or the status of your current codebase? How do you remove files from the repository? How do you destroy unwanted commits? How do you compare two revisions of a file, or your current file and a previous revision? How do you see the history of revisions to a file or repository? How do you handle binary files (visio docs, for instance, or compiler environments)? How do you merge files changed at the "same time"? How do you revert a Changeset? How do you go back to a previous version of the code? How do you extract a patch from a specific changeset? How do you record that you renamed or deleted a file without using the Mercurial command?

标记、分支、发布、基线

你如何“标记”、“标记”或“发布”一组特定文件的特定修订,以便以后总是可以提取它? 你如何进行特定的“释放”? 你如何分支? 如何合并分支? 如何将一个分支的部分合并到另一个分支中?

其他

好的GUI/IDE插件Mercurial?优点/缺点? 还有其他初学者应该知道的常见任务吗? 如何与Subversion交互?

其他Mercurial参考资料

Mercurial:权威指南 Mercurial维基 满足Mercurial |偷窥码屏幕播放 掌握Mercurial | TekPub屏幕播放 Hg初始化-接地水银教程


当前回答

如何从存储库中删除文件?

从存储库中删除一个文件,并在下次提交时删除它:

$ hg remove {file(s)}

从存储库中删除文件,但不删除该文件

$ hg remove -Af {file(s)}

或从Mercurial 1.3

$ hg forget {file(s)}

其他回答

如何将其配置为忽略文件?

Ignore是在存储库根目录中一个名为.hgignore的普通文本文件中配置的。添加它就像一个普通的文件:

hg add .hgignore

有两种语法选项可用于文件匹配,glob和regexp。Glob是类unix的文件名扩展,regexp是正则表达式。通过在一行上单独添加syntax: glob或syntax: regexp来激活每一个。接下来的所有行都将使用该语法,直到下一个语法标记。您可以有任意多的语法标记。默认语法是regexp,因此如果只使用regexp,则不需要任何语法标记。

您可以使用#添加注释

例子:

# python temporary files
syntax: glob
*.pyc

#editor autosaves
*~

# temporary data
syntax: regexp
temp

Ignore仅适用于非托管文件(即尚未签入的文件)。要忽略版本控制下的文件,可以使用开关-I和-X。

如何恢复变更集?

有几个选项

简单方法(撤销单个更改集)

$ hg backout -m 'back out second change' tip
reverting myfile
changeset 2:01adc4672142 backs out changeset 1:7e341ee3be7a
$ cat myfile
first change

Hard Way(手动区分和应用)

步骤1:创建一个补丁文件来恢复版本107和108之间的更改:

hg diff -r107 -r108 --reverse  > revert-change.patch

(或者,hg diff -r108 -r107加上no——reverse也会做同样的事情)

第二步:应用补丁文件:

patch -p1 < revert-change.patch

有些差异可能不适用,例如:

Hunk #3 FAILED at 517.
1 out of 3 hunks FAILED -- saving rejects to file 'foo/bar.c.rej'

.rej文件将包含应用失败的diff的内容,您需要查看一下。

如何与Subversion交互?

有三种方法:


convert扩展将把一个现有的Subversion存储库克隆为一个Mercurial存储库。它是Mercurial公司提供的。它的工作原理大致如下:

hg convert <Subversion URL or directory> <path to new Mercurial repository>

例如,这将获取SixApart memcached存储库的主干。

hg convert http://code.sixapart.com/svn/memcached/trunk

扩展可以增量地将新修订从Subversion存储库引入Mercurial存储库(有点像pull)。但是,它不支持获取Mercurial的修订并将它们发送回Subversion(不推送)。[XXX:如有错误请纠正]。


The hgsubversion extension. It is in many ways the most sophisticated solution as it uses the Subversion API to communicate with the Subversion repository. It aims to become the hg-svn bridge. It allow full round-tripping of revisions (full clone, pull, and push), However as of this writing [XXX: Amend this if/when it becomes incorrect] it is still in development and there are not yet official releases. As a consequence it works with only the most up-to-date Mercurial (1.3 as of this writing).

It maps tags and branches (preceding all tags with tags/ to distinguish them from equivalently named branches). It maintains a special branch closed-branches for closing off branches which are removed in Subversion. It requires that the Subversion repository be laid out according to the convention of trunk/branches/tags. The command set is typically hg svn <subcommand> though it aims at being integrated to the point that you don't need the 'svn' part (i.e. it wants to treat a Subversion clone as much as possible like any other Mercurial repository).;

它是这样工作的:

克隆:

hg svnclone <Subversion URL> 

OR(仅适用于svn:// url)

hg clone <svn:// URL>

拉:

hg svn pull

推动:

hg svn push

输入:

hg svn incoming

输出:

hg svn outgoing

检查整个存储库:

hg svnclone http://code.sixapart.com/svn/memcached

hgsvn实用程序(位桶树)。直到最近,这只允许您克隆和提取Subversion存储库,但从hgsvn 0.1.7开始,它支持推送。我不知道它推力有多大。任何有经验的人都应该更新这个。它有以下显著特点:

It generates a Mercurial tag for every SVN tag. It puts a local tag on every changeset to mark its SVN revision. It puts every Mercurial revision on a named branch named after its SVN branch. For example branches/some-feature would be like hg branch some-feature. It puts the trunk on trunk (i.e. nothing is on the Mercurial default branch, unless the user explicitly switches to it.) It will try to identify branches and tags, and create them but if it can't it just skips them. This is handy when the Subversion repository is not following the conventional trunk/branches/tags layout.

它是这样工作的:

克隆:

hgimportsvn <Subversion URL>

拉:

hgpullsvn

推动:

hgpushsvn

输入:

hgpullsvn -n

输出:

hgpushsvn -n

检查整个存储库:

hgimportsvn http://code.sixapart.com/svn/memcached

检查只是后备箱:

hgimportsvn http://code.sixapart.com/svn/memcached/trunk

好的GUI/IDE插件Mercurial?

GUI

TortoiseHg for just about any OS. Includes Windows Explorer integration. It also works in Linux and a few other OS:es including Max OS X. It has a somewhat clunky interface and is a little awkard to use at first, but it is very complete and powerful. Murky runs on Mac OS X 10.5 or later. Murky is good for exploring the repository and basic commands, but you will need to know how to use the command line as well. MacHg is a nice Mac OS X Gui that has a little more functionality and polish than Murky, but you will still need the command line with it as well. SourceTree is a Mac client originally, with a Windows version available just recently. Pretty nice UI (at least on OS X), supports majority of Hg features, including shelve.

插件

VisualHG for Visual Studio HgSccPackage for Visual Studio 2008/2010 mercuraleclipse for Eclipse 对NetBeans的Mercurial支持 Mercurial对Sublime Text的支持

如何合并分支?

$ cd repository-where-i-want-to merge
$ hg pull branch-i-want-to-merge
$ hg merge # if necessary