Be Careful with Find-Replace in Git Projects

You need to be careful with the find command inside a git project.

Imagine you need to change a URL.

Terminal
1
2
3
4
5
6
7
8
9
[mburke]$ ack "/resources/"
data/_mobile_navigation.html
5: <li><a href="/resources/">Blog</a></li>

data/_nav_footer.html
3: <li><a href="/resources/">Blog</a></li>

data/_navigation.html
16: <li><a href="/resources/">Blog</a></li>

Hey, only three! Now craft a nice find replace with sed.

Terminal
1
2
3
4
[mburke]$ find . -type f -exec sed -i '/\\/resources\\//\\/blog\\//gi' {} \;
[mburke]$ git status
error: garbage at end of loose object 'bc2ed3f91aa91052805f2581ca7b1eeee7a5a7fa'
fatal: loose object bc2ed3f91aa91052805f2581ca7b1eeee7a5a7fa (stored in .git/objects/bc/2ed3f91aa91052805f2581ca7b1eeee7a5a7fa) is corrupt

Oops.

ack has the nice behavior of ignoring the .git/ directory. find . is not so helpful.

You’ve just clobbered over some commit objects in the git database. By changing their content, you’ve change the hash checksums, and git reports an error for just about every command.

Luckily, in my case, I was all pushed up and could just re-clone the repository. If this happens to you and you have un-pushed changes, its going to take a good bit of git spelunking to get back to a stable state.

Be careful out there.