SlideShare a Scribd company logo
版本控制 - Mercurial
A quick introduction
Vincent CY Liao
2010-06-06, revised at 2014-01-19

1
Outline
■ 序論
●

什麼是 Mercurial ?

●

Mercurial vs. Subversion

■ 使用 Mercurial
●
●

安裝與首次設定
日常使用
■
■
■

建立檔案庫
編修檔案
與其他成員合作

2
序論
■ 什麼是 Mercurial ?
■ Mercurial vs. Subversion

3
什麼是 Mercurial ?
■ 分散式版本控制系統 (Distributed Version Control System, DVCS)
● 大部份作業的進行不需網路
● 使開發能夠較為獨立不受影響
■ 發展現況
●

活躍的發展中
■

●

在業界已廣泛使用
■

●

最新的版本 : 2.8.2 (Jan. 1, 2014)
Google, Sun Oracle, Mozilla …

http://mercurial.selenic.com/

支援多種作業系統
■

AIX, Darwin, FreeBSD, Linux, Solaris, Windows ...
4
Mercurial vs. Subversion
branch
repo

pull/push

clone

repo

commit

clone

checkout

clone

checkout
checkout

update
repo

repo

work
copy

work
copy

commit

work
copy

repo

Mercurial

Subversion
5
Mercurial vs. Subversion (cont.)
main
repo

repo

push
commit:
bug
pull

repo
commit:
bug

update

commit:
bug-fix

work
copy
repo

work
copy
取得尚有問
題的版本

一次取得修
正好的版本
Mercurial

Subversion
( 在 commit 時無需連線主檔案庫 )

6
Mercurial vs. Subversion (cont.)
Network-1

Network-1

repo

clone

work
copy

repo
commit

Mobile Storage

repo
commit

pull+merge

repo
Network-2

work
copy
Network-2

Mercurial

Subversion
( 可在分離的網路使用 )

7
使用 Mercurial
■ 安裝與首次設定
● 安裝流程概觀
● 取得軟體
● 建置與安裝
● 首次設定

8
安裝流程概觀
■ 前置要件
●

Python 2.4+

■ 安裝流程
● 取得軟體
● 安裝 Python
■
■

●

若系統中沒有 Python 或是版本太舊才需要進行這步驟
使用 python -V 指令檢視系統的 Python 版本

安裝 Mercurial

9
取得軟體 (Python)
http://www.python.org/download/

10
取得軟體 (Mercurial)
http://mercurial.selenic.com/downloads/

11
建置與安裝
■ 準備安裝目錄
cd ~
mkdir bin

■ 安裝 Python ( 如需要 )
mkdir tmp
cd tmp
tar -jxf ~/Python-2.7.5.tar.bz2
./configure –prefix=/home/USER/bin/python-2.7
make
make install
cd ~/bin
ln -s python-2.7/bin/python .
./python --version
export PATH=~/bin:$PATH

12
建置與安裝 (cont.)
■ 安裝 Mercurial
cd tmp; tar -jxf ~/mercurial-2.6.3.tar.gz
cd mercurial-2.6.3/
make PREFIX=/home/USER/bin/mercurial install
cp contrib/hgk ~/bin/mercurial/bin
cd ~/bin
ln -s mercurial/bin/hg . ; ln -s mercurial/bin/hgk .

13
首次設定
■ 編輯設定檔 ~/.hgrc
●

也可針對 repository 有不同的設定
■

%REPO%/.hg/hgrc

[ui]
username = Alicia <alicia@alpha.local>
[extensions]
hgk =
record =

14
使用 Mercurial
■ 日常使用
● 建立檔案庫
● 檔案新增、更名 / 搬移、刪除
● 檢視與送交變更
● 忽略檔案
● 合併其他成員的變更
■
■

單一開發分支的合併
多個開發分支的合併

15
使用 Mercurial
■ 日常使用 (cont.)
●
●

解決變更衝突
檢視紀錄

16
建立檔案庫
■ 建立檔案庫
●

mkdir proj

●

cd proj/

●

hg init
■

在 proj/ 資料夾內會出現存放版本控制資訊的 .hg/ 資料夾

■ 從主檔案庫建立開發分支
●

hg clone /home/dev/proj proj-my-workcopy

■ 建立檔案庫只需在專案開始時進行一次

17
檔案新增、更名 / 搬移、刪除
■ 檔案新增
●

hg add FILE.name

■ 檔案更名或搬移
●

hg mv OLDFILE.name NEWFILE.name

■ 檔案刪除
●

hg rm FILE.name

■ 檔案補刪除
●

hg rm -A FILE.name

18
檢視與送交變更
■ 檢視變更狀況
●

hg st

■ 送交變更
●

hg ci [FILE]
■
■

整個工作資料夾的所有變更都會被送入
要送交部份變更可指明檔名
commit
.hg/

19
忽略檔案
■ 可透過 .hgignore 檔指定要忽略的檔案
●

指定的檔案不會在 hg st 的輸出中顯示

syntax: glob
*.o
a.out
Doxy
doxy-doc
*~
.DS_Store
*.a
src/product
src/executable

20
合併其他成員的變更
■ 從其他檔案庫取得變更
●

hg incoming PATH_TO_OTHER_REPO

●

hg pull PATH_TO_OTHER_REPO

■ 將變更反應到檔案上
●

hg update

●

hg merge
pull

.hg/

update / merge

.hg/

other-repo
21
單一開發分支的合併
2:e1

2:e1

1:f7

1:f7

0:ab

clone

2:e1

0:ab

3:ca

2:e1

編輯
1:f7

1:f7

0:ab

0:ab

22
單一開發分支的合併 (cont.)
3:ca

2:e1

1:f7

0:ab

3:ca

2:e1

2:e1

1:f7

3:ca
pull
update

2:e1

合併
1:f7

0:ab

0:ab

1:f7

0:ab

23
多個開發分支的合併
2:e1

1:f7
2:e1

clone

3:b9

2:e1

0:ab

1:f7

1:f7

編輯

3:ca
0:ab

0:ab

clone

2:e1

2:e1

1:f7

1:f7

0:ab

0:ab
24
多個開發分支的合併 (cont.)
3:b9

5:df

2:e1

4:ca

合併
0:ab

2:e1

1:f7

0:ab

3:b9

3:b9

1:f7
3:ca

4:ca

3:ca
2:e1
2:e1

pull
1:f7

1:f7
0:ab

merge
commit

2:e1

1:f7

0:ab

0:ab
25
解決變更衝突
■ 變更衝突的起因
● 要合併的變更與本地端的變更有所重疊
■ 解決變更衝突
●
●
●

Mercurial 會將雙方的變更都留在檔案內
協調後手動修改
修改完成後將檔案標示為已解決衝突

■ 標示檔案為已解決衝突
●

hg resolve -m FILE.name

26
檢視紀錄
■ 文字模式
●

hg log [FILE]

■ 視窗模式
●

hg view
■

系統上須安裝有 Tcl/Tk

■ 網頁模式
●

hg serve -a 127.0.0.1 -p 8000
■
■

到瀏覽器上透過 http://127.0.0.1:8000/ 作為 URL 連線
未給參數的話預設是啟動在所有介面上的 8000 埠
▸

■

有安全顧慮的話最好指定 -a 參數指定介面

按下 CTRL+C 終止
27
檢視紀錄 (cont.)

28
檢視紀錄 (cont.)

29
小撇步
■ 推送變更到多臺主機
■ 送交部份變更
■ 合併兩個獨立的檔案庫

30
推送變更到多臺主機
■ 可以一定程度的作為佈署的手段
■ 增加 paths 設定到檔案庫的設定中
●

%REPO%/.hg/hgrc

●

hg push [PATH_NAME]

[paths]
default
dev01 =
dev02 =
pubserv

= ssh://ira@192.168.3.29/project-1
ssh://devuser@192.168.10.8/repo/project-1
ssh://devuser@192.168.10.9/repo/project-1
= http://pubacc@repo.serv.net/project/my-project-1

31
送交部份變更
■ 送交部份變更
●

hg record [FILE]
■

用在一次的程式碼變更內含兩個以上修改主題之場合
▸
▸
▸
▸

■
■

一次修了好幾個 bug
一次加了好幾個 feature
順便做了 re-factor
順便更新了文件

相當於 hg commit
會提示選擇要加入的變更

■ 需要在設定檔中啓動這個功能
[extensions]
record =
32
送交部份變更 (cont.)
$ hg record

diff --git a/hello.txt b/hello.txt
2 hunks, 5 lines changed

examine changes to 'hello.txt'? [Ynesfdaq?] y
@@ -4,11 +4,11 @@
So she was considering in her own mind (as well as she could, for the hot day
-made her feel very sleepy and stupid), whether the pleasure of making a
-daisy-chain would be worth the trouble of getting up and picking the daisies,
+made her feel very sleepy and stupid), whether the pleasure of making a daisy+chain would be worth the trouble of getting up and picking the daisies,
when suddenly a White Rabbit with pink eyes ran close by her.

record change 1/3 to 'hello.txt'? [Ynesfdaq?] y

@@ -9,12 +9,12 @@
she ought to have wondered at this, but at the time it all seemed quite
-natural); but when the Rabbit actually TOOK A WATCH OUT OF ITS
+natural); but when the Rabbit actually TOOK A WATCH OUT OF ITS WAISTCOAT...

record change 2/3 to 'hello.txt'? [Ynesfdaq?] s
diff --git a/world.txt b/world.txt
1 hunks, 1 lines changed

examine changes to 'world.txt'? [Ynesfdaq?] y
@@ -1,2 +1,2 @@
-An apple a day keeps the doctors away.
+An Apple A Day Keeps the Doctors Away.

record change 3/3 to 'world.txt'? [Ynesfdaq?] y
33
送交部份變更 (cont.)
■ 動作選項
●

Y: 紀錄這個變更

●

N: 略過這個變更

●

E: 手動編輯這個變更

●

S: 目前檢視的檔案中,略過這個以下 ( 含 ) 的變更

●

F: 目前檢視的檔案中,紀錄這個以下 ( 含 ) 的變更

●

D: 完成,後面的變更與檔案都略過

●

A: 紀錄後面所有檔案的所有變更

●

Q: 離開,不紀錄任何變更

●

?: 顯示說明訊息
34
合併兩個獨立的檔案庫
■ 偶而會發生
● 合併兩個原本不相干的專案
● 在新的檔案庫開相對獨立子功能之更新版本
■
■

不建議這麼做
建議直接 clone 原本的來進行進一步開發

■ 進行合併前建議先調整兩個檔案庫內的檔案配置
●

降低合併 (merge) 時發生衝突 (conflict) 的機會

+

=

+

=

35
合併兩個獨立的檔案庫 (cont.)
■ 直接使用 hg pull 取得變更集時會被拒絕
●

“abort: repository is unrelated”

■ 要加上強制拉取選項
●

hg pull -f [TARGET_REPO]

●

後續動作跟一般的 pull-merge 流程相同

36
其他細節
■ 個人識別名稱
■ 共用帳號

37
個人識別名稱
■ 目前常見的有兩種
● 以個人名義為主
■

●

My Name <email@address.tld>

附加公司簡稱
■

My Name, CORP. <email@address.tld>

■ 在開放專案中會看到第一種
●
●

假設合作的人都會看 E-Mail 位址來辨識所屬組織
個人色彩鮮明

■ 公司專案就比較不一定
■ 總之,選一個寫法然後就避免變動
38
共用帳號
■ 在開發中有時多個開發者得共用主機帳號
● 特別是測試或驗證主機
■ 一般來說建議避免把個人化設定放到共用帳號裡
●

常見的有環境變數、編輯器設定、版控設定等
■

●

~/.bashrc, ~/.profile, ~/.vimrc, ~/.hgrc … etc.

避免讓變數非預設值而影響其他人甚至影響程式運作
■

有些設定要怎麼設基本上是信仰的問題

■ 透過建立個人的環境變數檔來兼顧
●
●

利用 alias 改變預設指令的傳遞參數
大部份程式都有參數可以改變預設讀取的設定檔
39
共用帳號 (cont.)
■ 建立 ~/.myrc-myaccount 指令稿
●

其他設定檔可建立 ~/.myrc-myaccount-data/ 資料夾
來存放

■ 登入時匯入即可完成執行時期客制化
●

. ~/.myrc-myaccount

●

source ~/.myrc-myaccount

alias hg=”hg –config 'ui.username=Vincent CY Liao, MyCO <v...@company.com>'”
alias vi='vim -u ~/.myrc-vincentl-data/vimrc'
export EDITOR=/usr/bin/vim
export PATH=/opt/mercurial-2.4/bin:$PATH
40
Thank you !

41

More Related Content

ODP
The New Process No. 1 of Linux -- SystemD
PDF
Git Tutorial
PPT
Cassandra运维之道(office2003)
PDF
Linux基础
PPTX
使用Rpm&yum进行基础软件管理
PPT
Git 超簡單學習懶人包(軟體程式版本控管系統)
PDF
Tiptop gp 5.1 setup_instructions
ODP
Git basis - usage
The New Process No. 1 of Linux -- SystemD
Git Tutorial
Cassandra运维之道(office2003)
Linux基础
使用Rpm&yum进行基础软件管理
Git 超簡單學習懶人包(軟體程式版本控管系統)
Tiptop gp 5.1 setup_instructions
Git basis - usage

What's hot (17)

PPTX
5, system admin
PDF
Clonezilla tutorial.v7
PDF
Install Oracle11g For Aix 5 L
PDF
Showinnodbstatus公开
PPTX
Cassandra运维之道
PDF
Linux chapt3
PDF
Heartbeat+my sql+drbd构建高可用mysql方案
PDF
PostgreSQL 9 1 新特性
ODP
Git 教學
PPT
Hadoop平台搭建
PPTX
MySQL aio
PPT
Mysql展示功能与源码对应
DOC
尚观Linux研究室 linux驱动程序全解析
PDF
Infiniflash benchmark
PDF
OpenWRT Case Study
PDF
Asm+aix
PDF
unixtoolbox_zh_CN
5, system admin
Clonezilla tutorial.v7
Install Oracle11g For Aix 5 L
Showinnodbstatus公开
Cassandra运维之道
Linux chapt3
Heartbeat+my sql+drbd构建高可用mysql方案
PostgreSQL 9 1 新特性
Git 教學
Hadoop平台搭建
MySQL aio
Mysql展示功能与源码对应
尚观Linux研究室 linux驱动程序全解析
Infiniflash benchmark
OpenWRT Case Study
Asm+aix
unixtoolbox_zh_CN
Ad

Similar to 版本控制 - Mercurial (20)

PDF
版本控制 使用Git & git hub
PDF
Git in a nutshell
PDF
Git 簡介(古時候的簡報備份)
PPTX
Git 入門與實作
PDF
Git 入门实战
PDF
Git+使用教程
PPTX
Git内部培训文档
PDF
Learning to Use Git | WeiYuan
PPTX
Git前世今生
PPTX
Git Essence Tutorial
PPTX
Git 使用介绍
PDF
Introduction to git
PDF
Git 版本控制系統 -- 從微觀到宏觀
PPTX
使用 Visual Studio Code 建構 JavaScript 應用程式
PDF
Git tutorial for windows user (給 Windows user 的 Git 教學)
PDF
Git Tutorial 教學
ODP
Git 程式碼版本控制軟體介紹
PPT
Subversion
PPTX
Git & git flow
PPT
Learn git
版本控制 使用Git & git hub
Git in a nutshell
Git 簡介(古時候的簡報備份)
Git 入門與實作
Git 入门实战
Git+使用教程
Git内部培训文档
Learning to Use Git | WeiYuan
Git前世今生
Git Essence Tutorial
Git 使用介绍
Introduction to git
Git 版本控制系統 -- 從微觀到宏觀
使用 Visual Studio Code 建構 JavaScript 應用程式
Git tutorial for windows user (給 Windows user 的 Git 教學)
Git Tutorial 教學
Git 程式碼版本控制軟體介紹
Subversion
Git & git flow
Learn git
Ad

版本控制 - Mercurial