Search code examples
gitgit-resetgit-index

How to stage subsequent changes to a file that has already been staged in index?


I made some changes to a file and then staged it in the index for the next commit. Then I realized that I needed some more changes to do. Instead of unstaging it, is there a way to capture these subsequent changes before finally committing?

Would it be

git add -u filename (as many times as I make changes to the filename file)

instead of

git reset

git add filename

?


Solution

  • You can call git add filename on the same file as many times as you want.

    It'll always add any existing non-staged changes, regardless of whether the file already has changes in the index.


    The -u flag (shortcut for --update) serves a different purpose. From the man page:

    Update the index just where it already has an entry matching <pathspec>. This removes as well as modifies index entries to match the working tree, but adds no new files.

    In other words: if filename has never been tracked before, git add -u filename won't add it to the index. But for files that are already tracked, using the -u flag makes no difference.