這個叫水銀的源碼管理工具雖然默默無聞,但還是得到了很多團隊的使用。為了迎合某些團隊的需要,我們也要用它來管理我們的代碼。
今天的任務是先突擊學習,磨刀不誤砍柴工。對工具的掌握越快,工作的效率就會越高。
#### 1.安裝
首先從[官網](http://mercurial.selenic.com/)下載最新的版本,我這次做個實驗,下載了3.2-rc。
解壓到你指定的目錄下:
~~~
[linc@localhost?mercurial]$?ls??
mercurial-3.2-rc.tar.gz??
[linc@localhost?mercurial]$?tar?xzvf?mercurial-3.2-rc.tar.gz???
~~~
安裝之前要檢查一下是否已經安裝了python-dev,這在fedora下叫作python-devel, 如果沒有,編譯時會出現錯誤:
~~~
mercurial/base85.c:13:20:?fatal?error:?Python.h:?No?such?file?or?directory??
~~~
只要看看/usr/include/python2.7/下有沒有上述的頭文件就知曉了。
安裝也好辦,ubuntu下使用:
` sudo?apt-get?install?python-dev??`
fedora下使用:
` [linc@localhost?etc]$?sudo?yum?install?python-devel??`
來到mercurial-3.2-rc路徑下,執行:
~~~
[linc@localhost?mercurial-3.2-rc]$?make?install-home??
python?setup.py??build???
running?build??
running?build_mo??
running?build_ext??
building?'mercurial.base85'?extension??
...??
~~~
~~~
make[1]:?***?[hg.1]?Error?255??
make[1]:?Leaving?directory?`/home/linc/dev/mercurial/mercurial-3.2-rc/doc'??
make:?***?[doc]?Error?2??
~~~
最后的錯誤是關于文檔的,這里被我無視了,嘗試執行hg,得到了反饋,說明基本功能是安裝完成了。
~~~
[linc@localhost?mercurial-3.2-rc]$?hg??
Mercurial?Distributed?SCM??
??
basic?commands:??
??
?add???????????add?the?specified?files?on?the?next?commit??
?annotate??????show?changeset?information?by?line?for?each?file??
?clone?????????make?a?copy?of?an?existing?repository??
?commit????????commit?the?specified?files?or?all?outstanding?changes??
?diff??????????diff?repository?(or?selected?files)??
?export????????dump?the?header?and?diffs?for?one?or?more?changesets??
?forget????????forget?the?specified?files?on?the?next?commit??
?init??????????create?a?new?repository?in?the?given?directory??
?log???????????show?revision?history?of?entire?repository?or?files??
?merge?????????merge?working?directory?with?another?revision??
?pull??????????pull?changes?from?the?specified?source??
?push??????????push?changes?to?the?specified?destination??
?remove????????remove?the?specified?files?on?the?next?commit??
?serve?????????start?stand-alone?webserver??
?status????????show?changed?files?in?the?working?directory??
?summary???????summarize?working?directory?state??
?update????????update?working?directory?(or?switch?revisions)??
??
(use?"hg?help"?for?the?full?list?of?commands?or?"hg?-v"?for?details)??
~~~
2.簡單的使用
下面就要試著創建一個項目并提交代碼。
創建init目錄:
~~~
[linc@localhost?testHG]$?hg?init?testMercurial??
[linc@localhost?testHG]$?ls??
testMercurial??
[linc@localhost?testHG]$?cd?testMercurial/??
~~~
添加我的小項目,將其他目錄下的源碼拷到這里:
~~~
[linc@localhost?testMercurial]$?cp?-r?../../hello/*?.??
[linc@localhost?testMercurial]$?ls??
Debug??Release??src??
~~~
查看當前狀態:
~~~
[linc@localhost?testMercurial]$?hg?status??
??Debug/makefile??
??Debug/objects.mk??
??Debug/sources.mk??
??Debug/src/subdir.mk??
??Release/makefile??
??Release/objects.mk??
??Release/sources.mk??
??Release/src/subdir.mk??
??src/hello.c??
~~~
問號表示還沒有納入版本管理,下面就給它們添加進來:
~~~
[linc@localhost?testMercurial]$?hg?add??
adding?Debug/makefile??
adding?Debug/objects.mk??
adding?Debug/sources.mk??
adding?Debug/src/subdir.mk??
adding?Release/makefile??
adding?Release/objects.mk??
adding?Release/sources.mk??
adding?Release/src/subdir.mk??
adding?src/hello.c??
[linc@localhost?testMercurial]$?hg?status??
A?Debug/makefile??
A?Debug/objects.mk??
A?Debug/sources.mk??
A?Debug/src/subdir.mk??
A?Release/makefile??
A?Release/objects.mk??
A?Release/sources.mk??
A?Release/src/subdir.mk??
A?src/hello.c??
~~~
A就是add的標識了。下面我們提交這些代碼吧。
~~~
[linc@localhost?testMercurial]$?hg?commit?-m?'initial?commit'??
abort:?no?username?supplied??
(use?"hg?config?--edit"?to?set?your?username)??
[linc@localhost?testMercurial]$?hg?config?--edit??
~~~
可惜由于我沒有編輯好我的名稱在配置文件中,先去添加一下,然后繼續提交代碼:
~~~
[linc@localhost?testMercurial]$?hg?commit?-m?'initial?commit'??
[linc@localhost?testMercurial]$?hg?status??
[linc@localhost?testMercurial]$?hg?log??
changeset:???0:23ac3f5bfc59??
tag:?????????tip??
user:????????Lincoln???
date:????????Mon?Oct?27?21:05:10?2014?+0800??
summary:?????initial?commit??
~~~
上面的changeset就是git中的commit id,冒號前面的0就是你的changeset id,越靠前表示它的資歷越老。
tag中的tip,一般出現在最新的changeset中,這個小技巧你要記住了哦。
最后我們要將提交推到遠程庫中才算大功告成。與git類似,也是用push命令:
` hg?push?/the?remote?repository??`
我想要看看這一版都修改了那些地方怎么查?記得git中用show命令,hg呢?export就好了:
~~~
[linc@localhost?testMercurial]$?hg?export?0:23ac3f5bfc59??
#?HG?changeset?patch??
#?User?Lincoln???
#?Date?1414415110?-28800??
#??????Mon?Oct?27?21:05:10?2014?+0800??
#?Node?ID?23ac3f5bfc592c7bd2b293e8ace0f42b9e541ece??
#?Parent??0000000000000000000000000000000000000000??
initial?commit??
??
diff?-r?000000000000?-r?23ac3f5bfc59?Debug/makefile??
---?/dev/null???Thu?Jan?01?00:00:00?1970?+0000??
+++?b/Debug/makefile????Mon?Oct?27?21:05:10?2014?+0800??
@@?-0,0?+1,44?@@??
~~~
這基本的功能就是這樣了,后面還會繼續merge、conflict等功能。
后記:
2015.1.29
ubuntu上安裝:
上述報錯是因為doc安裝需要python-docutils工具,我們需要安裝它:
/opt/mercurial-3.3-rc$ sudo apt-get install python-docutils
這次再編譯就沒有問題了:
/opt/mercurial-3.3-rc$ sudo make install
參考:
http://mercurial.selenic.com/guide