Bitbucket + Mercurial


虽说GitHub和git是现在应用最广泛的代码托管平台和版本控制工具,但是对于Bitbucket和Mercurial (hg)仍有一定市场份额。特别是对小开发团队来说,hg有其吸引人的地方。
https://blog.csdn.net/photon222/article/details/90646679

本文讲以Bitbucket + Mercurial为例子,详细介绍他们的用法

Bitbucket

Bitbucket就不多介绍了,可参考下面两篇文章,注册账号、新建仓库等:
https://blog.csdn.net/hk2291976/article/details/42292427
https://blog.csdn.net/gao_fei1129/article/details/52682324

Mercurial

Mercurial是一种免费的分布式源代码管理工具。它为您提供了在使用直观界面的同时有效处理任何规模项目的能力。它很容易使用,也很难断开,这使得它非常适合任何使用版本化文件的人。

Mercurial中文的意思是水银,化学符号为hg,所以它的命令为hg, 类似于git。

1. 安装及基本配置

官方网站:http://mercurial.selenic.com/wiki/
下载下来解压缩以后,里面有个README文件,打开以后按照说明来:
$ make
$ make install
$ hg debuginstall
$ hg
安装之后执行hg version输出,表示安装成功

$ hg version
Mercurial Distributed SCM (version 3.1.1)
(see http://mercurial.selenic.com for more information)

Copyright (C) 2005-2014 Matt Mackall and others
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

hg的配置文件在~/.hgrc, 配置文件中设置username等信息

# This is a Mercurial configuration file.
[ui]
username = Firstname Lastname <email.address@example.net>
2. hg help

hg help 列出mercurial 所有commands.
如果想查看某个hg 命令的帮助文档, hg help somecmd 将列出其简介和用法,如

$ hg help init
hg init [-e CMD] [--remotecmd CMD] [DEST]

create a new repository in the given directory

    Initialize a new repository in the given directory. If the given directory
    does not exist, it will be created.

    If no directory is given, the current directory is used.

    It is possible to specify an "ssh://" URL as the destination. See "hg help
    urls" for more information.

    Returns 0 on success.

options:

 -e --ssh CMD       specify ssh command to use
    --remotecmd CMD specify hg command to run on the remote side
    --insecure      do not verify server certificate (ignoring web.cacerts
                    config)
    --mq            operate on patch repository

use "hg -v help init" to show the global options
3. repository

repository意思是仓库,存放代码的仓库,分为local本地仓库和remote远程仓库。 hg 命令分为两种:一种是本地命令, 用于管理本地代码;另一种是网络操作, 用于将本地的改变发送到其他仓库,或者从其他仓库下载别人的改变。

新建仓库的方法有两种:

一种是本地初始化(initialize)一个仓库

hg init myproject 

#将创建一个新的目录myproject并初始化它。如果是已存在的目录,可以先cd 进去,在执行hg init即可。

另外一种方法是从其他仓库clone一个

hg clone [OPTION]... SOURCE [DEST]

SOURCE可以是一个ssh或http网址等,DEST是目标, 可以给这个repo重命名。 如果对方的repo是私有的,你还要被允许(ssh_key等)才能clone. 也可以从其他机器上拷贝,如

hg clone ssh://<username>@<hostname>/<repopath>

注意ssh:后是两个//, 包括repopath的一个/, hostname后也是两个//。

repo的目录下包含一个.hg的隐藏目录,该目录下保存这个repo的私有数据,如包含哪些文件,修改内容,每次提交信息等。 如果是 hg clone下的,在.hg/hgrc配置文件里,包含对应远端repo的path,这样hg push时,hg就知道向哪里推送。

[paths]
default = <remote_repopath>

具体其他配置可以hg help config命令查看

4. 向repo添加内容

假如我们在myproject目录下添加了一个文件hello.c:

int main()
{
    printf("hello world!\n");
}

使用hg statushg st for short)可查看当前repo下有哪些变化

hg st
? hello.c
The codes used to show the status of files are:

      M = modified 修改
      A = added 已添加至repo
      R = removed 
      C = clean
      ! = missing (deleted by non-hg command, but still tracked)
      ? = not tracked 新增,未添加到repo
      I = ignored
        = origin of the previous file (with --copies)

该文件还未被追踪,他的任何修改hg是无法进行版本控制的,使用hg add通知Mercurial追踪该文件

$ hg st
A hello.c

hg add 可一次添加多个文件,只要后面接上文件名即可。如要要添加所有文件hg add .即可, 其反向操作为hg forget.

在添加一个文件hello.h, 并修改hello.c, 并add.

使用hg diff查看做了哪些修改(默认当前与上一次commit版本之间比较)

$ hg diff
diff -r 000000000000 hello.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hello.c	Wed May 29 21:22:24 2019 +0800
@@ -0,0 +1,7 @@
+#include <hello.h>
+
+int main()
+{
+    printf("hello world!\n");
+}
+
diff -r 000000000000 hello.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hello.h	Wed May 29 21:22:24 2019 +0800
@@ -0,0 +1,1 @@
+include <stdio.h>

使用hg commit提交一个版本,-m 后跟上提交信息。以后无论做了多少修改,提交了多少次,都是可以恢复到这个版本的。

$ hg commit -m "first commit"

假如提交了一个版本后,需要修改hello.c文件,修改后使用hg status查看当前reop的状态,使用hg diff查看修改内容

$ hg status
M hello.c

$ hg diff
diff -r b3e3d75272f2 hello.c
--- a/hello.c	Wed May 29 21:25:33 2019 +0800
+++ b/hello.c	Wed May 29 21:28:48 2019 +0800
@@ -2,6 +2,6 @@
 
 int main()
 {
-    printf("hello world!\n");
+    printf("goodbye world!\n");
 }
 
5. 历史版本查看

假如之后又修改了几个版本, 使用hg summary查看repo当前的总体状态,使用hg log查看日志

$ hg summary
parent: 2:ef7aaa238086 tip
 let it say something
branch: default
commit: 1 modified, 1 unknown
update: (current)

$ hg log
changeset:   2:ef7aaa238086
tag:         tip
user:        photon <photon@csdn.com>
date:        Wed Jun 05 20:51:42 2019 +0800
summary:     let it say something

changeset:   1:13a0159ba210
user:        photon <photon@csdn.com>
date:        Wed Jun 05 20:43:16 2019 +0800
summary:     second commit

changeset:   0:b3e3d75272f2
user:        photon <photon@csdn.com>
date:        Wed May 29 21:25:33 2019 +0800
summary:     first commit

hg log输出的具体含义为
changest: 第一个数字是本地版本号, 一般连续递增,方便在本地使用, 但可能在不同机器上不对应; 第二个字符串是版本的唯一标志,即使克隆或push到其他机器上也不会变。 注意: changeset的中文意思是变更集,和revision修改是一个意思,所以hg很多命令的参数 -r (–rev) 即是指changeset, -r后面可以跟这里的数字或者字符串。
user: 该版本的提交者
date: 提交日期和时间
summary: commit的时候 -m 参数后面的commit message

hg log -v (–verbose) 还可以展示更详细的日志信息,

$ hg log -v -r 2
changeset:   2:ef7aaa238086
tag:         tip
user:        photon <photon@csdn.com>
date:        Wed Jun 05 20:51:42 2019 +0800
files:       hello.c hello.h saysth.c
description:
let it say something

hg log -p (-p) 给出版本的修改内容, 类似于diff

$ hg log -v -r 1 -p
changeset:   1:13a0159ba210
user:        photon <photon@csdn.com>
date:        Wed Jun 05 20:43:16 2019 +0800
files:       hello.c
description:
second commit


diff -r b3e3d75272f2 -r 13a0159ba210 hello.c
--- a/hello.c	Wed May 29 21:25:33 2019 +0800
+++ b/hello.c	Wed Jun 05 20:43:16 2019 +0800
@@ -2,6 +2,6 @@
 
 int main()
 {
-    printf("hello world!\n");
+    printf("goodbye world!\n");
 }

命令参数可以合并起来,如:hg log -r 1 -vp

6. 版本共享

以上是一个开发者一个repo的版本控制,如果有很多开发者,需要在多个repo之间分享版本修订怎么办?
比如我们在本地clone两个repo, clone的用法见第3节:

$ hg clone myproject/ myproject1
updating to branch default
3 files updated, 0 files merged, 0 files removed, 0 files unresolved
$ ls
myproject  myproject1 

继续在myproject上修改,提交一个版本:changeset: 3:8563ab5842ff, 这时候在myporject1上看不到我的修改的。 使用hg pull把其他myporject的修改拉到myproject1。在此之前可以先使用hg incoming查看哪些changeset会被pull到这个repo, hg incoming并不会真正把修改内容pull过来。

$  cd ../myproject1
$ hg incoming ../myproject
comparing with ../myproject
searching for changes
changeset:   3:8563ab5842ff
tag:         tip
user:        photon <photon@csdn.com>
date:        Wed Jun 05 21:33:10 2019 +0800
summary:     say how do you do

$ hg pull ../myproject
pulling from ../myproject
searching for changes
adding changesets
adding manifests
adding file changes
added 1 changesets with 2 changes to 2 files
(run 'hg update' to get a working copy)

在pull之后我们查看当前目录下的文件,发现并没有改变。 这是因为,hg pull默认并不改变工作目录, 现在的工作目录我们可以使用hg parents查看:

$ hg parents
changeset:   2:ef7aaa238086
user:        photon <photon@csdn.com>
date:        Wed Jun 05 20:51:42 2019 +0800
summary:     let it say something

或者使用hg log -G查看,带@的版本为当前工作目录:

$ hg log -G
o  changeset:   3:8563ab5842ff
|  tag:         tip
|  user:        photon <photon@csdn.com>
|  date:        Wed Jun 05 21:33:10 2019 +0800
|  summary:     say how do you do
|
@  changeset:   2:ef7aaa238086
|  user:        photon <photon@csdn.com>
|  date:        Wed Jun 05 20:51:42 2019 +0800
|  summary:     let it say something
|
o  changeset:   1:13a0159ba210
|  user:        photon <photon@csdn.com>
|  date:        Wed Jun 05 20:43:16 2019 +0800
|  summary:     second commit
|
o  changeset:   0:b3e3d75272f2
   user:        photon <photon@csdn.com>
   date:        Wed May 29 21:25:33 2019 +0800
   summary:     first commit

使用hg update更新工作目录

$ hg update 3
2 files updated, 0 files merged, 0 files removed, 0 files unresolved

以上是从别人的repo里pull变更,相反也可以讲本地变更push给别的repo。例如我们在myproject1里修改并提交了新的版本, 为了防止冲突,修改了hello.h文件。然后push给myproject, 同样可以使用hg outgoing预览哪些变更将被push:

$ hg outgoing ../myproject
comparing with ../myproject
searching for changes
changeset:   4:de0cb67ea28c
tag:         tip
user:        photon <photon@csdn.com>
date:        Wed Jun 05 22:07:14 2019 +0800
summary:     nice to meet you

$ hg push ../myproject
pushing to ../myproject
searching for changes
adding changesets
adding manifests
adding file changes
added 1 changesets with 1 changes to 1 files

同样,这时候我们回到myproject中也看不要文件内容变化,也是工作目录的问题,使用hg update更新工作目录即可。

其实,上面pull push的时候如果不指定repo, 默认的repo也会是myproject, 因为hg clone时.hg/hgrc中就记下了默认的repo, 如果想修改也是可以的。

[paths]
default = <repo path>

Reference:
https://www.oschina.net/question/17_4318
https://book.mercurial-scm.org/

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值