Tuesday, May 13, 2008

Git Stash Lets You Snapshot All Your Stuff

I keep the home directory on my laptop under git.

Last night, I read a trick in a good article, Git from the bottom up, by John Wiegley, that I tried out this morning.

git stash stashes away the current state of your world (the index), and replaces it with the HEAD. git stash apply restores the stash. The pair snapshots your work, but leaves it unchanged.

I now run this command as an hourly cron job:
#!/bin/bash
# snapshot the current repository

git stash
git stash apply
Here's the crontab entry:
# | hour (0-23)
# | | day of the month (1-31)
# | | | month of the year (jan-dec)
# | | | | day of the week (sun-mon, with 0=sun)
# | | | | | command
# | | | | | |
#

01 * * * * ~/bin/snapshot &> /dev/null
The snapshots, however, are all accessible through the reflogs, which means that my states are stashed, hourly, invisibly to me, but I can get them back pretty easily.

If there have been no changes, git stash notices, and does nothing, so the hourly job doesn't fill my logs with irrelevant entries.

Very nice.

No comments: