1

ScottChaconの「ProGit」の本を使って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か?もしそうなら、私がそれを追加しなかったとしても、なぜそれは2番目の変更をコミットするのですか?

4

1 に答える 1

2

git commit現在インデックスにあるもの、したがって明示的に追加したものをコミットします。

ただし、あなたの例でgit commit README.TXTは、指定したファイル、つまり現在のファイルをコミットすることを行っていますREADME.TXTgit commit -m "only hello"インデックスをコミットするだけです。

于 2012-10-20T16:22:38.437 に答える