How do I tell git-svn about a remote branch create

2019-01-03 00:37发布

I'm using git-svn to work against my company's central svn repository. We've recently created a new feature branch in the central repo. How do I tell git about it? When I run git branch -r I can only see the branches that existed when I ran fetch against the svn repo to initialize my git repo?

标签: git git-svn
9条回答
淡お忘
2楼-- · 2019-01-03 00:53

If you want to track ALL the remote svn branches, then the solution is as simple as:

git svn fetch

This will fetch ALL the remote branches that have not been fetched yet.

Extra tip: if you checked out only the trunk at first, and later you want to track ALL branches, then edit .git/config to look like this and re-run git svn fetch:

[svn-remote "svn"]
        url = https://svn/path_to_repo_root/
        fetch = path_to_trunk:refs/remotes/git-svn
        branches = path_to_branches/*:refs/remotes/*

The key points are url should point to the repository root, and the paths defined in fetch and branches should be relative to url.

If you want to fetch only specific branches instead of ALL, there is a nice example in git svn --help:

[svn-remote "huge-project"]
        url = http://server.org/svn
        fetch = trunk/src:refs/remotes/trunk
        branches = branches/{red,green}/src:refs/remotes/branches/*
        tags = tags/{1.0,2.0}/src:refs/remotes/tags/*

With older versions of git-svn, once you specified branches like this, you might not be able to get new branches with git svn fetch. One workaround is adding more fetch lines, like this:

[svn-remote "huge-project"]
        url = http://server.org/svn
        fetch = trunk/src:refs/remotes/trunk
        fetch = branches/blue:refs/remotes/branches/blue
        fetch = branches/yellow:refs/remotes/branches/yellow
        branches = branches/{red,green}/src:refs/remotes/branches/*

Another workaround by @AndyEstes: edit .git/svn/.metadata and change the value of branches-maxRev or tags-maxRev to a revision before any newly-specified branches or tags were created. Once you've done this, run git svn fetch to track the new svn remote branch.

查看更多
神经病院院长
3楼-- · 2019-01-03 00:55

Maybe I messed it up somehow but I followed the instructions in vjangus' answer and it almost worked. The only problem was that newbranch didn't appear to be branched from the trunk. In gitk, it was kind of "floating" all on its own; it had no common ancestor with the trunk.

The solution to this was:

  1. Find the SHA1 of the last commit that happened on trunk before the branch was created.
  2. Find the SHA1 of the first commit on the new branch (message is probably "Created new branch, copied from trunk@12345" or something)
  3. git diff-tree <sha1 from step 1> <sha1 from step 2> -- there should be no output. If there is output, you may have selected the wrong commits.
  4. git checkout local-newbranch then git rebase <sha1 from step 1>. This will rebase local-newbranch onto the new tree but remotes/newbranch will still be disconnected.
  5. Go to the file .git/refs/remotes/newbranch and edit it to contain the full SHA1 of the new commit (on the rebased newbranch) that corresponds to the old commit it's currently pointing at. (Or maybe use git-update-ref refs/remotes/newbranch <new-SHA>. Thank you inger.)
  6. The next time you git svn dcommit to newbranch, you'll get a bunch of messages about it updating some log. This is normal I think.

I recommend keeping gitk --all open the whole time and refreshing it often to keep track of what you're doing. I'm still sort of new to git and git svn so please suggest improvements to this method.

查看更多
Juvenile、少年°
4楼-- · 2019-01-03 00:55

If you don't check out with a valid layout, you won't be able to checkout a remote branch.

This is what I do:

git svn init -s <svn path with no trunk> local_repo
cd local_repo
git svn fetch 
## wait

After that, you can switch to a remote branch:

git checkout --track -b branch_name branch_name

Then you will automatically be switched to your branch.

查看更多
Evening l夕情丶
5楼-- · 2019-01-03 00:58

I have not found any documentation about this feature, but looks like git svn configuration supports multiple fetch entries. This way you can also add branches separately without need to add another remote svn repository entry to your config nor using wildcards to get all branches of certain directory.

Assume that your SVN tree is really nasty having lots of branches without any logic how they are located, e.g. having branches and sub-directories containing more branched.

i.e.

trunk
branches
  -> branch1
  -> sub-dir1
    -> branch2
    -> branch3
  -> sub-dir2
    -> branch4
    -> sub-dir3
      -> branchX 
<... hundreds more ...>

and you just want to hand pick some of the branches to be included to your git repository.

You may first init your repository with only trunk without any additional branches:

git svn clone -r 10000:HEAD https://svn.com/MyRepo myrepo --prefix=svn/ --trunk=trunk 

After that you should see following configuration:

localhost: elhigu$ git config --get-regexp "svn-remote."
svn-remote.svn.url https://svn.com/MyRepo
svn-remote.svn.fetch trunk:refs/remotes/svn/trunk

when ever you want to fetch new branch from MyRepo you can just add new fetch entries to configuration by:

git config --add svn-remote.svn.fetch branches/sub-dir2/branch4:refs/remotes/svn/branches/sub-dir2/branch4

Or you may edit the same configuration in .git/config

To fetch the new branches after adding them to config just run:

git svn fetch -r 10000:HEAD

[Edit] Sometimes it seems to be necessary to run fetch with --all parameter to fetch newly added branches:

git svn fetch --all -r 10000:HEAD
查看更多
可以哭但决不认输i
6楼-- · 2019-01-03 01:02

It appears I just needed to git svn fetch; somehow I had convinced myself that would fetch the entire repo instead of just the changes.

查看更多
beautiful°
7楼-- · 2019-01-03 01:02

To add to vjangus' answer, which helped me, I also found it useful to add use git grafts to tie the branches to the trunk at the appropriate point - allowing git to see the history and perform merges correctly.

This is simply a case of adding a line to .git/info/grafts with the hashes:

<initial branch commit> <parent commit in trunk>

eg.

378b0ae0902f5c2d2ba230c429a47698810532e5 6c7144991381ce347d4e563e9912465700be0638

Credit to http://evan-tech.livejournal.com/255341.html

(I'd add this as a comment, but I've not enough reputation.)

查看更多
登录 后发表回答