Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simpler way to stage only modified files in Git (not deleted)

Tags:

git

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?

like image 406
Ziglr Avatar asked Jan 16 '13 21:01

Ziglr


1 Answers

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
like image 72
Lily Ballard Avatar answered Nov 15 '22 14:11

Lily Ballard