コミット済みとステージ済みとステージ未済

git commitで歴史に記録されるのは、ステージに乗せたその時のファイル内容であり、あくまでgit addした瞬間のものなのであって、git commitした瞬間ではない。

流れを確認するシェルスクリプト

  1. リポジトリ作成
  2. 追跡するファイルをステージに乗せる
  3. 一部をさらに変更し、それはステージ更新しないまま、コミット
  4. リポジトリの状態を確認
#!/bin/bash

# 一時ディレクトリを作り直す
rm -fr git-tmp; mkdir git-tmp; cd git-tmp
# スクリプト表示しながら実行
set -v

#--------------------------------------
# リポジトリ作成: repo
mkdir repo
cd repo
git init


#--------------------------------------
# 追跡するファイル
echo 'A' >> abc
echo 'X' >> xyz
git add abc xyz
git commit -m 'コミット1'


#--------------------------------------
# 追跡していないファイル
touch 非追跡ファイル 無視ファイル
echo '無視ファイル' >> .gitignore


#--------------------------------------
# ステージに乗せる
echo 'B' >> abc
echo 'Y' >> xyz
git add abc xyz

# ステージ後に変更
echo 'C' >> abc


# リポジトリの状態
git status


#--------------------------------------
# コミット
git commit -m 'コミット2'


# リポジトリの状態
git status

#
cat abc

実行結果

#--------------------------------------
# リポジトリ作成: repo
mkdir repo
cd repo
git init
Initialized empty Git repository in /home/alice/git-tmp/repo/.git/


#--------------------------------------
# 追跡するファイル
echo 'A' >> abc
echo 'X' >> xyz
git add abc xyz
git commit -m 'コミット1'
[master (root-commit) d09ae49] コミット1
 2 files changed, 2 insertions(+)
 create mode 100644 abc
 create mode 100644 xyz


#--------------------------------------
# 追跡していないファイル
touch 非追跡ファイル 無視ファイル
echo '無視ファイル' >> .gitignore


#--------------------------------------
# ステージに乗せる
echo 'B' >> abc
echo 'Y' >> xyz
git add abc xyz

# ステージ後に変更
echo 'C' >> abc


# リポジトリの状態
git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
# modified:   abc
# modified:   xyz
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified:   abc
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
# .gitignore
# 非追跡ファイル


#--------------------------------------
# コミット
git commit -m 'コミット2'
[master 75e066e] コミット2
 2 files changed, 2 insertions(+)


# リポジトリの状態
git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified:   abc
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
# .gitignore
# 非追跡ファイル
no changes added to commit (use "git add" and/or "git commit -a")

#
cat abc
A
B
C