概要
Gitのv2.2は1.8とデフォルトの挙動などが違ってくるので、v2.2.4について改めて整理。
- ワーキングディレクトリーの作成は、対象ディレクトリー直下で
git initを実行 - ファイル・ディレクトリーの作成・変更・削除後のコミットの前に、
git addでステージングが必要 - 削除まで含めたステージングのコマンドは、
git add --all .- v1.8と違って
--allオプションは不要
- v1.8と違って
- ステージングエリアの状態をコミットするには、
git commit -m "コメント"でコメントを付して実行
v2.24のインストール
Vagrant-bento/CentOS7の場合Gitがインストール済みだがバージョンが古い。
|
1 2 |
[vagrant@localhost ~]$ git --version git version 1.8.3.1 |
また、yumで普通にインストールする場合も、バージョンが1.8のものしか提供されていない。
そこで、最新バージョンのv2.24をインストールする。
Gitの初期設定
git configコマンドでユーザー名とメールアドレスを設定。この結果カレントディレクトリーに.gitconfigファイルが作成され、内容が記述される。
|
1 2 3 4 5 6 |
[vagrant@localhost ~]$ git config --global user.name taustation [vagrant@localhost ~]$ git config --global user.email "taustation@gmail.com" [vagrant@localhost ~]$ cat .gitconfig [user] name = taustation email = taustation@gmail.com |
必要に応じてGit実行時のカラー設定。
|
1 2 3 4 5 6 7 |
[vagrant@localhost ~]$ git config --global color.ui auto [vagrant@localhost ~]$ cat .gitconfig [user] name = taustation email = taustation@gmail.com [color] ui = auto |
Gitのデフォルトブランチ名はmasterだが、GitHubでリモートリポジトリーを作成する場合、デフォルトブランチの名前がmasterからmainに変更されている。必要に応じてデフォルトブランチの名前を変更しておく。
|
1 2 3 4 5 6 7 8 9 |
[vagrant@localhost ~]$ git config --global init.defaultBranch main [vagrant@localhost ~]$ cat .gitconfig [user] name = taustation email = taustation@gmail.com [color] ui = auto [init] defaultBranch = main |
ワーキングディレクトリー
ワーキングディレクトリーの作成
~/git/testディレクトリーを作成し、そこに移動する。このディレクトリーをワーキングディレクトリーとする。
|
1 |
[vagrant@localhost test]$ mkdir ~/git/test; cd ~/git/test |
ワーキングディレクトリーの初期化
~/git/testディレクトリーでgit initを実行する。
|
1 2 3 4 5 |
[vagrant@localhost test]$ git init Initialized empty Git repository in /home/vagrant/git/test/.git/ [vagrant@localhost test]$ ls -Al 合計 0 drwxrwxr-x. 7 vagrant vagrant 119 8月 5 06:11 .git |
この結果、リポジトリーのスケルトンとして.gitディレクトリーが作成される。
|
1 2 3 4 5 6 7 8 9 10 |
[vagrant@localhost test]$ ls -Al .git 合計 16 -rw-rw-r--. 1 vagrant vagrant 23 8月 5 06:11 HEAD drwxrwxr-x. 2 vagrant vagrant 6 8月 5 06:11 branches -rw-rw-r--. 1 vagrant vagrant 92 8月 5 06:11 config -rw-rw-r--. 1 vagrant vagrant 73 8月 5 06:11 description drwxrwxr-x. 2 vagrant vagrant 4096 8月 5 06:11 hooks drwxrwxr-x. 2 vagrant vagrant 21 8月 5 06:11 info drwxrwxr-x. 4 vagrant vagrant 30 8月 5 06:11 objects drwxrwxr-x. 4 vagrant vagrant 31 8月 5 06:11 refs |
ステータス確認
ワーキングディレクトリー内でgit statusコマンドを実行してみる。
この段階でステージングエリアにファイルがあれば、それがコミット可能として表示される。しかし現時点ではワーキングディレクトリーの初期化直後なので、ステージングエリアにはコミット可能なファイルはない。ファイルを作成またはコピーして、git addするよう示唆されている。
また、ブランチはmasterとなっている。
v1.8と内容は同じだが、各行の頭に'#'がついておらず、一部表現が"Initial commit"→"No commits yet"に変更されたている。
|
1 2 3 4 5 6 |
[vagrant@localhost test]$ git status On branch master No commits yet nothing to commit (create/copy files and use "git add" to track) |
ファイルの新規作成とコミット
初期化したリポジトリーに、新たにファイルを作成してコミットする。手順は以下のとおり。
- ワーキングディレクトリーにファイルを新規作成・コピーする
- ファイルをステージング(ステージングエリアに登録)
- ステージングエリアのファイルをコミット
ファイルの新規作成
“初めてのGit”という内容のテキストファイルsample.txtを作成する。まだステージングされていないことがgit statusで確認できる。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[vagrant@localhost test]$ cat sample.txt 初めてのGit [vagrant@localhost test]$ git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) sample.txt nothing added to commit but untracked files present (use "git add" to track) |
新規ファイルのステージング
作成したファイルをコミットする前に、そのファイルをステージングエリアに登録する必要がある。git add ファイル名でファイルをステージングエリアに登録する。
ワーキングディレクトリー下の対象ファイルを再帰的に全てステージングするにはgit add .とする。
git statusで確認すると、新たにsample.txtが登録されて、変更結果がコミット可能となっている。また、ステージングエリアからファイルを取り除くコマンドも示されている。
|
1 2 3 4 5 6 7 8 9 |
[vagrant@localhost test]$ git add sample.txt [vagrant@localhost test]$ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: sample.txt |
コミット
ステージングエリアのファイルをコミットするには、以下のコマンドでコメントを付けて実行。
git commit -m "コメント"
以下の例では、sample.txtを"First file"というコメントを付けてコミットしている。コミット後のステータスは、コミットすべきファイルがない状態になっている。
|
1 2 3 4 5 6 7 |
[vagrant@localhost test]$ git commit -m "First file" [master (root-commit) 188e42a] First file 1 file changed, 1 insertion(+) create mode 100644 sample.txt [vagrant@localhost test]$ git status On branch master nothing to commit, working tree clean |
コミットの結果はログに記録され、git logで確認できる。
|
1 2 3 4 5 6 |
[vagrant@localhost test]$ git log commit 0fc749b7aa248f5608033952420bb650c13f4109 (HEAD -> master) Author: taustation <taustation@gmail.com> Date: Thu Aug 5 06:20:25 2021 +0900 First file |
ファイル編集とコミット
既作成のファイルを修正した場合のコミットの手順も新規作成と同じで、ステージング→コミットとなる。
ファイルの修正
先に作成したファイルに1行追加している。このままではステージングエリアに登録されていないことがget statusで確認できる。
|
1 2 3 4 5 6 7 8 9 10 11 |
[vagrant@localhost test]$ cat sample.txt 初めてのGit 1行追加 [vagrant@localhost test]$ git status On branch master 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: sample.txt no changes added to commit (use "git add" and/or "git commit -a") |
ステージング
git addで変更後のファイルをステージング。アンステージのコマンドがgit resetからgit restoreに変わっている。
|
1 2 3 4 5 6 |
[vagrant@localhost test]$ git add sample.txt [vagrant@localhost test]$ git status On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: sample.txt |
コミット
ファイルを変更した旨のコメントを付けてコミット。
|
1 2 3 4 5 6 |
[vagrant@localhost test]$ git commit -m "First file modified" [master 8051434] First file modified 1 file changed, 1 insertion(+) [vagrant@localhost test]$ git status On branch master nothing to commit, working tree clean |
ログには最初のコミット以降の一連の操作が記録されている。
|
1 2 3 4 5 6 7 8 9 10 11 12 |
[vagrant@localhost test]$ git log commit 8051434061d69fa704a436132ad9c070555c68d5 (HEAD -> master) Author: taustation <taustation@gmail.com> Date: Thu Aug 5 06:26:40 2021 +0900 First file modified commit 0fc749b7aa248f5608033952420bb650c13f4109 Author: taustation <taustation@gmail.com> Date: Thu Aug 5 06:20:25 2021 +0900 First file |
ファイル削除とコミット
ファイル削除
まずrmコマンドでファイルを削除する。
|
1 |
[vagrant@localhost test]$ rm sample.txt |
ステージング
v1.8ではgit addのデフォルトオプションが--ignore-removalで、削除の時には--allオプションを明示的に指定する必要があったが、v2.24ではその必要はない。
削除済みファイルを対象とするので"."を指定。
この結果、sample.txtが削除されたことがステージングされている。
|
1 2 3 4 5 6 |
[vagrant@localhost test]$ git add . [vagrant@localhost test]$ git status On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) deleted: sample.txt |
この結果をコミットすると、無事sample.txtが削除される。
|
1 2 3 4 |
[vagrant@localhost test]$ git commit -m "Committed including deletion" [master 2602291] Committed including deletion 1 file changed, 2 deletions(-) delete mode 100644 sample.txt |
ログの内容は以下の通り。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[vagrant@localhost test]$ git log commit 2602291540264fcd996e3509476c0e8554cf92ad (HEAD -> master) Author: taustation <taustation@gmail.com> Date: Thu Aug 5 06:30:16 2021 +0900 Committed including deletion commit 8051434061d69fa704a436132ad9c070555c68d5 Author: taustation <taustation@gmail.com> Date: Thu Aug 5 06:26:40 2021 +0900 First file modified commit 0fc749b7aa248f5608033952420bb650c13f4109 Author: taustation <taustation@gmail.com> Date: Thu Aug 5 06:20:25 2021 +0900 First file |