Luke Hagan

git addremove

Save some keystrokes while working with git.

I’ve finally given in to peer pressure and switched to using Git for version control[^1]. For some reason, I’d been hanging on to Mercurial, and even using hg-git to interact with GitHub. Well, I’m done with all that, but I already miss using hg addremove to automatically add untracked files and mark missing ones as removed. So thanks to these two posts, I added an alias to my .profile to replicate the addremove functionality in Git as git-addremove:

alias git-addremove='git add .; git ls-files --deleted | xargs --no-run-if-empty git rm'

Update 9/3/2011: The above alias did not work on my new system. Apparently, the version of xargs that ships with OS X doesn’t support the --no-run-if-empty option. I must have installed the GNU version of xargs on my old system at some point, which does support that option. Anyway, here’s a new version that should work anywhere:[^2]

alias git-addremove='git add . -v; git ls-files --deleted | while read FILE; do git rm $FILE; done'

[^1]: Except for this site, which will stay on Mercurial for the time being.

[^2]: I also added the verbose flag to git add because I think it’s nice to see what’s going on.