0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Git] 動作を試す 実行例52:pullで引数を省略した時の振る舞いを見る

Posted at

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

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

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

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

実行例

引数 (レポジトリ、ブランチ)を省略し「上流ブランチ」を取り込む
https://www-creators.com/archives/2295#git_pull-3

-----

(※試行用にlocal-repo3を作成)

cd /test-space
mkdir local-repo3
cd /test-space/local-repo3

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

-----

※全て取得
git pull
↓
結果: 
remote: Enumerating objects: 27, done.
remote: Counting objects: 100% (27/27), done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 27 (delta 5), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (27/27), 2.03 KiB | 2.00 KiB/s, done.
From D:/test-space/remote-repo1
 * [new branch]      branch-R1  -> origin/branch-R1
 * [new branch]      branch-R2  -> origin/branch-R2
 * [new branch]      master     -> origin/master
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> master

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

(※ローカルブランチが無いので、ローカルブランチをマージして進める処理は実施されない。fetch取得だけ)

-----

(※checkoutでローカルブランチを作成してpullを試行)

git checkout master
↓
結果: 
Already on 'master'
branch 'master' set up to track 'origin/master'.

git branch -vv -a
↓
結果: 
* master                   1247fa9 [origin/master] message R4
  remotes/origin/branch-R1 e009b2d message R2
  remotes/origin/branch-R2 fab68d8 message R3
  remotes/origin/master    1247fa9 message R4

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

git pull
↓
結果: 
Already up to date.

(※ローカルブランチの位置を過去に戻してpullを試行すると)

git reset --hard 00cad71
↓
結果: 
HEAD is now at 00cad71 message2

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

※ローカルブランチを進める処理を実施
git pull
↓
結果: 
Updating 00cad71..1247fa9
Fast-forward
 test1.txt | 5 +++++
 test2.txt | 2 +-
 test4.txt | 1 +
 3 files changed, 7 insertions(+), 1 deletion(-)
 create mode 100644 test4.txt

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

(※masterローカルブランチを進める処理が実施されている)

-----

(※新規に単独でローカルブランチを作成して、pullを試行)

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

git branch -vv -a
↓
結果: 
  branch-R1                00cad71 message2
* master                   1247fa9 [origin/master] message R4
  remotes/origin/branch-R1 e009b2d message R2
  remotes/origin/branch-R2 fab68d8 message R3
  remotes/origin/master    1247fa9 message R4

git pull
↓
結果: 
Already up to date.

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

(※ローカルブランチを単独で作成するだけでは、pullによる処理なし)

-----

(※--set-upstream-toでローカルブランチを関連付けてpullを試行)

※--set-upstream-toでローカルブランチを関連付け
git branch -u origin/branch-R1 branch-R1
↓
結果: 
branch 'branch-R1' set up to track 'origin/branch-R1'.

git branch -vv -a
↓
結果: 
  branch-R1                00cad71 [origin/branch-R1: behind 2] message2
* master                   1247fa9 [origin/master] message R4
  remotes/origin/branch-R1 e009b2d message R2
  remotes/origin/branch-R2 fab68d8 message R3
  remotes/origin/master    1247fa9 message R4

git pull
↓
結果: 
Already up to date.

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

(※--set-upstream-toでローカルブランチを関連付けても、pullによる処理なし)

git pull origin branch-R1
↓
結果: 
From D:/test-space/remote-repo1
 * branch            branch-R1  -> FETCH_HEAD
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

git pull origin branch-R1 --ff-only
↓
結果: 
From D:/test-space/remote-repo1
 * branch            branch-R1  -> FETCH_HEAD
fatal: Not possible to fast-forward, aborting.

git pull origin branch-R1 --rebase
↓
結果: 
From D:/test-space/remote-repo1
 * branch            branch-R1  -> FETCH_HEAD
Auto-merging test1.txt
CONFLICT (content): Merge conflict in test1.txt
error: could not apply 94996a0... message4
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 94996a0... message4

git diff
↓
結果: 
diff --cc test1.txt
index f4e41fe,bb7eb1a..0000000
--- a/test1.txt
+++ b/test1.txt
@@@ -1,4 -1,4 +1,8 @@@
  Sample-Added-11
  Sample-Added-12
  Sample-Added-13
++<<<<<<< HEAD
 +Sample-Added-R2
++=======
+ Sample-Added-14
++>>>>>>> 94996a0 (message4)

(※上記「94996a0 message4」と「e009b2d message R2」のコミット履歴をマージしようとしており、
 現在チェックアウトしているmasterに、origin/branch-R1をマージするようなpullの動作となる模様)

git rebase --abort

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

-----

(※pullしたいローカルブランチをcheckoutすると、ブランチが最新側へ進んで成功)

git checkout branch-R1
↓
結果: 
Switched to branch 'branch-R1'
Your branch is behind 'origin/branch-R1' by 2 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

git pull
↓
結果: 
Updating 00cad71..e009b2d
Fast-forward
 test1.txt | 2 ++
 test2.txt | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)

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

(※pullしたいローカルブランチをcheckoutすると、ブランチを進める処理が実施されている)

-----

(※--allで、全てのブランチをpull)

[Git]全てのブランチを同時に pull する方法
https://codelab.website/git-all-branch-pull/

(※pullする前に戻す)
git checkout master
git reset --hard 00cad71
git branch -f branch-R1 00cad71
git branch branch-R2 00cad71
git log --oneline --graph --all
↓
結果: 
* 1247fa9 (origin/master) message R4
* fab68d8 (origin/branch-R2) message R3
* c69a305 message R1
* 94996a0 message4
| * e009b2d (origin/branch-R1) message R2
|/
* 20bbab7 message3
* 00cad71 (HEAD -> master, branch-R2, branch-R1) message2
* 4ace194 message1

git branch -vv -a
↓
結果: 
  branch-R1                00cad71 [origin/branch-R1: behind 2] message2
  branch-R2                00cad71 message2
* master                   00cad71 [origin/master: behind 5] message2
  remotes/origin/branch-R1 e009b2d message R2
  remotes/origin/branch-R2 fab68d8 message R3
  remotes/origin/master    1247fa9 message R4

※全てをpull
git pull --all
↓
結果: 
Updating 00cad71..1247fa9
Fast-forward
 test1.txt | 5 +++++
 test2.txt | 2 +-
 test4.txt | 1 +
 3 files changed, 7 insertions(+), 1 deletion(-)
 create mode 100644 test4.txt

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

(※チェックアウト中のmasterしかpullされていない)

-----

(※上記のチェックアウトをせずにマージする(進める)には、fetchで可能)

(※branch-R1をpullする前に戻す)
git checkout master
git branch -f branch-R1 00cad71
git log --oneline --graph --all
↓
結果: 
* 1247fa9 (HEAD -> master, origin/master) message R4
* fab68d8 (origin/branch-R2) message R3
* c69a305 message R1
* 94996a0 message4
| * e009b2d (origin/branch-R1) message R2
|/
* 20bbab7 message3
* 00cad71 (branch-R1) message2
* 4ace194 message1

git fetch origin branch-R1:branch-R1
↓
結果: 
From D:/test-space/remote-repo1
   00cad71..e009b2d  branch-R1  -> branch-R1

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

(※masterをチェックアウト中に、branch-R1ブランチを進める処理が実施されている)

-----

(※上記のチェックアウトをせずにマージする(進める)には、fetchで可能、"."指定版)

(※branch-R1をpullする前に戻す)
git checkout master
git branch -f branch-R1 00cad71
git log --oneline --graph --all
↓
結果: 
* 1247fa9 (HEAD -> master, origin/master) message R4
* fab68d8 (origin/branch-R2) message R3
* c69a305 message R1
* 94996a0 message4
| * e009b2d (origin/branch-R1) message R2
|/
* 20bbab7 message3
* 00cad71 (branch-R1) message2
* 4ace194 message1

git fetch . origin/branch-R1:branch-R1
↓
結果: 
From .
   00cad71..e009b2d  origin/branch-R1 -> branch-R1

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

(※masterをチェックアウト中に、branch-R1ブランチを進める処理が実施されている)

-----

(※local-repo3を削除)
cd /test-space
rd /s /q "/test-space/local-repo3"

環境

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?