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).

8
  • you can perform git reset then git adds again
    – potame
    Sep 13, 2016 at 19:10
  • When you did git reset your will lost files list. It doesn't work.
    – Kirby
    Sep 13, 2016 at 19:11
  • Which files are lost? I don't get what you mean
    – potame
    Sep 13, 2016 at 19:13
  • Did you mean "readd". Perhaps, yes, "stage". If we're talking about result of git add.
    – Kirby
    Sep 13, 2016 at 19:14
  • @potame, git reset will drop files from "stage". Let's imagine we have 20 files in different directories. It's a disaster. :)
    – Kirby
    Sep 13, 2016 at 19:17

4 Answers 4

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

3
  • I suppose you mean git add $(git diff --name-only --cached). Yeah, it works.
    – Kirby
    Sep 13, 2016 at 19:22
  • 2
    FYI: breaks for deleted files. But super useful
    – teddybear
    Jun 10, 2019 at 0:54
  • Very useful command, thanks. Only problem is, it gives out full file paths, so if you are not in the root of your git repo, this wont work
    – Husmus
    Mar 15, 2021 at 20:32
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.

1
  • 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, 2016 at 8: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 :/:
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.