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. :)

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Reddit
  • Slashdot
  • description
  • LinkedIn
  • Live
  • StumbleUpon
  • Technorati

Comments (2)

Himanshu ChhetriJanuary 27th, 2010 at 10:46 am

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.

PrasannaJanuary 28th, 2010 at 11:52 am

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.

Leave a comment

Your comment