Wednesday, December 16, 2009

Using Git locally for revisions and backups

Git is great. You will probably see me mention it alot here. Or you might not, since it is one of those tools that should just become invisible in the background. If you are used to working on a centralized version control system (like Subversion) you probably have a scenario like this:
  • Create a branch on the server for your changes and copy from the trunk.
  • Work locally, and periodically commit changes back to the branch as a backup.
  • Merge the changes back to the trunk when they are ready.
That all works well. But when you are working on something locally (for example, some CakePHP web pages in a local www directory for your local apache installation) you want the same kinds of benefits: you want to be able to roll back to previous versions if needed and you want to have the assurance of backing up your code somewhere. In git, you can do those as two separate functions.

Version control

Set up git in the www directory:

go to the www directory
git init
git add .
git commit -a

(Note: you can do this before you do any work, or even after you already have a bunch of files.)
Now you can add, commit, etc. to your heart's content and use all of git's version control features. But you are getting nervous because you don't have this backed up. Perhaps this directory is not included in your online backup system. Perhaps you are a moron and you simply don't backup your computer at all. In any case, you want to make sure your repository is backed up somewhere.


You can always use git's built in remote capabilities with its push and pull features to sync up to a remote repository and you will have everything backed up just as well as you do in the Subversion scenario above. But perhaps you just have your laptop and a free service like DropBox or Mozy and you want to make sure your work gets copied there. You can use git's bundle feature to push your entire repository to a single file, and restore the repository from that bundle whenever you like (also handy for moving your repository from place to place without navigating any connectivity issues).
To backup, do the following: git bundle create /path/to/dir/bundle.bnd master. You can replace master with the specific revisions you want included. The result will be a single file that is memory stick friendly, and which preserves the file information that might be lost with a file system change. And all of your repository (at least the revisions you listed) are present.
To restore, go to the directory you want to restore into (even if it is a different directory) and create an empty git repository, then simply pull from the bundle:
git init
Get the list of revisions with git ls-remote /path/to/dir/bundle.bnd which will probably result in refs/heads/master
git pull /path/to/dir/bundle.bnd refs/heads/master
You should be back in business, and able to work in the newly restored location with your repository intact.

No comments:

Post a Comment