LoginSignup
0
0

[Git] 動作を試す 実行例57:一度pushしたコミット履歴を他人が取得した後にコミット履歴を消した場合の挙動を見る

Posted at

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

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

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

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

実行例

(※一度pushしたコミット履歴を、reset等で意図的にコミットを削除すると、他人側ではどうなるかを試行)

-----

(※試行用に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 fetch origin
git checkout branch-R1
git log --oneline --graph --all
↓
結果: 
* 1247fa9 (origin/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

-----

(※試行用にlocal-repo4をコピーして作成)

xcopy /H /E "D:\test-space\local-repo3" "D:\test-space\local-repo4"

cd /test-space/local-repo4
git log --oneline --graph --all
↓
結果: 
* 1247fa9 (origin/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

(※local-repo3とlocal-repo4の間で以降試行する)

-----

(※local-repo3でpushした内容をlocal-repo4で取得後に、pushを戻してしまう時)

cd /test-space/local-repo3

(※ひとまず適当にbranch-R1で変更の操作)
echo Sample-Added-R5 >> test1.txt
git commit -a -m "message R5"
↓
結果: 
[branch-R1 3e758f9] message R5
 1 file changed, 1 insertion(+)

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

git push origin branch-R1
↓
結果: 
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 303 bytes | 303.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
To D:/test-space/remote-repo1.git
   e009b2d..3e758f9  branch-R1 -> branch-R1

cd /test-space/local-repo4

git log --oneline --graph --all
↓
結果: 
* 1247fa9 (origin/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

git fetch origin branch-R1
↓
結果: 
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 283 bytes | 1024 bytes/s, done.
From D:/test-space/remote-repo1
 * branch            branch-R1  -> FETCH_HEAD
   e009b2d..3e758f9  branch-R1  -> origin/branch-R1

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

git pull origin branch-R1
↓
結果: 
From D:/test-space/remote-repo1
 * branch            branch-R1  -> FETCH_HEAD
Updating e009b2d..3e758f9
Fast-forward
 test1.txt | 1 +
 1 file changed, 1 insertion(+)

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

(※local-repo4側でもmessage R5の所まで更新される)

cd /test-space/local-repo3

(※前回のpushの操作を戻す)
git reset --hard e009b2d

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

※位置を戻したbranch-R1ブランチをリモートリポジトリへpush送信 (過去側に進めるpush)
git push -f origin branch-R1
↓
結果: 
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To D:/test-space/remote-repo1.git
 + 3e758f9...e009b2d branch-R1 -> branch-R1 (forced update)

git log --oneline --graph --all
↓
結果: 
* 1247fa9 (origin/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

(※message R5のコミット履歴が消える)

cd /test-space/local-repo4

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

git fetch origin branch-R1
↓
結果: 
From D:/test-space/remote-repo1
 * branch            branch-R1  -> FETCH_HEAD
 + 3e758f9...e009b2d branch-R1  -> origin/branch-R1  (forced update)

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

(※ローカルブランチbranch-R1だけ1つ先に進んだ状態のまま)

git pull origin branch-R1
↓
結果: 
From D:/test-space/remote-repo1
 * branch            branch-R1  -> FETCH_HEAD
Already up to date.

(※ローカルブランチbranch-R1の方が1つ先に進んでいるので、pullでは更新なしとなる模様)

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

-----

(※ここから更に、リモートブランチorigin/branch-R1が違う枝の方へ進んだ時)

cd /test-space/local-repo3

(※branch-R1を違う枝の方へ進める操作)
echo Sample-Added-R6 >> test1.txt
git commit -a -m "message R6"
↓
結果: 
[branch-R1 3a9cf58] message R6
 1 file changed, 1 insertion(+)

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

git push origin branch-R1
↓
結果: 
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 302 bytes | 302.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
To D:/test-space/remote-repo1.git
   e009b2d..3a9cf58  branch-R1 -> branch-R1

cd /test-space/local-repo4

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

git fetch origin branch-R1
↓
結果: 
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 282 bytes | 0 bytes/s, done.
From D:/test-space/remote-repo1
 * branch            branch-R1  -> FETCH_HEAD
   e009b2d..3a9cf58  branch-R1  -> origin/branch-R1

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

git pull -f 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.

(※リモートorigin/branch-R1とローカルbranch-R1の間で違う枝になっているので、pull出来ない状態)

(※local-repo4側では、知らないうちに違う枝に取り残されて、何もしていないのにmergeが必要になってしまう)

git merge origin/branch-R1
↓
結果: 
Auto-merging test1.txt
CONFLICT (content): Merge conflict in test1.txt
Automatic merge failed; fix conflicts and then commit the result.

git diff
↓
結果: 
diff --cc test1.txt
index 874d124,5de92d6..0000000
--- a/test1.txt
+++ b/test1.txt
@@@ -2,4 -2,4 +2,8 @@@ Sample-Added-1
  Sample-Added-12
  Sample-Added-13
  Sample-Added-R2
++<<<<<<< HEAD
 +Sample-Added-R5
++=======
+ Sample-Added-R6
++>>>>>>> origin/branch-R1

(※コンフリクトを手動で解消)
echo Sample-Added-11 > test1.txt
echo Sample-Added-12 >> test1.txt
echo Sample-Added-13 >> test1.txt
echo Sample-Added-R2 >> test1.txt
echo Sample-Added-R5 >> test1.txt
echo Sample-Added-R6 >> test1.txt
type test1.txt
↓
結果: 
Sample-Added-11
Sample-Added-12
Sample-Added-13
Sample-Added-R2
Sample-Added-R5
Sample-Added-R6

git add .
git status -s
↓
結果: 
M  test1.txt

※マージ処置をコミット
git commit --no-edit
↓
結果: 
[branch-R1 f85522d] Merge remote-tracking branch 'origin/branch-R1' into branch-R1

git log --oneline --graph --all
↓
結果: 
*   f85522d (HEAD -> branch-R1) Merge remote-tracking branch 'origin/branch-R1' into branch-R1
|\
| * 3a9cf58 (origin/branch-R1) message R6
* | 3e758f9 message R5
|/
* e009b2d message R2
| * 1247fa9 (origin/master) message R4
| * fab68d8 (origin/branch-R2) message R3
| * c69a305 message R1
| * 94996a0 message4
|/
* 20bbab7 message3
* 00cad71 message2
* 4ace194 message1

git push origin branch-R1
git log --oneline --graph --all
↓
結果: 
*   f85522d (HEAD -> branch-R1, origin/branch-R1) Merge remote-tracking branch 'origin/branch-R1' into branch-R1
|\
| * 3a9cf58 message R6
* | 3e758f9 message R5
|/
* e009b2d message R2
| * 1247fa9 (origin/master) message R4
| * fab68d8 (origin/branch-R2) message R3
| * c69a305 message R1
| * 94996a0 message4
|/
* 20bbab7 message3
* 00cad71 message2
* 4ace194 message1

(※回復は出来るが、面倒なので、苦情の元となる)

-----

(※pushで公開済みのbranch-R1ブランチを、local-repo3でリベースしてpushしてしまう時)

cd /test-space/local-repo3

git pull origin
↓
結果: 
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 6 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), 603 bytes | 0 bytes/s, done.
From D:/test-space/remote-repo1
   3a9cf58..f85522d  branch-R1  -> origin/branch-R1
Updating 3a9cf58..f85522d
Fast-forward
 test1.txt | 1 +
 1 file changed, 1 insertion(+)

git log --oneline --graph --all
↓
結果: 
*   f85522d (HEAD -> branch-R1, origin/branch-R1) Merge remote-tracking branch 'origin/branch-R1' into branch-R1
|\
| * 3a9cf58 message R6
* | 3e758f9 message R5
|/
* e009b2d message R2
| * 1247fa9 (origin/master) message R4
| * fab68d8 (origin/branch-R2) message R3
| * c69a305 message R1
| * 94996a0 message4
|/
* 20bbab7 message3
* 00cad71 message2
* 4ace194 message1

git rebase origin/master
↓
結果: 
Auto-merging test1.txt
CONFLICT (content): Merge conflict in test1.txt
error: could not apply e009b2d... message R2
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 e009b2d... message R2

git diff
↓
結果: 
diff --cc test1.txt
index 4222b46,f4e41fe..0000000
--- a/test1.txt
+++ b/test1.txt
@@@ -1,7 -1,4 +1,11 @@@
  Sample-Added-11
  Sample-Added-12
  Sample-Added-13
++<<<<<<< HEAD
 +Sample-Added-14
 +Sample-Added-R1
 +Sample-Added-R3
 +Sample-Added-R4
++=======
+ Sample-Added-R2
++>>>>>>> e009b2d (message R2)

(※コンフリクトを手動で解消)
echo Sample-Added-11 > test1.txt
echo Sample-Added-12 >> test1.txt
echo Sample-Added-13 >> test1.txt
echo Sample-Added-R1+2+3+4 >> test1.txt
type test1.txt
↓
結果: 
Sample-Added-11
Sample-Added-12
Sample-Added-13
Sample-Added-R1+2+3+4

git add .
git rebase --continue

git rebase --continue
↓
結果: 
Auto-merging test1.txt
CONFLICT (content): Merge conflict in test1.txt
error: could not apply 3e758f9... message R5

git diff
↓
結果: 
diff --cc test1.txt
index 08cdc60,874d124..0000000
--- a/test1.txt
+++ b/test1.txt
@@@ -1,4 -1,5 +1,9 @@@
  Sample-Added-11
  Sample-Added-12
  Sample-Added-13
++<<<<<<< HEAD
 +Sample-Added-R1+2+3+4
++=======
+ Sample-Added-R2
+ Sample-Added-R5
++>>>>>>> 3e758f9 (message R5)

(※コンフリクトを手動で解消)
echo Sample-Added-11 > test1.txt
echo Sample-Added-12 >> test1.txt
echo Sample-Added-13 >> test1.txt
echo Sample-Added-R1+2+3+4+5 >> test1.txt
type test1.txt
↓
結果: 
Sample-Added-11
Sample-Added-12
Sample-Added-13
Sample-Added-R1+2+3+4+5

git add .
git rebase --continue

git rebase --continue
↓
結果: 
Auto-merging test1.txt
CONFLICT (content): Merge conflict in test1.txt
error: could not apply 3a9cf58... message R6

git diff
↓
結果: 
diff --cc test1.txt
index 5e4aa50,5de92d6..0000000
--- a/test1.txt
+++ b/test1.txt
@@@ -1,4 -1,5 +1,9 @@@
  Sample-Added-11
  Sample-Added-12
  Sample-Added-13
++<<<<<<< HEAD
 +Sample-Added-R1+2+3+4+5
++=======
+ Sample-Added-R2
+ Sample-Added-R6
++>>>>>>> 3a9cf58 (message R6)

(※コンフリクトを手動で解消)
echo Sample-Added-11 > test1.txt
echo Sample-Added-12 >> test1.txt
echo Sample-Added-13 >> test1.txt
echo Sample-Added-R1+2+3+4+5+6 >> test1.txt
type test1.txt
↓
結果: 
Sample-Added-11
Sample-Added-12
Sample-Added-13
Sample-Added-R1+2+3+4+5+6

git add .
git rebase --continue

git rebase --continue
↓
結果: 
Successfully rebased and updated refs/heads/branch-R1.

git log --oneline --graph --all
↓
結果: 
* a605230 (HEAD -> branch-R1) message R6
* 5f95c9d message R5
* 1002a3a message R2
* 1247fa9 (origin/master) message R4
* fab68d8 (origin/branch-R2) message R3
* c69a305 message R1
* 94996a0 message4
| *   f85522d (origin/branch-R1) Merge remote-tracking branch 'origin/branch-R1' into branch-R1
| |\
| | * 3a9cf58 message R6
| * | 3e758f9 message R5
| |/
| * e009b2d message R2
|/
* 20bbab7 message3
* 00cad71 message2
* 4ace194 message1

(※origin/masterからmessage R2, R5, R6がリベースで追加されている状態)

(※リベースしたものをpush送信)
git push origin branch-R1
↓
結果: 
To D:/test-space/remote-repo1.git
 ! [rejected]        branch-R1 -> branch-R1 (non-fast-forward)
error: failed to push some refs to 'D:/test-space/remote-repo1.git'

git push -f origin branch-R1
↓
結果: 
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 8 threads
Compressing objects: 100% (9/9), done.
Writing objects: 100% (9/9), 867 bytes | 289.00 KiB/s, done.
Total 9 (delta 3), reused 0 (delta 0), pack-reused 0
To D:/test-space/remote-repo1.git
 + f85522d...a605230 branch-R1 -> branch-R1 (forced update)

(※強制的なpushで実施可)

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

(※枝分かれ・マージしていた履歴が消えている)

cd /test-space/local-repo4

git log --oneline --graph --all
↓
結果: 
*   f85522d (HEAD -> branch-R1, origin/branch-R1) Merge remote-tracking branch 'origin/branch-R1' into branch-R1
|\
| * 3a9cf58 message R6
* | 3e758f9 message R5
|/
* e009b2d message R2
| * 1247fa9 (origin/master) message R4
| * fab68d8 (origin/branch-R2) message R3
| * c69a305 message R1
| * 94996a0 message4
|/
* 20bbab7 message3
* 00cad71 message2
* 4ace194 message1

git fetch origin
↓
結果: 
remote: Enumerating objects: 11, done.
remote: Counting objects: 100% (11/11), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 9 (delta 3), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (9/9), 847 bytes | 3.00 KiB/s, done.
From D:/test-space/remote-repo1
 + f85522d...a605230 branch-R1  -> origin/branch-R1  (forced update)

git log --oneline --graph --all
↓
結果: 
* a605230 (origin/branch-R1) message R6
* 5f95c9d message R5
* 1002a3a message R2
* 1247fa9 (origin/master) message R4
* fab68d8 (origin/branch-R2) message R3
* c69a305 message R1
* 94996a0 message4
| *   f85522d (HEAD -> branch-R1) Merge remote-tracking branch 'origin/branch-R1' into branch-R1
| |\
| | * 3a9cf58 message R6
| * | 3e758f9 message R5
| |/
| * e009b2d message R2
|/
* 20bbab7 message3
* 00cad71 message2
* 4ace194 message1

(※local-repo4側では、知らないうちにorigin/branch-R1が、branch-R1系列から別のブランチ系列に移ってしまった)

git pull origin branch-R1
↓
結果: 
From D:/test-space/remote-repo1
 * branch            branch-R1  -> FETCH_HEAD
fatal: Need to specify how to reconcile divergent branches.

git log --oneline --graph --all
↓
結果: 
* a605230 (origin/branch-R1) message R6
* 5f95c9d message R5
* 1002a3a message R2
* 1247fa9 (origin/master) message R4
* fab68d8 (origin/branch-R2) message R3
* c69a305 message R1
* 94996a0 message4
| *   f85522d (HEAD -> branch-R1) Merge remote-tracking branch 'origin/branch-R1' into branch-R1
| |\
| | * 3a9cf58 message R6
| * | 3e758f9 message R5
| |/
| * e009b2d message R2
|/
* 20bbab7 message3
* 00cad71 message2
* 4ace194 message1

(※リモートorigin/branch-R1とローカルbranch-R1の間で違う枝になっているので、pull出来ない状態)

(※local-repo4側では、知らないうちに違う枝に取り残されて、何もしていないのにmergeが必要になってしまう)

-----

(※リモートリポジトリを元に戻す)

git push -f origin e009b2d:branch-R1

git checkout origin/master
git branch -D branch-R1
git log --oneline --graph --all
↓
結果: 
* 1247fa9 (HEAD, 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

-----

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

環境

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