Gitの動作を理解するために、Gitのコマンドを実際に試して、結果を見てみました。
1つの記事内で一連のGitコマンドが完結しているので、これら一連のコマンドを順に実行させて結果を見ることで、一連のGitの動作を実際に体感でき、一通り独習することが可能です。
※前回記事のリポジトリ状態からの続きになっています。
前回記事へ | 目次へ:Git関連記事のまとめページ | 次回記事へ |
---|
実行例
git log --oneline
↓
結果:
94996a0 (HEAD -> master) 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
type test2.txt
↓
結果:
Sample-Added-**21
-----
git show 00cad71
↓
結果:
commit 00cad7159948154b9a0e2011d052584bedd643c9
Author: person1 <person1@abc.def>
Date: Sun Jan 1 18:08:04 2023 +0900
message2
diff --git a/test1.txt b/test1.txt
index 046a699..0116b50 100644
--- a/test1.txt
+++ b/test1.txt
@@ -1 +1,2 @@
Sample-Added-11
+Sample-Added-12
diff --git a/test2.txt b/test2.txt
new file mode 100644
index 0000000..21a6906
--- /dev/null
+++ b/test2.txt
@@ -0,0 +1 @@
+Sample-Added-21
-----
※一部コミットを戻すとマージが必要、競合の状態となる。
git revert 00cad71 --no-edit
↓
結果:
Auto-merging test1.txt
CONFLICT (content): Merge conflict in test1.txt
CONFLICT (modify/delete): test2.txt deleted in parent of 00cad71 (message2) and modified in HEAD. Version HEAD of test2.txt left in tree.
error: could not revert 00cad71... message2
git status
↓
結果:
On branch master
You are currently reverting commit 00cad71.
(fix conflicts and run "git revert --continue")
(use "git revert --skip" to skip this patch)
(use "git revert --abort" to cancel the revert operation)
Unmerged paths:
(use "git restore --staged <file>..." to unstage)
(use "git add/rm <file>..." as appropriate to mark resolution)
both modified: test1.txt
deleted by them: test2.txt
no changes added to commit (use "git add" and/or "git commit -a")
type test1.txt
↓
結果:
Sample-Added-11
<<<<<<< HEAD
Sample-Added-12
Sample-Added-13
Sample-Added-14
=======
>>>>>>> parent of 00cad71 (message2)
※手動で書き換えて競合を解消。
echo Sample-Added-11 > test1.txt
echo Sample-Added-13 >> test1.txt
echo Sample-Added-14 >> test1.txt
type test1.txt
↓
結果:
Sample-Added-11
Sample-Added-13
Sample-Added-14
git add .
git status
↓
結果:
You are currently reverting commit 00cad71.
(all conflicts fixed: run "git revert --continue")
(use "git revert --skip" to skip this patch)
(use "git revert --abort" to cancel the revert operation)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: test1.txt
-----
git revert --continue --no-edit
↓
結果:
[master 6c92812] Revert "message2"
1 file changed, 3 deletions(-)
git log --oneline
↓
結果:
6134ee3 (HEAD -> master) Revert "message2"
94996a0 message4
20bbab7 message3
00cad71 message2
4ace194 message1
dir /b
↓
結果:
test1.txt
test2.txt
test4.txt
type test1.txt
↓
結果:
Sample-Added-11
Sample-Added-13
Sample-Added-14
type test2.txt
↓
結果:
Sample-Added-**21
-----
(※始めの状態に戻す)
git reset --hard 94996a0
git log --oneline
↓
結果:
94996a0 (HEAD -> master) message4
20bbab7 message3
00cad71 message2
4ace194 message1
環境
Windows 10、PortableGit-2.40.0-64-bitを使用、全てローカルPC上で実施、GitHub等は不使用。