12

I have a bunch of deleted files in my working branch that I'm not sure I want to commit yet. I need to commit my modified files as needed though.

This is what I came up with:

git add `git status | grep modified | cut -f2 | cut -f4 -d' '`

This answer does something similar but isn't much better: https://stackoverflow.com/a/8277826

So is there a simpler solution?

Community
  • 1
  • 1
Ziglr
  • 133
  • 1
  • 5

1 Answers1

14

Just use git add --no-all . (Git v. 2.0+) or git add . (Git v. 1.x). This will pick up any files it can find by traversing the current directory, which naturally won't include deleted files.

Of course, this also picks up any untracked files too. If you need to avoid those, then you can use a more complicated expression. It's similar to yours, but it uses the output that's intended for scripting (so it's more stable and easier to parse):

git diff-files -z --diff-filter=M --name-only --relative | xargs -0 git add
Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • 1
    This isn't simpler which was the point of the question, but mechanically it's less likely to break. Accepted. – Ziglr Jan 22 '13 at 16:33
  • @Ziglr: Well the `git add .` is definitely simpler, as long as you don't have untracked files. But yeah, my command that works in the presence of untracked files is not simpler, but it is more reliable. – Lily Ballard Jan 22 '13 at 18:53
  • 2
    Since version 2.0+ `git add .` also picks removed files. The new replacement for old-school `git add .` is `git add --no-all .` – Nick Volynkin Jul 01 '15 at 20:36
  • Note that `diff-files` is relative to the top-level Git repo dir, but `git add` is relative to the current dir. So this command won't work if you are not in the top level directory. – Kyle Strand Jun 08 '17 at 21:19
  • I don't understand this answer. `git add .` has always been adding deleted files as well. This is incorrect – Jérôme MEVEL Jun 23 '22 at 07:06