How git rm . should behave
So when you do:
git add .
It automatically adds files to your index ready to be committed right? But when I did:
git rm .
I assumed it would remove files/folders that you have removed from the filesystem, kinda like the inverse of how git add . should be. But it doesn’t, it gives you something that about not removing the directory you are currently working in. Here’s what it says in the git manual about git rm:
Remove files from the index, or from the working tree and the index. git-rm will not remove a file from just your working directory. (There is no option to remove a file only from the work tree and yet keep it in the index; use /bin/rm if you want to do that.) The files being removed have to be identical to the tip of the branch, and no updates to their contents can be staged in the index, though that default behavior can be overridden with the -f option. When —cached is given, the staged content has to match either the tip of the branch or the file on disk, allowing the file to be removed from just the index.
Notice they say use bin/rm to remove a file from the work tree and still keep it in the index… what? Ok, but for me I’ll usually just want to remove a file/directory from both the work tree and the index, it just puts more work on the developer to manually have to remove it from the git index when you have just removed it from the filesystem. Granted it’s a simple git rm whatever/file/you/deleted/ but once you multiply that by a bunch of files in directories you have removed, it becomes tedious. So what do we do?
Sed, Grep and Bash to the rescue!
By using the power of sed and grep you can now freely delete your files and directories as you wish without worrying about manually git rm-ing everything. I decided to write a quick bash function to give me that behavior, drop this in your ~/.bash_profile or ~/.bashrc and you’ll be good to go:
# removes all deleted files / directories
# kinda like what git rm . should do
git_rm_all (){
git st | grep deleted | sed -e 's/deleted: *//' | sed 's/# *//' | xargs git rm
}
