17
votes

I'm quite new to git and I'm trying to move a svn repository to git. I followed the guide below so now I have a git repo on my server
http://pauldowman.com/2008/07/26/how-to-convert-from-subversion-to-git/

So, if I do "git branch" git replies "* master" and if I do "git branch -r" i get a list of all the branches in the svn repository.

My last svn-checkins have been in one of the branches, but when I did the "git svn clone"-stuff my commits in that branch (they have not been merged into the trunk yet) are visible in my (git) master branch. What am I missing here?

Also, if I on my development machine do "git clone " I get the files alright. But if I do "git branch -r" I can only see the master branch and not the remote branches".

Since we're getting rid of the svn-repo all together I would like to have all the svn branches in the git-repo so that they can be accessed from the developer clients.

Again, I'm not a total git newbie but not far from it. So if there is something fundamental I'm missing here please tell me.

Update
After doing some RTFM (man git-svn) I solved the first problem with branch stuff present in the master branch

reset --hard remotes/trunk

Now the trunk and the master branch are the same. Now, next is to figure out how to get the branches from the development clients.

Update 2
I got it working by combining the url above and the url that Scott pointed to. So, from the beginning.

I first created an empty repository on the server, they are kept in /usr/local/git-repos on our server:

server> cd /usr/local/git-repos
server> mkdir my_project.git
server> cd my_project.git
server> git init

Then i cloned the svn-repository to my dev-machine (note, that on our svn server the "branches" dir is called "branch" witout the 's'):

dev> git svn clone http://<svn.server>/my_project --no-metadata -A authors.txt -t tags -b branch -T trunk my_project

Then some clean-up to get the tags and branches in order:

dev> cp -Rf .git/refs/remotes/tags/* .git/refs/tags/
dev> rm -Rf .git/refs/remotes/tags
dev> cp -Rf .git/refs/remotes/* .git/refs/heads/
dev> rm -Rf .git/refs/remotes

Now add my server as a remote repository:

dev> git remote add origin jorgen@<our_server>:/usr/local/git-repos/my_project.git

Finally, push all branches and tags up to the server:

dev> git push origin --all

Phew, now there you have it, now I can get rid of that svn-repo.

Update 3
Checkout ebneters post below for an easier way of doing it...

4

4 Answers

16
votes

There is a fairly detailed explanation on how to do a pretty good SVN import that explains how to convert the branches properly here:

https://git-scm.com/book/en/v1/Git-and-Other-Systems-Migrating-to-Git

The short answer is to run this:

$ cp -Rf .git/refs/remotes/* .git/refs/heads/
$ rm -Rf .git/refs/remotes

Hope that's helpful.

8
votes

Another good way to do this, just for the record, is to use svn2git — I'm in the middle of converting several rather large repositories and it has been a godsend. It automates all of the steps needed to take care of branches and convert svn tags to real git tags.

5
votes

Scott's solution didn't work for me. I suspect something may have changed in a recent version of git-svn since he posted that (and since the linked book was written), as it seems to aggressively garbage-collect as soon as the clone is complete. But this is just a guess as to why it didn't work. I'm using git 1.6.5.6.

Specifically, my .git/refs/remotes directory was completely empty except for a tags directory, which was also empty. So there is nothing I can copy to make it right.

After some poking around, I was able to fix this by checking the file .git/packed-refs and doing search-and-replace on the following (in this order):

refs/remotes/tags => refs/tags
refs/remotes => refs/heads

If your editor is vim, you can do it with these two commands:

:%s/refs\/remotes\/tags/refs\/tags/g
:%s/refs\/remotes/refs\/heads/g

svn2git 1.3.1 also did not produce a usable result for me (did not import any commits after a certain point several months ago, and branches all showed the same commits). For now I have given up on svn2git and have had the most success using git-svn combined with the above.

Wishful thinking: sure would be nice if git-svn simply added a command like 'abandon' or 'migrate' that would automate this process in a future-proof way.

1
votes

I have migrated 2 svn repos to git (git version 1.7.0.4) following Scott's recipe, a smaller one and a larger one. The smaller one behaved as described by Scott in the book chapter. The larger one required David's solution. Another thing is that

$ git push origin --all

did not push any tags and instead I had to do this:

$ git push origin --all
$ git push origin --tags

This may not be obvious from the flag --all and from the book chapter and I realized this after I have deleted the local git svn repo.