Question

The section of the book:

http://git-scm.com/book/en/Git-Basics-Recording-Changes-to-the-Repository#Staging-Modified-Files

says that if I change file A, run 'git add A' and then change file A again, run 'git commit' on this file, only previously staged changes will be committed. To commit unstaged changes - add file again and then commit.

After some testing, however it turns out that git commits all changes of the file A, even those that were made after the file had been staged initially.

Can anyone shed some light on this matter? Thanks.

EDIT: I am not using -a option It looks like if I specify the file explicitly for "git commit A" (I am not using -a option), it commits unstaged changes as well. Committing without file "git commit" asks to enter a commit message and then behaves as described in the book.

Was it helpful?

Solution

How are you determining that it "commits all changes of the file A"? I can't reproduce this on my version of git. To reiterate what I think you're saying, if you follow this process:

$ git add A                 #initial commit of A
$ git commit -m first
$ <modify A>
$ git add A                 #stage first set of changes
$ <modify A again>
$ git commit -m second      #without staging second modifications

What you should see at this point is that the first set of changes, but not the second, was committed. git show HEAD:A should display the file as it was before you made the second modifications. However, the second modifications will still be in your working copy of the file (cat A), and git status should report that there are unstaged changes in A. That's exactly what I got when I tried to replicate this, and is what should be expected.

If that's not what you're seeing, then either you did something different than what was listed above, or somehow the version of git you're running is broken.

OTHER TIPS

You're probably adding the -a option when you commit, which does another add on everything before the commit. It works as described when you omit the -a.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top