gitコマンドの使い方 ~ git clone ~

git clone コマンド概要

git cloneは以下の3つの処理を行うコマンドです。

  • remoto-repository(リモートリポジトリ)のcommitツリーを元にlocal-repository(ローカルリポジトリ)を作成する
  • デフォルトブランチの内容をstaging-area(ステージングエリア)に反映する
  • デフォルトブランチの内容をworking-directory(作業ディレクトリ)に反映する

コマンドの使い方

git clone [remote-repository-path] [local-repository-path]

remote-repository-pathに指定するパスは「.git」ファイルまでのパスを指定します。

例えば、GitHubの場合は以下のようなパスになります。

https://github.com/[GitHubのアカウント]/xxx.git

また、local-repository-pathは省略した場合は、remote-repositoryと同じ名前でリポジトリが生成されます。

以下のように、masterブランチにcommitが2つある状態のGitHubのリポジトリをgit cloneでローカルリポジトリを生成してみます。

/c/Git
$ git clone https://github.com/[GitHubのアカウント]/clonetest.git
Cloning into 'clonetest'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), 431 bytes | 5.00 KiB/s, done.

/c/Git
$ cd clonetest/

/c/Git/clonetest (master)
$ ls
file-A.txt  file-B.txt

/c/Git/clonetest (master)
$ git ls-files
file-A.txt
file-B.txt

/c/Git/clonetest (master)
$ git log --oneline --all
2c94bfe (HEAD -> master, origin/master, origin/HEAD) commit-B
84df7c4 commit-A

git clone の全てのオプションを確認する方法

以下のコマンドを実行するとブラウザでgit cloneのヘルプページが表示される

git clone --help

Gitコマンドの使い方一覧

Git設定git config ~Gitリポジトリの設定~
初期化git init ~ローカルリポジトリの構築~
ログ&設定値確認git log ~commitログの履歴確認~
git status ~作業ディレクトリとステージングエリアの更新状態確認~
git diff ~commitやファイルの差分確認~
git show ~commitの内容確認~
git remote ~リモート追跡ブランチのリモートリポジトリの設定確認~
git reflog ~HEADやブランチの移動履歴の確認と整理~
ステージングエリアの操作git add ~ステージングエリア(インデックス)にファイル追加~
git ls-files ~ステージングエリア(インデックス)のファイル一覧表示~
git rm ~ステージングエリア(インデックス)のファイル削除~
ローカルリポジトリの操作git commit ~blobやtreeなどをまとめたcommitを作成~
git merge ~指定したブランチの内容を取り込み新しいcommitを作成~
git rebase ~commitの履歴の整理~
git cherry-pic ~特定のcommitの変更内容だけを取り込む~
git cat-file ~リポジトリのオブジェクト(commit,tree,blob)の内容表示~
git revert ~指定したcommitを取り消すcommitを作成~
commitのエイリアスなどgit branch ~commitツリーの枝(ブランチ)を作成~
git tag ~リリースなどのタイミングで特定のcommitに名前を付ける~
git stash ~commitを作成する前の変更内容を一時保存~
commitの内容をステージングエリアや作業ディレクトリに反映git checkout ~commitをステージングエリアと作業ディレクトリに展開~
git reset ~HEADの位置やステージングエリアと作業ディレクトリの内容を変更~
リモートリポジトリとのやり取りgit clone ~リモートリポジトリからローカルリポジトリ作成~
git fetch ~リモートリポジトリのブランチをローカルリポジトリに反映~
git pull ~git fetchとgit mergeを同時に行う~
git push ~ローカルリポジトリのブランチをリモートリポジトリに反映~