7

We have modified files in path/to/another/ and path/to/main/.
Files in path/to/main/ already added into git cache but we have updated path/to/main/Bar.php file AGAIN. We now have the following situation:

$ git status
[...]
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   path/to/main/Foo.php
        modified:   path/to/main/Bar.php

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:   path/to/main/Bar.php
        modified:   path/to/another/Aaa.php
        modified:   path/to/another/Bbb.php

(note that path/to/main/Bar.php shows up twice)

I need a command which could readd files which were added before without using particular paths.

P.S. git add --update will add all these files. It doesn't work.

P.P.S. This command should be able to readd modified: and new file: types.

UPD1

Thanks to @AyonNahiyan, yeah, it can work in bash. But maybe there is a command without using bash tricks (subcommands).

Kirby
  • 2,847
  • 2
  • 32
  • 42

4 Answers4

20

This shows list of files that are only staged:

git diff --name-only --cached

Now stage these files with their new changes(It will work in bash)

git add $(git diff --name-only --cached)

PS: (precondition) You need to be at the root of your git repo while running these commands. Running in any subfolder won't work

Ayon Nahiyan
  • 2,140
  • 15
  • 23
12

git update-index can be used for this purpose.

In particular,

git update-index --again

should work.

The --again option selects the files already in index that are different from HEAD.

And the actual action of update-index is to pull into index the new contents of those files.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
  • 1
    Yeah, it is. But there is a performance issue. I have 24k+ files in git and I was trying to readd the only file and it's too slow. :( Perhaps it tries to add all files from cache. – Kirby Sep 14 '16 at 08:15
7
git update-index --again

will do what you want. Note that this command operates on the current working directory (and subdirectories, recursively) by default. If you want to apply the command to the whole repo, add a pathspec:

git update-index --again :/:

:/: here means "the root of the working tree".

P.S. you can add an alias for this command to your .gitconfig file, e.g.:

[alias]
    readd = update-index --again :/:
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
0

The answer from Ayon works. A possible fix for deleted files is to add another parameter.

--diff-filter=d

This excludes all deleted files from "git add". Further information on how to use this parameter can be found on this thread: Filter git diff by type of change