0

In Git, whenever I need to stage files I can use

git add .

to add all the files that have modified since my last commit. However when I remove one file I have to remove it one by one by doing

git rm 'filename'

And git rm . simply deletes everything that was being tracked by git.

I was wondering if there is a command similar to git add . but for git rm.

Thanks :)

EDIT:

I think I didn't explain myself correctly. Lets say I have some files in my git directory which are being tracked by git and lets call them file1, file2, file3. If I where to remove (rm command) file1 and file3 then git status will tell me that those files where deleted and to execute git rm "filename" to update what will be committed. So the question is, is there a way to add this removing action of both files with one single command?

If I where to modify/add different files then I could simply run git add . and all modifications/additions will be added to the staging area so as to be committed.

tupini07
  • 518
  • 5
  • 13
  • `git add .` stages all modified and untracked (bot not ignored) files for commit. If you need to remove multiple files, i.e. a directory, you could use a mask, like: `git rm -r folder_with_unwanted_files/*`, or a mask for (as example) all jpegs: `git rm -r *.jpg`. Try `--dry-run` option to see what will be done. – Kleskowy Oct 22 '14 at 22:20
  • Thank you for your answer :) .. Yes I'm aware of what you say. I was just wondering if there is a exact way to duplicate the behavior of `git add .` but with `git rm` – tupini07 Oct 23 '14 at 01:52
  • maybe `git ls-files -m | xargs git rm` or something similar – Andrew C Oct 23 '14 at 03:11
  • You can remove the files manually and tell git afterwards about it with `git add -A ` where `` could be `.`. – Sascha Wolf Oct 23 '14 at 05:25
  • 1
    what do you need? to remove untracked files or to undo the changes to all tracked files? – Lluís Oct 23 '14 at 06:18
  • none :) let's say I have a file which I delete and Git tells me (when I access `git status`) that that file was removed and I should run `git rm` on that file to add the removing action to the staging area. I wanted to know if there is a way to 'bulk rm' this files – tupini07 Oct 24 '14 at 02:49
  • if you run `git rm filename` the delete action will be added to the staging area. For `git rm .` to behave the same way as `git add .` I assume you want all tracked files with unstaged changes to be removed – Leon Oct 24 '14 at 04:21

3 Answers3

2

Use git add -u/git add --update or git add -A/git add --all. The former will stage modified and deleted files while the latter also stages new (untracked) files.

Magnus Bäck
  • 11,381
  • 3
  • 47
  • 59
  • Wait, how does this answer the question? The OP is trying to `rm` multiple files, not `add`. – pattivacek Oct 23 '14 at 13:22
  • S/he wants to stage the deletion of files that s/he deleted with `rm filename` (as opposed to `git rm filename`). I'll rephrase my answer somewhat. – Magnus Bäck Oct 23 '14 at 14:12
1

You might want to checkout the answere here

The command is basically this: git rm $(git ls-files --deleted)

Community
  • 1
  • 1
krngrvr09
  • 123
  • 2
  • 9
0

For anyone who is interest I found a much cleaner solution to this.

The default when you execute git add . is:

git add --ignore-removal <paths>

This default behavior is exactly the root of this question (how to add the removals and changes to the staging area using just one instruction). This can be easily done with the command:

git add --all .

which does not ignore removal.

Hope it helps somebody as it helped me :)

tupini07
  • 518
  • 5
  • 13