Slide 1

Slide 1 text

1 30 Gitの基本的な使い方 慶應義塾大学理工学部物理情報工学科 渡辺 物理情報工学ソフトウェア開発演習

Slide 2

Slide 2 text

2 30 Gitでの一連の操作を概観する 基本的なコマンドを覚える メッセージの読み方を覚える

Slide 3

Slide 3 text

3 30 $ git switch –c branch コマンド プロンプト コマンド オプション 引数 Gitは「git」の後にコマンドやオプション、引数を指定する

Slide 4

Slide 4 text

4 30 $ git help コマンド名 以下でヘルプが参照できる Gitに限らず、近代的なツールはヘルプが 非常に充実していることが多い 「公式ヘルプを読む」 「エラーメッセージを読む」 この二つが上達の近道

Slide 5

Slide 5 text

5 30 Gitは「名前とメールアドレス」を教えないとコミットできず エラーになる $ git commit -m "initial commit" Author identity unknown *** Please tell me who you are. Run git config --global user.email "[email protected]" git config --global user.name "Your Name" to set your account's default identity. Omit --global to set the identity only in this repository. fatal: unable to auto-detect email address Gitは問題が起きたときに「こうすれば?」と提案してくれる →エラーメッセージを読む

Slide 6

Slide 6 text

6 30 $ git config --global user.name "H. Watanabe" $ git config --global user.email [email protected] 名前とメールアドレスの設定 デフォルトエディタの設定 $ git config --global core.editor vim ※この情報は公開リポジトリでは全世界に公開されるので注意 デフォルトブランチの設定 $ git config --global init.defaultBranch main

Slide 7

Slide 7 text

7 30 「--global」をつけて設定した情報はホームディレクトリの.gitconfigに保存される $ cat .gitconfig [user] name = H. Watanabe email = [email protected] [core] editor = vim [init] defaultBranch = main $ git config --global core.editor vim コマンドとの対応関係に注意

Slide 8

Slide 8 text

8 30 $ git config user.name "H. Watanabe" 「--global」をつけなかった場合は、そのリポジトリ固有の設定となる リポジトリ毎に別の名前やアドレスを使いたい場合に有用 現在の設定は「-l」で表示できる $ git config -l このオプション「-l」を忘れたのなら、git help configを実行 -l --list List all variables set in config file, along with their values. 上記の記述を見つけ、目的のオプションが「-l」「--list」であることがわかる

Slide 9

Slide 9 text

9 30 Gitは以下のような操作を行う 1. リポジトリの初期化 2. ファイルをインデックスに登録 3. コミット 4. ファイルを修正 5. ステージング&コミット 以後繰り返し

Slide 10

Slide 10 text

10 30 $ cd $ mkdir project $ cd project ホームディレクトリへ移動 projectディレクトリを作成 projectディレクトリへ移動 $ git init Initialized empty Git repository in C:/path/to/project/.git/ カレントディレクトリをGitリポジトリとして初期化

Slide 11

Slide 11 text

11 30 $ echo “Hello” > README.md $ git add README.md 新しいファイルを作成し、インデックスに登録する Home Project .git README.md

Slide 12

Slide 12 text

12 30 $ git status On branch main No commits yet Changes to be committed: (use "git rm --cached ..." to unstage) new file: README.md • 現在のカレントブランチはmain • まだ歴史はない • 現在コミットした場合に反映される修正はREADME.mdの追加 以下のようなことが書いてある

Slide 13

Slide 13 text

13 30 $ git commit デフォルトエディタ(Vim)が開いて、以下のような画面が出てくる # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # On branch main # # Initial commit # # Changes to be committed: # new file: README.md # • コミットメッセージを書け • 行頭に「#」がある行は無視される • 空メッセージならコミットを中断する

Slide 14

Slide 14 text

14 30 $ git commit [main (root-commit) 9d8aab0] initial commit 1 file changed, 1 insertion(+) create mode 100644 README.md コミット終了後、以下のようなメッセージが表示される • mainブランチの最初のコミットである(root-commit) • コミットハッシュの先頭7桁が9d8aab0である コミットハッシュ • コミットに付与された一意な識別子 • 実体はSHA-1ハッシュ(40桁) • 詳細は「Gitの仕組み」にて

Slide 15

Slide 15 text

15 30 歴史が作られたのでgit logで履歴が参照できる $ git log commit 9d8aab06e0a1f1b152546db086fe7737a02526e1 (HEAD -> main) Author: H. Watanabe Date: Thu Sep 16 17:15:41 2021 +0900 initial commit • 9d8aab0というコミットハッシュのコミット • mainブランチがそのコミットを指している • カレントブランチはmainブランチ (HEAD -> main) • 著者とメールアドレスはH. Watanabe • コミットされた日付が2021年9月16日 • コミットメッセージがinitial commit 以下のような情報がわかる

Slide 16

Slide 16 text

16 30 Hello エディタでREADME.mdに、以下のような行を追加 Hello Update ワーキングツリー インデックス リポジトリ

Slide 17

Slide 17 text

17 30 git statusで状態表示 $ git status On branch main Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git restore ..." to discard changes in working directory) modified: README.md no changes added to commit (use "git add" and/or "git commit -a") • カレントブランチがmainであり(On branch main) • ステージされていない変更があり(Changes not staged for commit) • その変更とは、README.mdが修正されたものである (modified: README.md) • コミットしたければgit addかgit commit –aを使え 状態表示の簡略版 $ git status -s M README.md ワーキングツリーとインデックスに差異がある

Slide 18

Slide 18 text

18 30 $ git diff diff --git a/README.md b/README.md index e965047..9c99d1a 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ Hello +Update • ワーキングツリーとインデックスを比較したら • README.mdに変更があり • ワーキングツリーには「Update」という行が追加されている git diffで差分を表示できる ワーキングツリー インデックス リポジトリ

Slide 19

Slide 19 text

19 30 $ git add README.md $ git diff –cached diff --git a/README.md b/README.md index e965047..9c99d1a 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ Hello +Update インデックスに登録してから差分表示 ワーキングツリー インデックス リポジトリ git add git diff --cached

Slide 20

Slide 20 text

20 30 $ git status On branch main Changes to be committed: (use "git restore --staged ..." to unstage) modified: README.md 状態を表示する • 「Changes not staged for commit:」→「Changes to be committed:」 • ステージングを取り消すならgit restore --staged しろ 状態表示の簡略版 $ git status -s M README.md インデックスとリポジトリに差異がある

Slide 21

Slide 21 text

21 30 $ git commit -m "updates README.md" [main a736d82] updates README.md 1 file changed, 1 insertion(+) 修正をコミットする 新たにa736d82というコミットが作られ、歴史に追加された main main a736d82 9d8aab0 9d8aab0

Slide 22

Slide 22 text

22 30 $ git log commit a736d82251279f592a25e38503bb9130bac12481 (HEAD -> main) Author: H. Watanabe Date: Thu Sep 16 19:13:34 2021 +0900 updates README.md commit 9d8aab06e0a1f1b152546db086fe7737a02526e1 Author: H. Watanabe Date: Thu Sep 16 17:15:41 2021 +0900 initial commit 履歴の一行表示 $ git log --oneline a736d82 (HEAD -> main) updates README.md 9d8aab0 initial commit 二つコミットができている

Slide 23

Slide 23 text

23 30 リポジトリを初期化し、ファイルをインデックスに登録し、最初のコミッ トをして、修正してさらにコミットをする、という一連の動作を確認した。 その過程で以下のコマンドを使った。 • git init リポジトリの初期化 • git add インデックスへの登録 • git commit インデックスにある状態をコミット • git status 現在の状態を表示 • git diff ワーキングツリー、インデックス、コミット間の差分を表示 • git log 歴史を表示 以下、それぞれのコマンドの詳細について説明

Slide 24

Slide 24 text

24 30 リポジトリを初期化する $ git init カレントディレクトリをGitで初期化する $ git init project projectというディレクトリを作成し、Gitで 初期化する $ git init --bare project.git project.gitというベアリポジトリを作る

Slide 25

Slide 25 text

25 30 git addには三つの役割がある • リポジトリの管理下にないファイルを管理下に置く • リポジトリの管理下にあるファイルをステージングする • Gitに衝突の解消について教える これらは全て以下の動作をしている • ワーキングツリーにあるファイルをインデックスに 登録する 「コミットされたとき、インデックスに登録されたスナップ ショットが歴史に登録される」と覚えておく

Slide 26

Slide 26 text

26 30 インデックスに登録されたスナップショットをコミットする $ git commit -m “commit message” コミットメッセージを指定してコミット $ git commit -a Git管理下にある修正されたファイルを自動で ステージングしてコミット $ git commit -am “commit message” 上記の合わせ技

Slide 27

Slide 27 text

27 30 ワーキングツリーやインデックス、コミット間の差分を表示する $ git diff ワーキングツリーとインデックス間の差分を表示 $ git diff --cached インデックスとリポジトリ間の差分を表示 $ git diff branchname カレントブランチと指定したブランチ間の差分を表示

Slide 28

Slide 28 text

28 30 Gitの様々な設定を行う エイリアス機能 $ git config --global alias.別名 “実際のコマンド” 長いコマンドに別名を与える $ git config --global alias.st “status –s” と設定すると、以後、 $ git st が エイリアスの例 $ git status -s と等価になる

Slide 29

Slide 29 text

29 30 Gitで無視するファイルを指定する test.datというGitが管理していないファイルが存在する時 $ git status -s ?? test.dat と表示される。特に大量のデータファイルがあるときなどに不便 *.dat .gitignoreファイルに以下を記述すると、拡張子datのファイルをすべて無視する .gitignoreファイルをプロジェクトに追加するのを忘れない事

Slide 30

Slide 30 text

30 30 • git init リポジトリの初期化 • git add インデックスへの登録 • git commit インデックスにある状態をコミット • git status 現在の状態を表示 • git diff ワーキングツリー、インデックス、コミット間の差分を表示 • git log 歴史を表示 • .gitignore 無視するファイルを指定する 以下のコマンドや機能について説明した コマンドやオプションが非常に多いので、一度に 覚える必要はない Gitはメッセージが非常に親切なので、ちゃんと読む