Git における同期
SVN は、単一の中央リポジトリを開発者間のコミュニケーションハブとして使用し、開発者の作業コピーと中央リポジトリ間で変更を受け渡しすることによってコラボレーションが行われます。Git の分散コラボレーションモデルはこれとは異なり、各開発者がリポジトリのコピーを持ち、ローカルの履歴やブランチ構造を完全な形で保有しています。開発者は、他の開発者と個々の変更を共有する必要はなく、通常は一連のコミットをまとめて共有します。Git では作業コピー内の個々の変更項目を中央リポジトリにコミットするのではなく、ブランチ全体をリポジトリ間で共有します。
git remote
git remote
は、他のリポジトリとの接続の作成、内容確認、削除を行うコマンドです。リモート接続とは、他のリポジトリへのダイレクトリンクではなく、ブックマークのようなものです。他のリポジトリにリアルタイムアクセスを行うのではなく、非短縮 URL への参照として使用可能な短縮名称として機能します。
例えば、次の図はローカルリポジトリと中央リポジトリとの間およびローカルリポジトリと他の開発者のリポジトリとの間の 2 つのリモート接続を示したものです。それらをフル URL を用いて参照するのではなく、他の Git コマンドに “origin” および “john” という名称のショートカットを引き渡すことが可能になります。
git remote の使い方の概要
git remote
コマンドは基本的に、リポジトリの ./.git/config
ファイルに保存されているリモートエントリのリストを管理するためのインターフェイスです。以下のコマンドを使用して、リモートリストの現在の状態を表示します。
git remote 構成の表示
git remote
他のリポジトリへのリモート接続の一覧を表示するコマンドです。
git remote -v
上のコマンドと同様ですが、ただし各々の接続のURLが表示されます。
git remote 構成の作成と変更
git remote
コマンドは、リポジトリの ./.git/config
ファイルに変更を加えるのに便利な、「ヘルパー」メソッドでもあります。以下に示すコマンドを使用すると、他のリポジトリとの接続を管理できます。以下の各コマンドは、リポジトリの /.git/config
ファイルに変更を加えます。各コマンドの実行結果は、テキストエディターで ./.git/config
ファイルを直接編集することによって得ることもできます。
git remote add <name> <url>
Create a new connection to a remote repository. After adding a remote, you’ll be able to use <name>
as a convenient shortcut for <url>
in other Git commands.
git remote rm <name>
Remove the connection to the remote repository called <name>
.
git remote rename <old-name> <new-name>
Rename a remote connection from <old-name>
to <new-name>
.
git remote の解説
Git は、完全に分離された開発環境を各開発者に提供するように設計されています。これは、情報が自動的にリポジトリ間を行き来するわけではないことを意味します。つまり、開発者は手動で上流のコミットをローカルリポジトリにプルするか、ローカルのコミットを手動で中央リポジトリにプッシュバックする必要があります。git remote
コマンドは、URL をこれらの「共有の」コマンドに渡すための非常に簡単な方法です。
origin リモート
git clone
コマンドを使用してリポジトリをクローンすると、クローンされたリポジトリをポイントバックする origin という名称のリモート接続が自動的に作成されます。このコマンドは上流での変更をプルしたりローカルなコミットを公開したりする簡便な手段を提供するものであり、開発者にとっては中央リポジトリの作業コピーを作成する場合に有用なコマンドです。この機能により、Git ベースのほとんどのプロジェクトでは中央リポジトリの名称が origin になっています。
リポジトリ URL
Git ではリモートリポジトリを参照する多くの手段がサポートされています。即ち、リモートリポジトリにアクセスする極めて簡便な手段として、HTTP プロトコルと SSH プロトコルが用意されています。HTTP はリポジトリに対して匿名のリードオンリーアクセスを行う簡便な方法です。次はその例です:
http://host/path/to/repo.git
ただし、通常は HTTP アドレスに対してプッシュを行うことはできません (他の開発者からローカルリポジトリに対して匿名のプッシュがあったとしても常に拒否するはずです)。リードライトアクセスを行う場合は SSH を使用しなければなりません:
ssh://user@host/path/to/repo.git
この場合、ホストマシンにおいて有効な SSH アカウントが必要ですが、それを除けば Git ではデフォルトで SSH 経由の認証済みアクセスをサポートしています。Bitbucket.com のような最新のセキュアなサードパーティ製ホスティングソリューションでは、そうした URL が自動的に提供されます。
git remote コマンド
git remote
コマンドには、他の多くの Git コマンドと同様に、追加の「サブコマンド」を付加することができます。よく使用される git remote
のサブコマンドを以下に説明します。
ADD <NAME> <URL>
Adds a record to ./.git/config
for remote named <name>
at the repository url <url>.
-f
オプションを指定すると、リモートレコードの作成直後に、git fetch
を実行します。
--tags
オプションを指定すると、ただちに git fetch
を実行し、リモートリポジトリからすべてのタグをインポートします。
RENAME <OLD> <NEW>
Updates ./.git/config
to rename the record <OLD>
to <NEW>
. All remote-tracking branches and configuration settings for the remote are updated.
REMOVE or RM <NAME>
Modifies ./.git/config
and removes the remote named <NAME>
. All remote-tracking branches and configuration settings for the remote are removed.
GET-URL <NAME>
リモートレコードの URL を出力します。
--push
オプションを指定すると、フェッチ URL ではなく、プッシュ URL を照会します。
--all
オプションは、そのリモートのすべての URL のリストを表示します。
SHOW <NAME>
Outputs high-level information about the remote <NAME>
.
PRUNE <NAME>
Deletes any local branches for <NAME>
that are not present on the remote repository.
--dry-run
オプションを指定すると、どのブランチが削除される設定になっているかを示すだけで、実際の削除は行いません。
git remote の例
origin に加えて他の開発者のリポジトリへの接続を作成しておくと便利なケースがよくあります。たとえば、同僚の John が公開リポジトリを dev.example.com/john.git
に保有している場合、次のようにして接続を新たに作成します。
git remote add john http://dev.example.com/john.git
個々の開発者のリポジトリに対してこのようなアクセスをすることにより、中央リポジトリを介さずにコラボレーションが可能になります。この機能は特に、大規模プロジェクト内で作業する小規模チームの場合に有用です。
リモートの表示
git remote
コマンドはデフォルトで、以前に保存された、他のリポジトリへのリモート接続のリストを表示します。これにより、「ブックマーク」名を示す 1 行の出力がリモートリポジトリごとに生成されます。
$ git remote
origin
upstream
other_users_repo
git remote
を -v
オプションを指定して実行すると、ブックマークされたリポジトリ名に加えて、それぞれに対応するリポジトリ URL のリストが出力されます。-v
オプションは「verbose (詳細)」を表しています。git remote
の詳細出力の例を以下に示します。
git remote -v
origin git@bitbucket.com:origin_user/reponame.git (fetch)
origin git@bitbucket.com:origin_user/reponame.git (push)
upstream https://bitbucket.com/upstream_user/reponame.git (fetch)
upstream https://bitbucket.com/upstream_user/reponame.git (push)
other_users_repo https://bitbucket.com/other_users_repo/reponame (fetch)
other_users_repo https://bitbucket.com/other_users_repo/reponame (push)
リモートリポジトリの追加
git remote add
コマンドは、リモートリポジトリに対する新しい接続レコードを作成します。リモートを追加した後は、他の Git コマンドで ./.git/config
の中に新しいレコードを作成します。この config ファイルの更新の例を次に示します。
$ git remote add fake_test https://bitbucket.com/upstream_user/reponame.git; [remote "remote_test"]
url = https://bitbucket.com/upstream_user/reponame.git
fetch = +refs/heads/*:refs/remotes/remote_test/*
リモートの検査
show サブコマンドを git remote
に追加すると、リモートの構成に関する詳細な出力を得ることができます。この出力には、リモートに関連付けられたブランチのリストと、フェッチやプッシュのために付加されたエンドポイントも含まれます。
git remote show upstream
* remote upstream
Fetch URL: https://bitbucket.com/upstream_user/reponame.git
Push URL: https://bitbucket.com/upstream_user/reponame.git
HEAD branch: main
Remote branches:
main tracked
simd-deprecated tracked
tutorial tracked
Local ref configured for 'git push':
main pushes to main (fast-forwardable)
Git リモートからのフェッチとプル
Git リモートへのプッシュ
git push
コマンドは、リモートリポジトリへの書き込みに使用します。
git push <remote-name> <branch-name>
This example will upload the local state of <branch-name>
to the remote repository specified by <remote-name>
.
リモートの名前変更と削除
git remote rename <old-name> <new-name>
The command git remote
rename is self-explanatory. When executed, this command will rename a remote connection from <old-name>
to <new-name>
. Additionally, this will modify the contents of ./.git/config
to rename the record for the remote there as well.
git remote rm <name>
The command git remote rm
will remove the connection to the remote repository specified by the <name>
parameter. To demonstrate let us 'undo' the remote addition from our last example. If we execute git remote rm remote_test
, and then examine the contents of ./.git/config
we can see that the [remote "remote_test"]
record is no longer there.