S-JIS[2016-11-28] 変更履歴

Gitクローン

Gitのクローンを行うコマンドのメモ。

 

概要

クローンは、既存のリポジトリーを複製して新しいリポジトリーを作るコマンド。

GitHub上のリポジトリーを複製して、自分のローカルマシン上に新しいリポジトリーを作るのによく使用する。


リポジトリーのコピー

既存のリポジトリーをコピーして新しいリポジトリーを作ることが出来る。

$ cd /tmp/from
$ git clone project /tmp/to/project

ただし、この方法だとmasterブランチのみになる(他のブランチはコピーされない(リモートのブランチとしては認識される))。

$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/wip/test

ブランチごとコピーするには以下のようにする。

  1. コピー元の確認
    $ cd /tmp/from
    $ ls -a project/
    .  ..  .git  test.txt
  2. コピーの実行(コピー先のディレクトリーは自動的に作られるので、事前に作っておく必要は無い)
    $ git clone --bare project /tmp/to/project/.git/
  3. コピーされたことを確認(この時点では、.gitディレクトリーのみが存在し、作業用のディレクトリーやファイルは存在しない)
    $ cd /tmp/to/project
    $ ls -a
    .  ..  .git
  4. 作業用のディレクトリーやファイルを生成
    $ git init
    Reinitialized existing Git repository in /tmp/to/project/.git/
    $ git reset --hard
    HEAD is now at e3320c6 Added test.txt
    
    $ ls -a
    .  ..  .git  test.txt
    
    $ git branch -a
    * master
      wip/test
    
  5. リモートの削除(クローン元がoriginになっている為)
    必要であれば、リモート先は追加する必要がある。
    $ git remote -v
    origin  /tmp/from/project (fetch)
    origin  /tmp/from/project (push)
    $ git remote rm origin

「git clone --bare」の代わりに「git clone --mirror」でも出来るが、違いはよく分からない^^;
ただし、ミラーだと「git remote rm」の際に警告が出る。


Git目次へ戻る / 技術メモへ戻る
メールの送信先:ひしだま