clone~リモートリポジトリーをローカルに複製
コマンド形式
git cloneはリモートリポジトリーの複製をローカルリポジトリーとして作成する。パスワード認証、SSH認証に対するコマンド形式は以下の通り。
パスワード認証
- git clone https://github.com/account/remote_rep [local_rep]
- SSH認証
- git clone git@github.com:account/remote_rep.git [local_rep]
- local_repを省略すると- remote_repと同じ名前でディレクトリーが作成され、ローカルリポジトリーに設定される
- パスワード認証はログインパスワードが非推奨になり、2段階認証化が必要
- SSH認証の場合は別途キーセットの生成・登録などが必要
実行例
- あらかじめリモートリポジトリーexampleを作成しておく
- user1ディレクトリーを作成し、そこに- exampleリポジトリーをクローン
| 1 2 3 4 5 6 7 8 9 10 11 | [vagrant@localhost git]$ mkdir user1 && cd user1 [vagrant@localhost user1]$ git clone git@github.com:taustation/example.git Cloning into 'example'... Enter passphrase for key '/home/vagrant/.ssh/id_rsa': remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. Receiving objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 [vagrant@localhost user1]$ cd example/ [vagrant@localhost example]$ ls README.md | 
push~ローカルのコミットをリモートに反映
コマンド形式
ローカルリポジトリー内でgit pushを実行すると、ローカルでのコミットをリモートに反映
git push
実行例
user1でcloneしたローカルリポジトリーのでファイルを作成・コミット。
| 1 2 3 4 5 6 7 8 | vagrant@localhost example]$ vi user1.txt [vagrant@localhost example]$ cat user1.txt user1による新規作成 [vagrant@localhost example]$ git add . [vagrant@localhost example]$ git commit -m "create user1.txt" [main b26165d] create user1.txt  1 file changed, 1 insertion(+)  create mode 100644 user1.txt | 
コミット結果のログ。
| 1 2 3 4 5 6 7 8 9 10 11 12 | [vagrant@localhost example]$ git log commit b26165d71c3748d673c1b0858c3f87ff9eb1cb80 (HEAD -> main, origin/main, origin/HEAD) Author: taustation <taustation@gmail.com> Date:   Thu Aug 12 22:36:03 2021 +0900     create user1.txt commit 5449e2a9ddfcac864a647b1093894d33a741c57c Author: taustation <88479749+taustation@users.noreply.github.com> Date:   Thu Aug 12 21:52:05 2021 +0900     Initial commit | 
コミット結果をリモートにプッシュ。
| 1 2 3 4 5 6 7 8 9 | [vagrant@localhost example]$ git push Enter passphrase for key '/home/vagrant/.ssh/id_rsa': Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 303 bytes | 303.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To github.com:taustation/example.git    5449e2a..b26165d  main -> main | 
GitHub上で作成されたファイルが確認できる。
cloneはその時点でのリモート内容を複製
user2ディレクトリーを作成してexampleをクローン。
| 1 2 3 4 5 6 7 8 9 10 | [vagrant@localhost git]$ mkdir user2 && cd user2 [vagrant@localhost user2]$ git clone git@github.com:taustation/example.git Cloning into 'example'... Enter passphrase for key '/home/vagrant/.ssh/id_rsa': 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 3 (delta 0), pack-reused 0 Receiving objects: 100% (6/6), done. [vagrant@localhost user2]$ cd example/ | 
user1が作成したファイルが反映される。
| 1 2 3 4 | [vagrant@localhost example]$ ls README.md  user1.txt [vagrant@localhost example]$ cat user1.txt user1による新規作成 | 
リモートへのpushはfast-forward
user2ファイル作成・コミット。
| 1 2 3 4 5 6 7 8 | [vagrant@localhost example]$ vi user2.txt [vagrant@localhost example]$ cat user2.txt user2による新規作成 [vagrant@localhost example]$ git add . [vagrant@localhost example]$ git commit -m "create user2.txt" [main 5efce34] create user2.txt  1 file changed, 1 insertion(+)  create mode 100644 user2.txt | 
コミット結果のログ。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | [vagrant@localhost example]$ git log commit 5efce34d814bce68977ed1bc0c4d1e66045837f1 (HEAD -> main, origin/main, origin/HEAD) Author: taustation <taustation@gmail.com> Date:   Thu Aug 12 22:40:12 2021 +0900     create user2.txt commit b26165d71c3748d673c1b0858c3f87ff9eb1cb80 Author: taustation <taustation@gmail.com> Date:   Thu Aug 12 22:36:03 2021 +0900     create user1.txt commit 5449e2a9ddfcac864a647b1093894d33a741c57c Author: taustation <88479749+taustation@users.noreply.github.com> Date:   Thu Aug 12 21:52:05 2021 +0900     Initial commit | 
user2のコミット結果をプッシュ。リモート上でfast-forwardでマージされる。この間にリモートで他のコミットが行われているとpushできない。
| 1 2 3 4 5 6 7 8 9 | [vagrant@localhost example]$ git push Enter passphrase for key '/home/vagrant/.ssh/id_rsa': Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 331 bytes | 331.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To github.com:taustation/example.git    b26165d..5efce34  main -> main | 
リモートリポジトリーからのpull
user1でリモートのexampleをプル。リモートの内容がfast-forwardでローカルリポジトリーにマージされる。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [vagrant@localhost example]$ git pull Enter passphrase for key '/home/vagrant/.ssh/id_rsa': remote: Enumerating objects: 4, done. remote: Counting objects: 100% (4/4), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From github.com:taustation/example    b26165d..5efce34  main       -> origin/main Updating b26165d..5efce34 Fast-forward  user2.txt | 1 +  1 file changed, 1 insertion(+)  create mode 100644 user2.txt | 
この時点までの変更(user2でのファイル作成)が反映されている。
| 1 2 3 4 | [vagrant@localhost example]$ ls README.md  user1.txt  user2.txt [vagrant@localhost example]$ cat user2.txt user2による新規作成 | 
ログも最新状態になる。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | [vagrant@localhost example]$ git log commit 5efce34d814bce68977ed1bc0c4d1e66045837f1 (HEAD -> main, origin/main, origin/HEAD) Author: taustation <taustation@gmail.com> Date:   Thu Aug 12 22:40:12 2021 +0900     create user2.txt commit b26165d71c3748d673c1b0858c3f87ff9eb1cb80 Author: taustation <taustation@gmail.com> Date:   Thu Aug 12 22:36:03 2021 +0900     create user1.txt commit 5449e2a9ddfcac864a647b1093894d33a741c57c Author: taustation <88479749+taustation@users.noreply.github.com> Date:   Thu Aug 12 21:52:05 2021 +0900     Initial commit | 
pullの後のコミットとpush
user1でファイルを変更、コミット。
| 1 2 3 4 5 6 7 8 | [vagrant@localhost example]$ vi user1.txt [vagrant@localhost example]$ cat user1.txt user1による新規作成 user1による追加 [vagrant@localhost example]$ git add . [vagrant@localhost example]$ git commit -m "modify user1.txt" [main 2869489] modify user1.txt  1 file changed, 1 insertion(+) | 
コミット後のログ。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | commit 2869489cd881ae897558d89aa91b23419b7c33e4 (HEAD -> main, origin/main, origin/HEAD) Author: taustation <taustation@gmail.com> Date:   Thu Aug 12 22:46:42 2021 +0900     modify user1.txt commit 5efce34d814bce68977ed1bc0c4d1e66045837f1 Author: taustation <taustation@gmail.com> Date:   Thu Aug 12 22:40:12 2021 +0900     create user2.txt commit b26165d71c3748d673c1b0858c3f87ff9eb1cb80 Author: taustation <taustation@gmail.com> Date:   Thu Aug 12 22:36:03 2021 +0900     create user1.txt commit 5449e2a9ddfcac864a647b1093894d33a741c57c Author: taustation <88479749+taustation@users.noreply.github.com> Date:   Thu Aug 12 21:52:05 2021 +0900     Initial commit | 
user1のローカルリポジトリーからリモートにpush。
| 1 2 3 4 5 6 7 8 9 | [vagrant@localhost example]$ git push Enter passphrase for key '/home/vagrant/.ssh/id_rsa': Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 342 bytes | 342.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To github.com:taustation/example.git    5efce34..2869489  main -> main | 
fetch~一時的なリモートの内容確認
フェッチの確認
user2でgit fetchを実行。
| 1 2 3 4 5 6 7 8 9 | [vagrant@localhost example]$ git fetch Enter passphrase for key '/home/vagrant/.ssh/id_rsa': remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From github.com:taustation/example    5efce34..2869489  main       -> origin/main | 
フェッチを実行した結果。
- フェッチした場合はローカルリポジトリーにマージされず、FETCH_HEADブランチに複製される
- FETCH_HEADは不可視で- git branchでは確認できないが、- git checkoutで移動することができる
- FETCH_HEADで変更・コミットした内容は最終的に破棄される
- 破棄したくない場合はgit switch -cで新たなブランチを作成できる
- この操作はgit switch -cで取り消せる
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | [vagrant@localhost example]$ git checkout FETCH_HEAD Note: switching to 'FETCH_HEAD'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -c with the switch command. Example:   git switch -c <new-branch-name> Or undo this operation with:   git switch - Turn off this advice by setting config variable advice.detachedHead to false HEAD is now at 2869489 modify user1.txt | 
FERCH_HEADブランチのログ。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | [vagrant@localhost example]$ git log commit 2869489cd881ae897558d89aa91b23419b7c33e4 (HEAD, origin/main, origin/HEAD) Author: taustation <taustation@gmail.com> Date:   Thu Aug 12 22:46:42 2021 +0900     modify user1.txt commit 5efce34d814bce68977ed1bc0c4d1e66045837f1 (main) Author: taustation <taustation@gmail.com> Date:   Thu Aug 12 22:40:12 2021 +0900     create user2.txt commit b26165d71c3748d673c1b0858c3f87ff9eb1cb80 Author: taustation <taustation@gmail.com> Date:   Thu Aug 12 22:36:03 2021 +0900     create user1.txt commit 5449e2a9ddfcac864a647b1093894d33a741c57c Author: taustation <88479749+taustation@users.noreply.github.com> Date:   Thu Aug 12 21:52:05 2021 +0900     Initial commit | 
フェッチ内容の反映 = pull
FETCH_HEADがある状態でmainブランチに移動。
| 1 2 3 4 5 | [vagrant@localhost example]$ git checkout main Previous HEAD position was 2869489 modify user1.txt Switched to branch 'main' Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.   (use "git pull" to update your local branch) | 
FETCH_HEADブランチをマージ。
| 1 2 3 4 5 | [vagrant@localhost example]$ git merge FETCH_HEAD Updating 5efce34..2869489 Fast-forward  user1.txt | 1 +  1 file changed, 1 insertion(+) | 
FETCH_HEADで変更していなければ、結果は直接pullを実行したときと同じ。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | [vagrant@localhost example]$ git log commit 2869489cd881ae897558d89aa91b23419b7c33e4 (HEAD -> main, origin/main, origin/HEAD) Author: taustation <taustation@gmail.com> Date:   Thu Aug 12 22:46:42 2021 +0900     modify user1.txt commit 5efce34d814bce68977ed1bc0c4d1e66045837f1 Author: taustation <taustation@gmail.com> Date:   Thu Aug 12 22:40:12 2021 +0900     create user2.txt commit b26165d71c3748d673c1b0858c3f87ff9eb1cb80 Author: taustation <taustation@gmail.com> Date:   Thu Aug 12 22:36:03 2021 +0900     create user1.txt commit 5449e2a9ddfcac864a647b1093894d33a741c57c Author: taustation <88479749+taustation@users.noreply.github.com> Date:   Thu Aug 12 21:52:05 2021 +0900     Initial commit |