imshota / system_development_project_application_1

講義用
0 stars 0 forks source link

git #2

Open imshota opened 3 years ago

imshota commented 3 years ago

演習1 リポジトリを新規作成し、変更を3回程度コミットせよ

作業記録

コマンド 意味
git init bar リポジトリbarを新規に作成、もしくは既存のリポジトリを初期化
git add foo ファイルfooをステージ
git commit -m ""msg" メッセージ"msg"をつけてコミット
git log コミット履歴を表示(--onelineをつけると1行で表示)
git status 現在の状況を表示

リポジトリpracを作成し、ディレクトリpracに移動

% git init prac
% cd prac

ファイルtestを用意

% echo hello > test

変更するために、ファイルをステージに上げ、メッセージをつけてコミットする。

% git add test
% git commit -m "change 1"
[master (root-commit) f79278b] change 1
 1 file changed, 1 insertion(+)
 create mode 100644 test
% git add test            
% git commit -m "change 2"
[master e1cc869] change 2
 1 file changed, 1 insertion(+)
% git add test            
% git commit -m "change 3"
[master 526b8b3] change 3
 1 file changed, 1 deletion(-)

ログを確認してみると、きちんと3回変更が行われているのがわかる。

% git log
commit 526b8b3fef8b3a9d44bfff01bf49eed776df0391 (HEAD -> master)
Author: imshota <imshotaseo@icloud.com>
Date:   Fri Oct 30 05:49:16 2020 +0900

    change 3

commit e1cc869ee04c04c6ec1dfc98bc45e08cd09a063f
Author: imshota <imshotaseo@icloud.com>
Date:   Fri Oct 30 05:49:00 2020 +0900

    change 2

commit f79278b820f36260400f5552b61b800589533f9c
Author: imshota <imshotaseo@icloud.com>
Date:   Fri Oct 30 05:48:19 2020 +0900

    change 1
imshota commented 3 years ago

演習2 各コミット間の差分を確認せよ

作業記録

git log --graph --all -- graph : コミット履歴をグラフ構造で表示 -- all : すべてのブランチを表示

実際にやってみる。


(base) shota@MacBook-Pro prac % git log --graph --all
* commit 526b8b3fef8b3a9d44bfff01bf49eed776df0391 (HEAD -> master)
| Author: imshota <imshotaseo@icloud.com>
| Date:   Fri Oct 30 05:49:16 2020 +0900
| 
|     change 3
| 
* commit e1cc869ee04c04c6ec1dfc98bc45e08cd09a063f
| Author: imshota <imshotaseo@icloud.com>
| Date:   Fri Oct 30 05:49:00 2020 +0900
| 
|     change 2
| 
* commit f79278b820f36260400f5552b61b800589533f9c
  Author: imshota <imshotaseo@icloud.com>
  Date:   Fri Oct 30 05:48:19 2020 +0900

      change 1

git diff commitA commitB commitA から commitBへの差分が表示 つまり、commitAに差分を足すとcommitBになる。

コミットの差分を見てみる。

% git diff f79278b820f36260400f5552b61b800589533f9c e1cc869ee04c04c6ec1dfc98bc45e08cd09a063f
diff --git a/test b/test
index ce01362..ac1f558 100644
--- a/test
+++ b/test
@@ -1 +1,2 @@
 hello
+nice
\ No newline at end of file
% git diff e1cc869ee04c04c6ec1dfc98bc45e08cd09a063f 526b8b3fef8b3a9d44bfff01bf49eed776df0391
diff --git a/test b/test
index ac1f558..3220089 100644
--- a/test
+++ b/test
@@ -1,2 +1 @@
-hello
 nice
\ No newline at end of file

(base) shota@MacBook-Pro prac % git diff f79278b820f36260400f5552b61b800589533f9c 526b8b3fef8b3a9d44bfff01bf49eed776df0391
diff --git a/test b/test
index ce01362..3220089 100644
--- a/test
+++ b/test
@@ -1 +1 @@
-hello
+nice
\ No newline at end of file
imshota commented 3 years ago

演習3 一部のハンクだけgit addし、2種類の差分を確認せよ

作業記録

ワークツリーからインデックス git diffで違いがわかる。

インデックスからリポジトリ git diff --cachedで違いがわかる。

git add -p foo 選択的にaddすることができる。

% git add -p test
diff --git a/test b/test
index 7c11bf4..abb4960 100644
--- a/test
+++ b/test
@@ -1,6 +1,6 @@
 nice
-pre
+pre ch
 to
 meet
-post
+post ch
 you
\ No newline at end of file
(1/1) Stage this hunk [y,n,q,a,d,s,e,?]? s
Split into 2 hunks.
@@ -1,4 +1,4 @@
 nice
-pre
+pre ch
 to
 meet
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? y
@@ -3,4 +3,4 @@
 to
 meet
-post
+post ch
 you
\ No newline at end of file
(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? n

分ける場所の間隔が狭すぎると、一緒の反区と見なされてしまう。 そういう時は、's'でもっと細かく輪かけることができる。

2種類の差分を見てみる。

% git add test
% git diff
% git diff --cached

diff --git a/test b/test
index 5a20399..abb4960 100644
--- a/test
+++ b/test
@@ -1,4 +1,6 @@
 nice
+pre ch
 to
 meet
+post ch
 you
\ No newline at end of file

全て、testファイルをステージしているため、ワークツリーとインデックスに違いはない。 一方、コミットしていないため、インデックスからリポジトリでは違いが出てくる。

imshota commented 3 years ago

演習4 特定の時点のファイル内容を確認せよ

作業記録

今のとことのlogはこんな感じ

% git log --oneline                 
fc2ed65 (HEAD -> master) change test
4d02112 change test
526b8b3 change 3
e1cc869 change 2
f79278b change 1

演習3が始まる前に戻ってみる。

% git checkout 526b8b3
Note: switching to '526b8b3'.

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 526b8b3 change 3
% cat test
nice                            
imshota commented 3 years ago

演習5 新しく作ったブランチに変更をいくつかコミットせよ途中でタグを打て

作業記録

git branch br-a HEADを指すブランチを作る

git checkout -b br-b HEADを指すブランチを作り、
そのブランチをチェックアウトする

br-aのブランチを作り、そのまま何回か変更していく中で、タグtag-aをうつ。

% git checkout -b br-a
Switched to a new branch 'br-a'
% git add test               
% git commit -m "change test tag1"
[br-a 2836775] change test tag1
 1 file changed, 1 insertion(+)
% git tag tag-a
% git add test                    
% git commit -m "change test tag2"
[br-a 20441f0] change test tag2
 1 file changed, 1 insertion(+)

変更後、ログを確認

% git log --graph --all --decorate=full
* commit 20441f0caf39725dbc33d207f56ea40018be97a1 (HEAD -> refs/heads/br-a)
| Author: imshota <imshotaseo@icloud.com>
| Date:   Fri Oct 30 07:31:56 2020 +0900
| 
|     change test tag2
| 
* commit 2836775aa851b038f7bf3aa2e1161858c0c46df2 (tag: refs/tags/tag-a)
| Author: imshota <imshotaseo@icloud.com>
| Date:   Fri Oct 30 07:31:00 2020 +0900
| 
|     change test tag1
| 
* commit fc2ed65da675fd9e0d160a7ed3fcec36d4a11425 (refs/heads/master)
| Author: imshota <imshotaseo@icloud.com>
| Date:   Fri Oct 30 07:18:31 2020 +0900
| 
|     change test
| 
* commit 4d02112d68f75612256a5e83b7810a3d9970ef25
:
imshota commented 3 years ago

演習6 br-aをmainにマージせよ。br-aとmainに競合するコミットを加え、さらにマージせよ。

作業記録

Gitは3-way merge

Fast-forward:単にポインタを動かすだけで事足りる状況 敢えてマージコミットを作る:
git merge --no-ff

br-aをmainにマージ

% git checkout master                  
Switched to branch 'master'
% git merge --no-ff br-a
Merge made by the 'recursive' strategy.
 test | 2 ++
 1 file changed, 2 insertions(+)
% git log --graph --all --decorate=full
*   commit d909337013cd6ae9dc5ffee638fe96e3b0823e15 (HEAD -> refs/heads/master)
|\  Merge: fc2ed65 20441f0
| | Author: imshota <imshotaseo@icloud.com>
| | Date:   Fri Oct 30 07:37:50 2020 +0900
| | 
| |     Merge branch 'br-a'
| | 
| * commit 20441f0caf39725dbc33d207f56ea40018be97a1 (refs/heads/br-a)
| | Author: imshota <imshotaseo@icloud.com>
| | Date:   Fri Oct 30 07:31:56 2020 +0900
| | 
| |     change test tag2
| | 
| * commit 2836775aa851b038f7bf3aa2e1161858c0c46df2 (tag: refs/tags/tag-a)
|/  Author: imshota <imshotaseo@icloud.com>
|   Date:   Fri Oct 30 07:31:00 2020 +0900
|   
|       change test tag1
| 

br-aは分岐前に止まっていることに注意 br-aに戻る。

%git checkout br-a
Switched to branch 'br-a'

ここで、ファイルを変更し、コミットしたあと、 再びmasterに戻って、conflictが起こるよう、ファイルを変更してコミットし、マージしてみる。

% git add test 
% git commit -m "conflict br-a"        
[br-a 967a50e] conflict br-a
 1 file changed, 2 insertions(+), 1 deletion(-)
% git checkout master
Switched to branch 'master'
% git commit -m "conflict master"
[master 59eb7b6] conflict master
 1 file changed, 3 insertions(+), 1 deletion(-)
% git merge --no-ff br-a               
Auto-merging test
CONFLICT (content): Merge conflict in test
Automatic merge failed; fix conflicts and then commit the result.

conflictしている該当箇所

nice
pre ch
tag
to
meet
tag2
post ch
you
<<<<<<< HEAD

conflict master
=======
conflict br
>>>>>>> br-a
imshota commented 3 years ago

演習7 br-bとmainにいくつかコミットし、br-bをmainにリベースせよ

作業記録

新しくブランチbr-bを作成

% git checkout -b br-b
Switched to a new branch 'br-b'

ブランチbr-bで変更をコミット

% git add test         
% git commit -m "add test br-b"
[br-b bd7872c] add test br-b
 1 file changed, 1 insertion(+)

masterブランチに戻り、変更をコミット

% git checkout master                  
Switched to branch 'master'
% git add test                 
% git commit -m "add test master"
[master 5d9b162] add test master
 1 file changed, 2 insertions(+), 1 deletion(-)

br-bブランチに戻り、rebase

% git checkout br-b
Switched to branch 'br-b'
% git rebase master
First, rewinding head to replay your work on top of it...
Applying: add test br-b
% git log --graph --all --decorate=full
* commit aee2fe3bb2b153c93414d7b6301f65ce41dc86b4 (HEAD -> refs/heads/br-b)
| Author: imshota <imshotaseo@icloud.com>
| Date:   Fri Oct 30 08:06:15 2020 +0900
| 
|     add test br-b
| 
* commit 5d9b162819f754570fd7344cea043d81e5c3f3e1 (refs/heads/master)
| Author: imshota <imshotaseo@icloud.com>
| Date:   Fri Oct 30 08:08:07 2020 +0900
| 
|     add test master
| 
* commit e293371bb4f14ecb8ec73fee3b6bf175bc1e0e99
| Author: imshota <imshotaseo@icloud.com>
| Date:   Fri Oct 30 08:04:25 2020 +0900
| 
|     reset
|   
*   commit 60c76a3197d77362b10265c8b7b86c8eec599e5d
imshota commented 3 years ago

演習8 “git rebase -i”でコミット順とコミットメッセージを修正してみよ

作業記録

以下のような状況を想定

% git log --graph --all --decorate=full
* commit fab502cf8a2fc0d8b1a91a1c80a583d761d9c323 (HEAD -> refs/heads/br-b)
| Author: imshota <imshotaseo@icloud.com>
| Date:   Fri Oct 30 08:34:00 2020 +0900
| 
|     rebase test2
| 
* commit e352dbe0a6387c9f5919f00cbca92ac9e199601d
| Author: imshota <imshotaseo@icloud.com>
| Date:   Fri Oct 30 08:33:51 2020 +0900
| 
|     rebase test1
| 
* commit fcbe045a7474037a166f4b37f7984d32ebc48ee0 (refs/heads/master)
  Author: imshota <imshotaseo@icloud.com>
  Date:   Fri Oct 30 08:32:21 2020 +0900

      my first update
% cat test
hello
re 1
re 2

ここから、コミット順を入れ替えたり、コメントを編集する。

% git rebase -i master
imshota commented 3 years ago

演習9 ファイルを変更してgit addし、変更をなくさずにgit addを取り消せ

作業記録

git reset --mixed インデックスがリセットされる

git reset --hard ワークツリーがリセットされる

add を破棄するには、reset --mixedすれば良い

% git add test  
% git status
On branch br-b
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   test
% git reset --mixed
Unstaged changes after reset:
M       test
% git status   
% git status       
On branch br-b
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   test

no changes added to commit (use "git add" and/or "git commit -a")
imshota commented 3 years ago

演習10 GitHubで良い/悪いコミットメッセージの例を探せ

作業記録 良いコミットメッセージ https://github.com/facebookresearch/detectron2/commit/9fe2edaf6f22bb1c2069ec12b9089d19069e5286#diff-8b7f7c845fb82a140e4700edbdb7cd098c35a0d8da5c98fca569471bc6c1c11c

rrename imports of point_rend and deeplab

Reviewed By: ytsheng

Differential Revision: D23015493

fbshipit-source-id: 246c37a9cd913202e801e4e781adb63c4b74c224

1行で簡潔に何をしてるのかがわかる。 誰が直したかが書いてある。 差分(一個前)のRevisionが書かれている。

悪いコミットメッセージ https://github.com/imshota/my_atcoder/commit/1af97dc7638ebe2ca35cadf51a1771a3687802eb

ABC 171 a c++

何をしたのかがわからない 目的がわからない