49

I know you can track a svn repo with git by using git svn init, however that is for if you want to create a brand new repo.

My situation is that I currently already have an existing git repo and want to track the trunk of a svn repo by making it a remote branch in my current git repo.

Any suggestions?

cfi
  • 10,915
  • 8
  • 57
  • 103
rip747
  • 9,375
  • 8
  • 36
  • 47
  • I guess this answer is also valid here: http://stackoverflow.com/a/1430782/1046584 – Luís Bianchin Feb 10 '16 at 15:48
  • Please consider changing the accepted answer to this question, latest `git` versions no longer allow the version that worked for you initially. [This answer](https://stackoverflow.com/a/38706530/313192) has a current working solution. – Caleb Aug 07 '18 at 07:12

4 Answers4

21

After searching last night, I have finally found the answer:

http://i-nz.net/2009/01/15/selective-import-of-svn-branches-into-a-gitgit-svn-repository/

It seems that you have to actually go in and manually edit the .git/config file in order to add an svn branch to an existing git repo. So according to these instructions I would have to add an entry for each branch.

rip747
  • 9,375
  • 8
  • 36
  • 47
  • 1
    I found this one helpful: * http://blog.tfd.co.uk/2008/11/21/adding-a-branch-to-git/ – Jody Garnett May 07 '11 at 04:32
  • 1
    It could have stopped working from Git v1.8.3.2, see http://stackoverflow.com/questions/19712735/git-svn-cannot-setup-tracking-information-starting-point-is-not-a-branch – Jaime Hablutzel Mar 28 '14 at 04:30
  • 2
    it stopped working as of git v1.8.3.2, see answer http://stackoverflow.com/a/38706530/3303668 for an updated solution – Elmer Aug 01 '16 at 19:54
10

The answers for this question (https://stackoverflow.com/a/840411 and https://stackoverflow.com/a/7592152) no longer work as of Git v1.8.3.2 (https://stackoverflow.com/a/19833311). You can do this instead:

1) Define the new branch in .git/config :

[svn-remote "release-branch"]
   url = svn+ssh://xxxx@mono-cvs.ximian.com/source/branches/mono-2-2/mcs
   fetch = :refs/remotes/git-svn-release-branch

2) Import the SVN branch. SVN_BRANCHED_REVISION is the the revision when the branch happened in SVN.

$ git svn fetch release-branch -r SVN_BRANCHED_REVISION

3) Create branch and hook up a local Git branch to the remote branch:

$ git checkout -b release refs/remotes/git-svn-release-branch

5) Update

$ git svn rebase
Community
  • 1
  • 1
Elmer
  • 809
  • 9
  • 15
  • 3
    This should probably be marked as the correct answer. The current checked answer does not work on current git (2.18.0), this does. The other answers have minor issues that stop it all from coming together. This does what the question asked about. – Caleb Aug 07 '18 at 07:11
  • From what I can gather, this "refs/remotes" notation is specific to how git names remote repos/branches? So if I wrote in the config `:refs/remotes/my_git_branch_name`, this is how git would track the branch? Is that correct? –  Jul 19 '21 at 16:56
3

You can find the SVN_BRANCHED_REVISION by doing:

$ svn log --stop-on-copy PATH_TO_BRANCH
Ian Mariano
  • 509
  • 2
  • 8
2

This is actually what git svn init does -- the other git svn commands simply merge things together, etc. You can git svn init and/or copy the layout of an SVN repo cloned with git svn clone, and you should just be able to pull into a local branch, or fetch, and so on. Have some time with the man page for git svn and you shouldn't have too much trouble piecing something together; if you do, #git on freenode is a good resource. So, this should be possible without too much trouble, but I don't know exactly how to do it all.

jeffcook2150
  • 4,028
  • 4
  • 39
  • 51