Removing editor swap files before commits in git
I use git a lot. And I use just about any text editor in my system to edit files. And they tend to come with baggages of their own. Albeit useful, the swap files tend to linger on and eventually end up in my git repository. Here’s how to avoid that scenario.
Git (okay I remember this from doing the same thing in subversion, so nothing new) has something called pre-commit. Pre-commit is just a script/action you can perform on the repository before committing. So, here’s what you need to do
I’ll use vim here, but use whatever you like
$ vim .git/hooks/pre-commit
and put this:
#!/bin/sh
find . -name ‘*~’ -exec rm \;
find . -name ‘*.swp’ -exec rm \;
This removes two types of files, those generated by vim and gedit. There should be a much more elegant way to include more file types, I’ll update them as I refine my pre-commit hook more.













Alternatively just add “.swp” or any other files you want Git to ignore to “.gitignore” in your Git repository. That way you avoid the hassle of using pre-commits althought Git’s pre/post hooks are very handy.
Yeah, I thought of that, I just liked the idea of just cleaning up things. Or even running a build and only commit if the program completes the build.