In the first part of this chapter, we will see the most cautious approach while starting to move away from Subversion, which is to keep the original repository using Git to fetch and push changes. For the purpose of learning, we will create a local Subversion repository, using both Subversion and Git to access its contents.
Without the hassle of remote servers, let's create a local Subversion repository as a container for our experiments:
$ cd Repos
$ svnadmin create MySvnRepo
Now there's nothing more and nothing less to be done here, and the repository is now ready to be filled with folders and files.
At this point, we have a working Subversion repository; we can now check it out in a folder of our choice, which will become our working copy; in my case, I will use the C:Sources
folder:
$ cd Sourcessvn
$ svn checkout file:///Repos/MySvnRepo
You now have a MySvnRepo
folder under the Sources
folder, ready to be filled with your project files; but first let me remind you of a couple of things.
As you may know, a Subversion repository generally has the following subfolder structure:
/trunk
: This is the main folder, where generally you have the code under development/tags
: This is the root folder of the snapshots you usually freeze and leave untouched, for example /tags/v1.0
/branches
: This is the root folder of all the repository branches you will create for feature development, for example /branches/NewDesign
Subversion does not provide a command to initialize a repository with this layout (commonly known as standard layout), so we have to build it up by hand.
At this point, we can import a skeleton folder that already contains the three subfolders (/trunk
, /branches
, and /tags
) with a command like this:
$ cd SourcessvnMySvnRepo $ svn import /path/to/some/skeleton/dir
Otherwise, we can create folders by hand using the svn mkdir
command:
$ cd SourcessvnMySvnRepo $ svn mkdir trunk $ svn mkdir tags $ svn mkdir branches
Commit the folders we just created and the repository is ready:
svn commit -m "Initial layout"
Now add and commit the first file, as shown in the following code:
$ cd trunk $ echo "This is a Subversion repo" > readme.txt $ svn add readme.txt $ svn commit -m "Readme file"
Feel free to add more files or import an existing project if you want to replicate a more real situation; for import files in a Subversion repository, you can use the svn import
command, as you saw before:
$ svn import MyProjectFolder
Later, we will add a tag and a branch to verify how Git interacts with them.
Git provides a set of tools to cooperate with Subversion; the base command is actually git svn
; with git svn
you can clone Subversion repositories, retrieve and upload changes, and much more.
So, wear the Git hat and clone the Subversion repository using the git svn clone
command:
$ cd Sourcesgit $ git svn clone file:///Repos/MySvnRepo
git svn clone
usually runs smoothly on Linux boxes, while in Windows you get a weird error message, as shown here:
Couldn't open a repository: Unable to open an ra_local session to URL
This happens because in Windows there are some problems in the git svn
command backport while using the file://
protocol.
To bypass this problem, we can use the svn://
protocol instead of file://
.
To do this, we will use the svnserve
command to start a Subversion server exposing our repositories directory root. But first we have to edit some files to set up the server.
The main file is the svnserve.conf
file; look at your MySvnRepo
repository folder, C:ReposMySvnRepo
in this case, jump into the conf
subfolder, and edit the file uncommenting these lines (remove the starting #):
anon-access = read auth-access = write password-db = C:ReposMySvnRepopasswd authz-db = C:ReposMySvnRepoauthz realm = MySvnRepo
Normally, full paths are unnecessary for the passwd
and authz
files, but in Windows I find them mandatory, otherwise the Subversion server does not load them.
Now edit the passwd
file, which contains the user's credentials, and add a new user:
[users] adminUser = adminPassword
Then edit the authz
file and set up groups and rights for the user:
[groups] Admins = admin
Continuing with the authz
file, set rights for repositories; the Admin will have read/write access to all repositories (the section [/]
means "all repository"):
[/] @Admins = rw * =
At this point, we can start the server with this command:
$ svnserve -d --config-file C:ReposMySvnRepoconfsvnserve.conf --root C:Repos
-d
starts the server as a daemon; --config-file
specifies the config file to load and --root
tells the server where the main root folder of all your repositories is.
Now we have a Subversion server responding at svn://localhost:3690
. We can finally clone the repository using Git:
$ cd Sourcesgit $ git svn clone svn://localhost/MySvnRepo/trunk
This time things will go smoothly; we are now talking with a Subversion server using Git.
Just to have a more realistic situation, I will add a tag and a branch; in this manner, we will see how to deal with them in Git.
So, add a new file:
$ echo "This is the first file" > svnFile01.txt $ svn add svnFile01.txt $ svn commit -m "Add first file"
Then tag this snapshot of the repository as v1.0
as you know that in Subversion, a tag or a branch is a copy of a snapshot:
$ echo "This is the first file" > svnFile01.txt $ svn add svnFile01.txt $ svn copy svn://localhost/MySvnRepo svn://localhost/MySvnRepo/tags/v1.0 -m "Release 1.0"
Now that we have a running clone of the original Subversion repository, we can use Git as it was a Subversion client. So add a new file and commit it using Git:
$ echo "This file comes from Git" >> gitFile01.txt $ git add gitFile01.txt $ git commit –m "Add a file using Git"
Now we have to push this file to Subversion:
$ git svn dcommit
Well done! We can even use Git to fetch changes with the git svn fetch
command, update the local code using the git svn rebase
command, and so on; for other commands and options, I recommend that you read the main page of git svn --help
.
Using Git as a Subversion client is not the best we can obtain, but at least it is a way to start using Git even if you cannot abandon Subversion instantly.