Refresh staged files

In git, is there any (simple) way to modify the index so that only changes to files which are already in it are added? It sounds kind of complicated, but what I want to achieve is simple.

Lets say my index looks like this (slightly stripped git status output):

  • Does git de-duplicate between files?
  • How to edit Git “add patch” hunks/diffs/lines during selective staging?
  • Is it possible to store only a checksum of a large file in git?
  • Why won't “git add *” add files that “git status” says are unstaged?
  • How can I reorder/combine commits using Git rebase?
  • Where's the 3-way Git merge driver for .PO (gettext) files?
  • # Changes to be committed:
    #       modified:   A
    #       modified:   B
    #
    # Changed but not updated:
    #       modified:   B
    #       modified:   C
    #
    # Untracked files:
    #       D
    

    Some changes to B are in the index, some aren’t.
    C is not staged at all.

    How can I update B in the index (stage its unstaged changes) without adding C?

    I.e. I would like for the index to look like this:

    # Changes to be committed:
    #       modified:   A
    #       modified:   B
    #
    # Changed but not updated:
    #       modified:   C
    #
    # Untracked files:
    #       D
    

    In this simple case it can of course be achieved with a simple git add B, but I would like to know if there’s a simple answer for the general case. I tried git add --refresh, but if I understand correctly, that only updates stat info.

  • Does git-svn handle moved files
  • What is a quilt patchset?
  • How to get Git on Windows to ignore symbolic links
  • What's best way to work with git on multiple master branch?
  • How do you tell git to permanently ignore changes in a file?
  • git: blame the previous version of each line changed in a given commit
  • 2 Solutions collect form web for “Refresh staged files”

    The following command will update the index to contain the other changes in B that has not been staged yet:

    git update-index --again
    

    I don’t know of a completely trivial way to do this, but:

    git status --porcelain
    

    will show file B (and only B) as state “MM”, so:

    git status --porcelain | grep ^MM | cut -d' ' -f 2
    

    will produce a list of such files.

    There’s no harm in “re-adding” A, though.

    You can also use git diff-index --cached --name-status HEAD. (Might need this if your git is too old to have git status --porcelain.)

    Git Baby is a git and github fan, let's start git clone.