D:\code\gt>git init
Initialized empty Git repository in D:/code/gt/.git/
D:\code\gt>echo Zero > a
D:\code\gt>git add a
D:\code\gt>git commit -m a
[master (root-commit) 392580e] a
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 a
D:\code\gt>echo One > a
D:\code\gt>git add a
D:\code\gt>git commit -m another
[master 271efba] another
1 files changed, 1 insertions(+), 1 deletions(-)
D:\code\gt>echo Second > a
D:\code\gt>git add a
D:\code\gt>git commit -m "yet another"
[master 8d2041e] yet another
1 files changed, 1 insertions(+), 1 deletions(-)
D:\code\gt>git status
# On branch master
nothing to commit (working directory clean)
D:\code\gt>git checkout 271ef a
D:\code\gt>git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: a
#
As you can see, I checkout the file in the state it had at the 2nd commit. However, I don’t understand why it’s already added to the index. Why isn’t git letting me do the add?
This is a point which isn’t entirely clear from the
git checkoutdocumentation, so it’s a good question.When you do:
… the working copy and the index for
PATHare updated with the version fromCOMMIT. So, relative toHEAD, there are indeed changes to that file staged after running that command.