How to re-add added files (update staged files only) in git?
Asked Answered
T

4

9

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

Traylor answered 13/9, 2016 at 19:7 Comment(8)
you can perform git reset then git adds againCommon
When you did git reset your will lost files list. It doesn't work.Traylor
Which files are lost? I don't get what you meanCommon
Did you mean "readd". Perhaps, yes, "stage". If we're talking about result of git add.Traylor
@potame, git reset will drop files from "stage". Let's imagine we have 20 files in different directories. It's a disaster. :)Traylor
Then use git reset -- path/to/main/Bar.php if you only want to remove a single file. Then git add path/to/main/Bar.php again.Common
Let us continue this discussion in chat.Traylor
possible dupe of Refresh staged filesIrita
F
21

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

Fino answered 13/9, 2016 at 19:17 Comment(3)
I suppose you mean git add $(git diff --name-only --cached). Yeah, it works.Traylor
FYI: breaks for deleted files. But super usefulFeatheredge
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 workBodgie
I
14

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.

Indelible answered 13/9, 2016 at 20:56 Comment(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.Traylor
T
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 :/:
Torry answered 25/1, 2021 at 11:29 Comment(0)
B
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

Bulldog answered 26/1, 2022 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.