Mercurial 快速入门

优质
小牛编辑
121浏览
2023-12-01

(参见 理解Mercurial 和 Mercurial教程)

1. 设置用户名

默认情况下,Mercurial 使用 user@localhost 格式作为commits时的用户名。 这样做通常无任何意义。您最好能在~/.hgrc(在Windows系统中,是%USERPROFILE%\Mercurial.ini)中加入合适的email地址。如下所示:

[ui]
username = John Doe <john@example.com>

2. 在现有的 Mercurial 项目中工作

如果你有一个URL支持的项目版本库 (例如 https://selenic.com/hg), 你可以用下面的方法抓取一个副本。 (参见 Mercurial教程:克隆仓库)

$ hg clone https://selenic.com/hg mercurial-repo
real URL is https://www.selenic.com/hg/
requesting all changes
adding changesets
adding manifests
adding file changes
added 6908 changesets with 13429 changes to 976 files
updating working directory
861 files updated, 0 files merged, 0 files removed, 0 files unresolved

这会建立一个 Mercurial仓库的新目录, 复制整个项目的历史记录并检出最新版

查看哪一个revision被checked out:

$ cd mercurial-repo
$ hg parents
changeset:   6907:6dcbe191a9b5
tag:         tip
user:        Maem Mackall <mpm@selenic.com>
date:        Mon Aug 18 16:50:36 2008 -0500
summary:     Fix up tests

3. 建立新的Mercurial项目

你希望从创建一个仓库开始:

$ cd project/
$ hg init           # 创建 .hg

Mercurial 将会在您repository的根目录查找名为.hgignore的文件,该文件用于设定需要忽略的文件或目录。以下是一个.hgignore文件的例子:

syntax: glob
*.orig
*.rej
*~
*.o
tests/*.err

syntax: regexp
.*\#.*\#$

以下代码测试.hgignore文件的效果:

$ hg status         # 显示所有没有被忽略的差异文件

这将会列出所有没有被忽略的文件。这些文件名前带有'?'号(表示mercurial没有跟踪它们)。 编辑你的.hgignore 文件直到status中只列出了你想要的文件为止。你需要将.hgignore文件添加到版本库,建议忽略自动生成的文件,如日志文件,缓存文件等

$ hg add            # add those 'unknown' files
$ hg commit         # commit all changes, edit changelog entry
$ hg parents        # see the currently checked out revision

4. 克隆仓库, 提交, 合并

$ hg clone project project-work    # clone repository
$ cd project-work
$ <make changes>
$ hg commit
$ cd ../project
$ <make other changes>
$ hg commit
$ hg pull ../project-work   # pull changesets from project-work
$ hg merge                  # merge the new tip from project-work into our working directory
$ hg parents                # see the revisions that have been merged into the working directory
$ hg commit                 # commit the result of the merge

(参见 Mercurial教程:合并改变)

5. 导出补丁

(make changes)
$ hg commit
$ hg export tip    # export the most recent commit

(参见 Mercurial教程:导出变更集)

6. 网络支持

# clone from the primary Mercurial repo
foo$ hg clone https://selenic.com/hg/
foo$ cd hg

# update an existing repo
foo$ hg pull https://selenic.com/hg/

# export your current repo via https with browsable interface
foo$ hg serve -n "My repo" -p 80

# push changes to a remote repo with SSH
foo$ hg push ssh://user@example.com/hg/