Thursday, September 1, 2011

Go back in time with Version Control

Like everybody else, you probably have things that you wish you could go into the past and meddle with. While you can't do that in real life. It's possible to on a computer using Version Control software.

Version Control is a fairly simple topic at it's most basic form, and can grow into something much more complex for larger scale projects. At it's simplest, after saving a file, and you're happy with it's current contents, you create a version, which should you need to, you can revert back to. Creating a new version is almost like saving the file(s) using a different file name each time you want to, but it's all managed behind the scenes. The files are stored in a repository, and the SVN software knows how to read the changes made to each file in each revision.

My software of choice is Subversion (abbreviated SVN) purely because it's what I was taught to use, and I understand it, there are others out there (GIT for example). What you'll need is a SVN server (can be your own computer, or an actual webserver) to manage the repository, and your computer with some SVN software on it.

It works by keeping the master copy of the repository on the server, with all of the revision changes, and you "check out" the most recent or HEAD version to your computer. Lets get started with creating a repository to store school documents.

If you don't have it installed already you'll want to install subversion.
$sudo apt-get install subversion

After that installs, we'll want to get started on creating the server side of the repository. I'm going to call mine School_Notes. I like to keep my repositories in the user folder. So lets get there first.
$cd ~/

If you intend to have more than one different repository, it's useful to have them organized in a folder. Lets make one:
$mkdir repositories && cd repositories

We're now ready to create the repository.
$svnadmin create School_Notes/

That wasn't too bad, 4 commands to get started, now lets get some documents there.

Lets navigate to somewhere to keep our local copies somewhere else.
mkdir ~/svn/ && cd ~/svn

Now we're in the folder where we will keep our local files, we need to check out a copy from the server (another folder on your computer at this point) to get access to versioning.
$svn co file:///home/USER_NAME/repositories/School_Notes/ School_Notes/

This will check out Revision 0 of school notes into your SVN file. 6 commands and we're almost done.

Let's say we've added the file Notes.txt, and the folder New_Folder. We now need to add these to the items to be checked in. This only needs to be done when you first create a file.
$svn add *

* tells SVN to add everything in this tier of files to the repository. Alternatively you could type
$svn add Notes.txt
and
$svn add New_Folder

SVN will prompt you saying:
A      Notes.txt
A      New_Folder

At this point these changes haven't been saved (called committing) to the master copy yet. We can do this by typing
$svn ci

ci is short for Commit. A terminal based text editor is now brought up. It will show the files you are updating, as well as a place where you can make comments. Press CTRL-x when you are finished, it will ask where to save, just press enter, this is a temporary file anyway. It will now upload these files to the server (the other folder on your computer)

And you're done! Now each time you make a change to the file Notes.txt, or any other files you have added, you can commit those changes, and then go back to that revision at any time.

You can just re-check out the repository to a different folder with a -r REV_# flag, and it will create the folder as it looked at that revision.

Now, next time you accidentally delete a file, or mess up a program so bad that you need to get it back, you can do it in no time.



No comments:

Post a Comment