Share
Embed
Transcript
30 slides
-
3 30 $ git switch –c branch コマンド プロンプト コマンド
オプション 引数 Gitは「git」の後にコマンドやオプション、引数を指定する -
4 30 $ git help コマンド名 以下でヘルプが参照できる Gitに限らず、近代的なツールはヘルプが 非常に充実していることが多い 「公式ヘルプを読む」
「エラーメッセージを読む」 この二つが上達の近道 -
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は問題が起きたときに「こうすれば?」と提案してくれる →エラーメッセージを読む -
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 -
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 コマンドとの対応関係に注意 -
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」であることがわかる -
9 30 Gitは以下のような操作を行う 1. リポジトリの初期化 2. ファイルをインデックスに登録 3. コミット 4.
ファイルを修正 5. ステージング&コミット 以後繰り返し -
10 30 $ cd $ mkdir project $ cd project
ホームディレクトリへ移動 projectディレクトリを作成 projectディレクトリへ移動 $ git init Initialized empty Git repository in C:/path/to/project/.git/ カレントディレクトリをGitリポジトリとして初期化 -
11 30 $ echo “Hello” > README.md $ git add
README.md 新しいファイルを作成し、インデックスに登録する Home Project .git README.md -
12 30 $ git status On branch main No commits
yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: README.md • 現在のカレントブランチはmain • まだ歴史はない • 現在コミットした場合に反映される修正はREADME.mdの追加 以下のようなことが書いてある -
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 # • コミットメッセージを書け • 行頭に「#」がある行は無視される • 空メッセージならコミットを中断する -
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の仕組み」にて -
15 30 歴史が作られたのでgit logで履歴が参照できる $ git log commit 9d8aab06e0a1f1b152546db086fe7737a02526e1 (HEAD
-> main) Author: H. Watanabe <[email protected]> Date: Thu Sep 16 17:15:41 2021 +0900 initial commit • 9d8aab0というコミットハッシュのコミット • mainブランチがそのコミットを指している • カレントブランチはmainブランチ (HEAD -> main) • 著者とメールアドレスはH. Watanabe <[email protected]> • コミットされた日付が2021年9月16日 • コミットメッセージがinitial commit 以下のような情報がわかる -
17 30 git statusで状態表示 $ git status On branch main
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: 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 ワーキングツリーとインデックスに差異がある -
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で差分を表示できる ワーキングツリー インデックス リポジトリ -
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 -
20 30 $ git status On branch main Changes to
be committed: (use "git restore --staged <file>..." to unstage) modified: README.md 状態を表示する • 「Changes not staged for commit:」→「Changes to be committed:」 • ステージングを取り消すならgit restore --staged しろ 状態表示の簡略版 $ git status -s M README.md インデックスとリポジトリに差異がある -
21 30 $ git commit -m "updates README.md" [main a736d82]
updates README.md 1 file changed, 1 insertion(+) 修正をコミットする 新たにa736d82というコミットが作られ、歴史に追加された main main a736d82 9d8aab0 9d8aab0 -
22 30 $ git log commit a736d82251279f592a25e38503bb9130bac12481 (HEAD -> main)
Author: H. Watanabe <[email protected]> Date: Thu Sep 16 19:13:34 2021 +0900 updates README.md commit 9d8aab06e0a1f1b152546db086fe7737a02526e1 Author: H. Watanabe <[email protected]> Date: Thu Sep 16 17:15:41 2021 +0900 initial commit 履歴の一行表示 $ git log --oneline a736d82 (HEAD -> main) updates README.md 9d8aab0 initial commit 二つコミットができている -
23 30 リポジトリを初期化し、ファイルをインデックスに登録し、最初のコミッ トをして、修正してさらにコミットをする、という一連の動作を確認した。 その過程で以下のコマンドを使った。 • git init リポジトリの初期化 •
git add インデックスへの登録 • git commit インデックスにある状態をコミット • git status 現在の状態を表示 • git diff ワーキングツリー、インデックス、コミット間の差分を表示 • git log 歴史を表示 以下、それぞれのコマンドの詳細について説明 -
24 30 リポジトリを初期化する $ git init カレントディレクトリをGitで初期化する $ git init
project projectというディレクトリを作成し、Gitで 初期化する $ git init --bare project.git project.gitというベアリポジトリを作る -
25 30 git addには三つの役割がある • リポジトリの管理下にないファイルを管理下に置く • リポジトリの管理下にあるファイルをステージングする • Gitに衝突の解消について教える
これらは全て以下の動作をしている • ワーキングツリーにあるファイルをインデックスに 登録する 「コミットされたとき、インデックスに登録されたスナップ ショットが歴史に登録される」と覚えておく -
26 30 インデックスに登録されたスナップショットをコミットする $ git commit -m “commit message” コミットメッセージを指定してコミット
$ git commit -a Git管理下にある修正されたファイルを自動で ステージングしてコミット $ git commit -am “commit message” 上記の合わせ技 -
27 30 ワーキングツリーやインデックス、コミット間の差分を表示する $ git diff ワーキングツリーとインデックス間の差分を表示 $ git diff
--cached インデックスとリポジトリ間の差分を表示 $ git diff branchname カレントブランチと指定したブランチ間の差分を表示 -
28 30 Gitの様々な設定を行う エイリアス機能 $ git config --global alias.別名 “実際のコマンド”
長いコマンドに別名を与える $ git config --global alias.st “status –s” と設定すると、以後、 $ git st が エイリアスの例 $ git status -s と等価になる -
29 30 Gitで無視するファイルを指定する test.datというGitが管理していないファイルが存在する時 $ git status -s ?? test.dat
と表示される。特に大量のデータファイルがあるときなどに不便 *.dat .gitignoreファイルに以下を記述すると、拡張子datのファイルをすべて無視する .gitignoreファイルをプロジェクトに追加するのを忘れない事 -
30 30 • git init リポジトリの初期化 • git add インデックスへの登録
• git commit インデックスにある状態をコミット • git status 現在の状態を表示 • git diff ワーキングツリー、インデックス、コミット間の差分を表示 • git log 歴史を表示 • .gitignore 無視するファイルを指定する 以下のコマンドや機能について説明した コマンドやオプションが非常に多いので、一度に 覚える必要はない Gitはメッセージが非常に親切なので、ちゃんと読む