我正在尝试使用 Scott Chacon 的“Pro Git”一书来学习 git。在解释如何暂存修改后的文件(第 18 页)时,我理解git add
文件被安排提交,然后提交git commit
。它提到提交已完成,只有添加的更改才会被实际提交,如果我再次更改文件,我必须在提交之前再次添加它,以便所有更改都被提交。文字说:
事实证明,Git 会完全按照您运行
git add
命令时的状态暂存文件。如果您现在提交,则上次运行git add
命令时的文件版本是它将如何进入提交,而不是版本运行时在工作目录中显示的文件git commit
。如果在运行后修改文件git add
,则必须git add
再次运行以暂存文件的最新版本。
但是,我自己尝试时看到了不同的行为:
$ git status #start clean
#On branch master
nothing to commit (working directory clean)
$ echo "hello" >> README.TXT
git-question> git add README.TXT #added change to README
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: README.TXT
#
$ echo "good bye" >> README.TXT #change README after adding
$ git status #now 'hello' is added to be committed but not 'good bye'
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: README.TXT
#
# 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: README.TXT
#
$ git commit -m "only hello" README.TXT #commit, i would expect just 'hello' gets commited
[master 86e65eb] only hello
1 file changed, 2 insertions(+)
$ git status #QUESTION: How come there's nothing to commit?!
# On branch master
nothing to commit (working directory clean)
所以问题是:不应该git commit
只提交添加的更改git add
吗?如果是这样,为什么即使我没有添加它,它也会提交第二次更改?