Version Control System Using Sub Version
When I thought of a version control system for my files I thought surely this will be very complicated/time consuming to implement, far too too expensive, or overkill for what I need. It's not. Want the peace of mind that all the major development companies have with the use of a version control system?
Please read on...
Set Up
The following is aimed primarily towards Anchor customers using our shared hosting services, however all the methods discussed are very much standard procedures.
All Anchor shared hosting accounts come by default with their own version control system available which is subversion.
Getting Started
Create A Repository
Login to your user account on our server via SSH and you can see that your version control software is already available, which is subversion:
useraccount@anchorserver ]$ which svn useraccount@anchorserver ]$ /usr/bin/svn
Now in order to get things started we need to create a separate directory to import the file system which subversion needs to manage your files. This directory is known as the repository.
You use the version control system to import what folder(s)/files you wish to have under version control into a newly created repository.
That's right! A totally different folder (repository) is created to manage your files - the version control system doesn't link directly to the files that you wish implement version control for, but the newly created repository.
To import your files into a new repository please log onto the server and run the following commands:
useraccount@anchorserver ]$ mkdir /PATH/TO/YOUR_NEW/FOLDER_NAME_FOR_REPOSITORY useraccount@anchorserver ]$ svnadmin create /PATH/TO/YOUR_NEW/FOLDER_NAME_FOR_REPOSITORY
Ok now you've created your repository, the version control system on the server is in place!
Import Your Files
The repository is in place, we now need to import the folder(s) you'd like implement version control into the repository:
useraccount@anchorserver ]$ svn import /PATH/TO/YOUR_FOLDER/ file:///PATH/TO/FOLDER_NAME_FOR_REPOSITORY/YOUR_FOLDER/ -m 'comment here'
Congratulations! You have successfully imported your files into your newly created repository, version control has been implemented on your files.
Install Client Software
The next step is to install client version control software on your local computer so you can interact with the repository on the server.
Red Hat Linux users:
# yum install -y subversion
Debian and Ubuntu Linux users:
# apt-get install subversion
OpenBSD users:
# pkg_add subversion
Download Working Copy
Next step we need to download a 'working copy' of the files from your newly created repository on the server to your local system.
We download the files by a term known as 'checking out' which downloads the current revision (version) from the repository. You see when you created the repository and then imported your files the software assigns a revision number of 0 (first version), and every time someone makes changes to any files within the repository the revision number is incremented e.g(1,2,3,4). So for example, if the current revision number is 234 there have been a total of 234 revision changes to your version control file system - it doesn't matter if you changed 1 file or 200 files, any changes no matter how small or large will be given a separate revision number.
To check out (download) a working copy create a new directory on your local system and run:
user@yourcomputer:$ cd /NEWLY_CREATED_FOLDER/ user@yourcomputer:$ svn co svn+ssh://SERVERNAME/PATH/TO/REPOSITORY/YOUR_FOLDER
So we have all the elements in place. We've, created a repository on the server, installed and configured the client software and now you have downloaded the latest revision (version) to your local computer by checking out the repository into your working copy (local folder).
You're free now to edit your working-copy files however you normally would using your favourite text editor or whatever application you would normally use. Every time you create, modify or delete a file within your local working copy directory the changes are tracked.
The version control system is now in place! For every new user they simply need to run through the check out progress, downloading a local working copy of the files from the repository.
Now to start actively using the version control system read on...
Editing Your Working Copy
First lets start by making some changes within your local working copy. Go ahead and open a file using your favourite text editor application and change some text within it.
Status
As all the changes are monitored within your working copy folder you simply need run 'svn status' to give yourself an update:
user@localcomputer:~/svn/workingcopy/$ svn status M yourfile.txt
The above outputs the file you edited with the letter 'M' to state that is has been modified. Your client software uses a folder called ".svn" to track what changes you have made locally within your working copy without having to interact with the server repository. This gives you a chance to preview what changes you have made to files before committing (uploading) them to the repository.
Diff
You can compare the difference between the new and old version of the file you just edited by running:
user@localcomputer:~/svn/workingcopy/$ svn diff yourfile.txt - minus displays previous version + plus will show updated changes
Revert
Now because changes are being logged locally within your working copy, via the .svn folder, before you commit (upload) any changes to the repository you have can easily 'revert' to the previous version of the file if you've made a mistake:
user@localcomputer:~/svn/workingcopy/$ svn revert yourfile.txt Reverted 'yourfile.txt'
If you did revert your changes to the file that you previously edited please edit the file again so we can then go onto the the next segment below 'committing changes'.
Commit Changes
So you have made a change to a file in your working copy and would like to update the changes to your server repository. The process of uploading file is called a commit. You make changes locally to your working copy and then commit them to the repository which in turn creates a new unique revision number implementing version control on your files.
You commit files by using 'ci' (short for "check-in") or 'commit', with the '-m' to add a comment about the changes made:
user@localcomputer:~/svn/workingcopy/$ svn ci -m 'A comment about the changes I have made' Sending yourfile.txt Transmitting file data . Committed revision 1
When you commit your changes your comment will be appended to every file that has changed within your local working copy. If you want to make specific comments for each individual file you would simply run 'svn ci -m 'comment on file' file.txt. Each time you run this a new revision is made on the server.
There you have it, you've successfully made changes to a file within your working copy (local folder) and committed the changes to the repository which in turn created a new revision (version 1).
Okay but what if you've made a mistake and you need to go back to the previous revision you just checked in? Read on...
Going Back A Revision
You can easily update your working copy to any previous revision at any time.
INFO
To view what revision you are working on run:
user@localcomputer:~/svn/workingcopy/$ svn info
To view the latest revision committed to the repository on the server run:
user@localcomputer:~/svn/workingcopy/$ svn info svn+ssh://SERVERNAME/PATH/TO/REPOSITORY/YOUR_FOLDER
The above two examples should give you the revision numbers '0' and '1', whereby '0' in this case is the previous revision and '1' being the current revision you are working on.
To update your working copy to the previous revision run:
user@localcomputer:~/svn/workingcopy/$ svn update -r 0 U path/to/file/yourfile.txt Updated to revision 0.
Update Your Working Copy
Given we have version control in place you may now have multiple users who have checked out (downloaded) working copies to their local computers and you are all working on the same file system. Therefore users will be committing to the repository creating new revisions whilst you are working on your own local working copy.
If you want to the get the latest revision from the repository simply run:
user@localcomputer:~/svn/workingcopy/$ svn update
This will update your local working copy to the latest revision within the repository.
Roll Out Changes
So we have a repository in place to provide version control on your files and with each new user downloading a working copy to their local system, we can now have multiple people working on the same project with version control in place. But how come when I commit my changes to the repository the original files which I imported into the repository are not updated? Ok now listen up as this is where I got caught out... please read on.
The version control system is doing it's job, you have version control and can locally revert changes or update changes to particular revision in the repository. Overwriting the files which you originally imported into the repository is a separate process, yes a separate progress, but don't worry Anchor Systems have it covered.
Introducing the Makefile
Download the following files and save them onto your local system within your local working copy:
Simply edit the Makefile and change the following variables to your Anchor user account settings:
ROUSER := USERNAME ROHOST := ANCHOR_SERVER_NAME RODIR := /home/USERNAME/IMPORTED_DIRECTORY
Please add them into repository by:
user@localcomputer:~/svn/workingcopy/$ svn add Makefile NOROLLOUT
And then commit your changes:
user@localcomputer:~/svn/workingcopy/$ svn ci -m 'Added Makefile and NOROLLOUT files'
Lets Roll
Ok lets roll! By utilising a system utility called Make we can copy all our local working copy files to our original imported directory on the server by simply running the following command:
user@localcomputer:~/svn/workingcopy/$ make out
By running 'make out' it will invoke the Makefile script. This script will make sure your have committed all changes to the repository before copying your files to your destination folder on the server - we use the rsync program so only files that have changes will be copied across.
And there you have it if you now check the destination folder on your server the files will have been updated to your latest working copy. If you need to go back a revision simply run 'svn update -r NUMBER' from your local working copy and then 'make out' to copy that particular revision across.
You know not only have a version control system in place whereby you can revert changes locally to files or update to specific revisions from the repository but can roll out changes by copying your local working copy to your original import folder.
Idiot's Guide to Sub Version
There is a lot to get your head around so if you simply want to a mind numbing command by command/step by step guide to getting version control system in place please click through here, an idiots guide to version control