LoginSignup
0
0

[Git] 動作を試す 実行例43:fetch/merge/pullでリモートリポジトリから履歴を取得

Posted at

Gitの動作を理解するために、Gitのコマンドを実際に試して、結果を見てみました。

1つの記事内で一連のGitコマンドが完結しているので、これら一連のコマンドを順に実行させて結果を見ることで、一連のGitの動作を実際に体感でき、一通り独習することが可能です。

※前回記事のリポジトリ状態からの続きになっています。

前回記事へ 目次へ:Git関連記事のまとめページ 次回記事へ

実行例

git fetchとブランチ取り込み系コマンドを解説 - Qiita
https://qiita.com/matsumon-development/items/d3231acccc08c8d74c21

※自分のリポジトリにまだ存在しないものをすべて取得
git fetch origin
※自動的にフェッチを行いリモートブランチの内容を現在のブランチにマージ
git pull

-----

(※local-repo2を削除して再試行)

cd /test-space
rd /s /q "/test-space/local-repo2"
mkdir local-repo2
cd /test-space/local-repo2

git init
git remote add origin "D:/test-space/remote-repo1.git"

git log
↓
結果: 
fatal: your current branch 'master' does not have any commits yet

-----

(※fetch→mergeを試行)

git fetch origin master
↓
結果: 
remote: Enumerating objects: 18, done.
remote: Counting objects: 100% (18/18), done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 18 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (18/18), 1.28 KiB | 1024 bytes/s, done.
From D:/test-space/remote-repo1
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master

git log --oneline --graph --all
↓
結果: 
* c69a305 (origin/master) message R1
* 94996a0 message4
* 20bbab7 message3
* 00cad71 message2
* 4ace194 message1

dir /b
↓
結果: 
(なし)

git merge origin/master

git log --oneline --graph --all
↓
結果: 
* c69a305 (HEAD -> master, origin/master) message R1
* 94996a0 message4
* 20bbab7 message3
* 00cad71 message2
* 4ace194 message1

dir /b
↓
結果: 
test1.txt
test2.txt
test4.txt

(※ファイル内容が生成されている)

type test1.txt
↓
結果: 
Sample-Added-11
Sample-Added-12
Sample-Added-13
Sample-Added-14
Sample-Added-R1

-----

(※local-repo2を削除して再試行)

cd /test-space
rd /s /q "/test-space/local-repo2"
mkdir local-repo2
cd /test-space/local-repo2

git init
git remote add origin "D:/test-space/remote-repo1.git"

git log
↓
結果: 
fatal: your current branch 'master' does not have any commits yet

-----

(※pullを試行)

git pull origin master
↓
結果: 
remote: Enumerating objects: 18, done.
remote: Counting objects: 100% (18/18), done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 18 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (18/18), 1.28 KiB | 1024 bytes/s, done.
From D:/test-space/remote-repo1
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master

git log --oneline --graph --all
↓
結果: 
* c69a305 (HEAD -> master, origin/master) message R1
* 94996a0 message4
* 20bbab7 message3
* 00cad71 message2
* 4ace194 message1

dir /b
↓
結果: 
test1.txt
test2.txt
test4.txt

(※ファイル内容が生成されている)

type test1.txt
↓
結果: 
Sample-Added-11
Sample-Added-12
Sample-Added-13
Sample-Added-14
Sample-Added-R1

-----

git pull origin branch-R1

git log --oneline --graph --all
↓
結果: 
* e009b2d (origin/branch-R1) message R2
| * c69a305 (HEAD -> master, origin/master, origin/HEAD) message R1
| * 94996a0 message4
|/
* 20bbab7 message3
* 00cad71 message2
* 4ace194 message1

環境

Windows 10、PortableGit-2.40.0-64-bitを使用、全てローカルPC上で実施、GitHub等は不使用。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0