Fun with Git

So, git has the —all command, which adds all tracked and untracked files for staging to commit. I really dislike this because I wanted something that would just add all the tracked files for staging to commit. So, a friend of mine in the office said that he had a shell script which did it so I give you git add for only tracked files:

git status | grep "modified: " | awk '{print $3}' | xargs git add


Does “git add -u” do what you need?

“git commit -a” does not add new files too

Caveat emptor, that shell snippet doesn’t look like it’ll work with paths that contain spaces.

It would probably make more sense to use the --porcelain option to git status as that is guaranteed not to change between versions of git

Also, it is unnecessary to use grep x | awk as awk is designed to do the line filtering itself.

So something along the lines of:

git status --porcelain | awk '/^.M/ {print substr($0,4)}' | xargs git add

The problem with this code is that it doesn’t handle the renamed form (X -> Y) of file names git status might output. Also, xargs does correctly handle space-containing file names on my system, but I don’t know if that is universal.

Leave a comment

About cyocum

user-pic Celticist, Computer Scientist, Nerd, sometimes a poet…