僕は、分散バージョン管理システムMercurialをサッパリ理解できなかったわけですが、「これは便利だ」と思ってからはずっと使っています。しかし、ジョエル・スポルスキのように研究したわけではなくて、数個のコマンドを覚えただけです。日常的作業は、それらでなんとか間に合っている感じです。なので、僕が使っているコマンド(少数です)を紹介します。
bashのコマンドラインで紹介しますが、使っているOSはWindowsです。とはいえ、コマンドラインでの使い方はOSによらず共通のはずです。
コマンドの詳しい解説はしません。あまり考え込んだり、躊躇はせずに;
「四の五の言ってないで、使ってミソ」ってことです。
“Mercurialのすすめ”や“Mercurialの概念的な解説”を一応挙げおくと:
- http://local.joelonsoftware.com/wiki/%E5%88%86%E6%95%A3%E3%83%90%E3%83%BC%E3%82%B8%E3%83%A7%E3%83%B3%E7%AE%A1%E7%90%86%E3%81%A7%E9%96%93%E9%81%95%E3%81%84%E3%81%AA%E3%81%84%E3%81%A3%E3%81%A6%E3%80%81%E3%83%99%E3%82%A4%E3%83%93%E3%83%BC
- Kiln が Git (や他の SCM)ではなく Mercurial をサポートする理由 - tcha.org
- http://mercurial.selenic.com/wiki/JapaneseUnderstandingMercurial
- http://hginit.com/
([追記]最初のリンクは、http://d.hatena.ne.jp/m-hiyama/20120813/1344816655 でも紹介した「分散バージョン管理で間違いないって、ベイビー」ですが、2012-08-31現在、サイトが落ちているようです。[/追記])
内容:
リポジトリを管理するコマンド
リポジトリを管理するコマンドはたまにしか使いません。しかし、リポジトリの使い始めに必ず必要になりますので、避けて通ることはできません。
リポジトリを作る
$ hg init myrepo$
または、
$ mkdir myrepo$ cd myrepo
$ hg init
$
既存リポジトリをクローンする場合。
$ hg clone https://m_hiyama@bitbucket.org/m_hiyama/yet-uncertain
destination directory: yet-uncertain
requesting all changes
adding changesets
adding manifests
adding file changes
added 1 changesets with 4 changes to 4 files
updating to branch default
4 files updated, 0 files merged, 0 files removed, 0 files unresolved$
クローンしたリポジトリのディレクトリ名を、元と別な名前に変えたい場合。
$ hg clone https://m_hiyama@bitbucket.org/m_hiyama/yet-uncertain sixteen... (省略) ...
$
リポジトリにする
$ hg init mywork$
リポジトリであることをやめる
.hg/ ディレクトリをゴソッと消すだけ。だけど十分注意してくださいね!
$ cd myrepo$ rm -r .hg
$
リポジトリをコピーする
クローンして新規作成と同じ。次の例はローカルのあいだでのコピー。
$ hg clone yet-uncertain sixteen
updating to branch default
4 files updated, 0 files merged, 0 files removed, 0 files unresolved$
日常的に使うコマンド
リポジトリを使い始めたら、次のコマンド達を使います。
ファイルを追加する
「追加する」とは、Mercurialの管理下に置くことです。
$ hg add hello.txt$
ファイルの状況を確認する
$ hg st -mard
A hello.txt$
-mardオプションの意味はヘルプで調べてください。僕はたいていこのオプションで使っています。
ヘルプを見る
とりあえず、
$ hg help
Mercurial Distributed SCMlist of commands:
add add the specified files on the next commit
... (省略) ...use "hg -v help" to show builtin aliases and global options
$
特定のコマンドのヘルプは、
$ hg st --help
hg status [OPTION]... [FILE]...aliases: st
show changed files in the working directory
... (省略) ...use "hg -v help status" to show more info
$
コミットする
コミットとは、ファイルへの変更を確定することです。
$ hg commit hello.txt$
このとき、テキストエディタでコミットログを書く必要があります。コマンドラインでコミットログを指定するなら、
$ $ hg commit -m "first commit" hello.txt$
bashのコマンドラインにうまく日本語が入らないので "first commit" としましたが、僕は通常テキストエディタを使い日本語でコミットログを書いています。
コミットを取り消す
直前のコミットだけは取り消せます。
$ hg rollback
repository tip rolled back to revision -1 (undo commit)
working directory now based on revision -1$
コミットログを確認する
$ hg log
changeset: 0:06da87e322da
tag: tip
user: m_hiyama
date: Fri Aug 31 12:24:24 2012 +0900
summary: 最初のコミット$
ログが長くなったら -l オプションで、hg log -l 5 とかします。
現在のリポジトリの変更分を他のリポジトリに反映させる
$ hg push
pushing to htps://m_hiyama@bitbucket.org/m_hiyama/yet-uncertain
searching for changes
http authorization required
realm: Bitbucket.org HTTP
user: m_hiyama
password:
remote: adding changesets
remote: adding manifests
remote: adding file changes
remote: added 1 changesets with 1 changes to 1 files
remote: bb/acl: m_hiyama is allowed. accepted payload.$
他のリポジトリの変更分を取り込む
$ hg pull
pulling from htps://m_hiyama@bitbucket.org/m_hiyama/yet-uncertain
searching for changes
adding changesets
adding manifests
adding file changes
added 1 changesets with 1 changes to 1 files
(run 'hg update' to get a working copy)$
作業ディレクトリを更新する
hg pull しただけだと、ワーキングコピー(作業ディレクトリの内容)は変わってません。次のように更新します。
$ hg update
1 files updated, 0 files merged, 0 files removed, 0 files unresolved$
変更をマージする
pushしようとすると、ヘッド(最新バージョン)が複数できちゃうよ、と警告されることがあります。
$ hg push
pushing to htps://m_hiyama@bitbucket.org/m_hiyama/yet-uncertain
searching for changes
abort: push creates new remote head 182145ce4d19!
(you should pull and merge or use push -f to force)$
こんなときは、hg pull して相手方の変更を取り込んでからマージします。
$ hg pull
pulling from htps://m_hiyama@bitbucket.org/m_hiyama/yet-uncertain
searching for changes
adding changesets
adding manifests
adding file changes
added 2 changesets with 2 changes to 1 files (+1 heads)
(run 'hg heads' to see heads, 'hg merge' to merge)$ hg merge
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
(branch merge, don't forget to commit)$
don't forget to commit と出てますね。忘れないように。
$ hg commit -m merged$
このときのコミットメッセージは merged で十分。
この後で再度 hg push しましょう。これも忘れないように。
コンフリクトを解消する
マージしようとしたら、コンフリクト(変更の衝突)が検出されることもあります。
$ hg push
pushing to htps://m_hiyama@bitbucket.org/m_hiyama/yet-uncertain
searching for changes
abort: push creates new remote head 54a3992974c5!
(you should pull and merge or use push -f to force)$ hg pull
pulling from htps://m_hiyama@bitbucket.org/m_hiyama/yet-uncertain
searching for changes
adding changesets
adding manifests
adding file changes
added 1 changesets with 1 changes to 1 files (+1 heads)
(run 'hg heads' to see heads, 'hg merge' to merge)$ hg merge
merging hello2.txt
warning: conflicts during merge.
merging hello2.txt incomplete! (edit conflicts, then use 'hg resolve --mark')
0 files updated, 0 files merged, 0 files removed, 1 files unresolved
use 'hg resolve' to retry unresolved file merges or 'hg update -C .' to abandon$
コンフリクトを処理します。手順は:
- hg resolve -l でどのファイルがコンフリクトしてるかを調べる。
- 問題のファイルを編集する。
- hg resolve -m で処理が済んだ(コンフリクトが解消された)旨を伝える。
- hg commit する。
$ hg resolve -l
U hello2.txt... (ここで、hello2.txtの編集作業を行う) ...
$ hg resolve -m
$ hg resolve -l
R hello2.txt$ hg st -mard
M hello2.txt$ hg commit -m merged
$
この後で再度 hg push しましょう。
コマンド以外
.hg/hgrc
.hg/hgrc に[paths]設定を書いておくと、hg pull, hg push のときのURLを短縮名で入力できます。パスを完全に省略すると、defaultに書いたURLが使われます。
[paths] default = htps://m_hiyama@bitbucket.org/m_hiyama/yet-uncertain local = file:../sixteen
この設定なら、次のように操作できます。
$ hg pull local
pulling from file:../sixteen
searching for changes
adding changesets
adding manifests
adding file changes
added 2 changesets with 1 changes to 1 files
(run 'hg update' to get a working copy)$ hg push
pushing to htps://m_hiyama@bitbucket.org/m_hiyama/yet-uncertain
searching for changes
http authorization required
realm: Bitbucket.org HTTP
user: m_hiyama
password:
remote: adding changesets
remote: adding manifests
remote: adding file changes
remote: added 2 changesets with 1 changes to 1 files
remote: bb/acl: m_hiyama is allowed. accepted payload.$
pathsの設定は hg paths コマンドで表示できます。
$ hg paths
default = htps://m_hiyama@bitbucket.org/m_hiyama/yet-uncertain
local = file:../sixteen$
設定ファイル
Mercurialの設定ファイルは色々な場所/名前で置けますが、~/.hgrc はどのOSでも有効です。Windowsにおける ~/ は、環境変数%HOME%、それがなければ%PROFILE%の値で決まります。
僕はほとんど設定はしてません。以下 ~/.hgrc の実物。
[ui] username=m_hiyama [extensions] mq =
環境変数EDITORを設定してない人は、[ui]セクションに、editor=emacsclient とか書いておくといいかも知れません。
.hgignore
Mercurial管理されているディレクトリの直下に.hgignoreファイルを置き、「Mercurialに認識して欲しくないファイル名パターン」を書いておくと、ゴミファイル/一時ファイルを無視してくれます。
syntax: glob *.pyc *.orig *.rej *~ *.db *.log* tmp/ *.tmp *.TMP syntax: regexp \#.*\#$
.hgignoreの書き方は hg help ignore とかを見てください。
たまに使うコマンド
その他のコマンド。僕の個人的な使用頻度順。
- hg parent
- hg rename
- hg revert
- hg diff
- hg heads
- hg cat
- hg locate
- hg archive