簡體   English   中英

我如何告訴 git-svn 獲取 repo 后創建的遠程分支?

[英]How do I tell git-svn about a remote branch created after I fetched the repo?

我正在使用git-svn來處理我公司的中央 Subversion 存儲庫。 我們最近在中央倉庫中創建了一個新的功能分支。

我如何告訴 Git 呢? 當我運行git branch -r我只能看到當我對 Subversion 存儲庫運行fetch以初始化我的 Git 存儲庫時存在的分支?

您可以手動添加遠程分支,

git config --add svn-remote.newbranch.url https://svn/path_to_newbranch/
git config --add svn-remote.newbranch.fetch :refs/remotes/newbranch
git svn fetch newbranch [-r<rev>]
git checkout -b local-newbranch -t newbranch
git svn rebase newbranch

如果要跟蹤所有遠程 svn 分支,那么解決方案很簡單:

git svn fetch

這將獲取所有尚未獲取的遠程分支。

額外提示:如果你一開始只檢出主干,然后你想跟蹤所有分支,那么編輯.git/config看起來像這樣並重新運行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/*

關鍵點是url應該指向存儲庫根目錄, fetchbranches定義的路徑應該是相對於url

如果你只想獲取特定的分支而不是 ALL,在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/*

對於舊版本的git-svn ,一旦您指定了這樣的分支,您可能無法使用git svn fetch獲得新分支。 一種解決方法是添加更多fetch行,如下所示:

[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/*

@AndyEstes 的另一種解決方法:編輯.git/svn/.metadata並在創建任何新指定的分支或標簽之前將branches-maxRevtags-maxRev的值更改為修訂版。 完成此操作后,運行git svn fetch以跟蹤新的 svn 遠程分支。

看來我只需要git svn fetch 不知何故,我說服自己將獲取整個 repo 而不僅僅是更改。

也許我以某種方式把它搞砸了,但我按照 vjangus 的回答中的說明進行了操作,它幾乎奏效了。 唯一的問題是 newbranch 似乎沒有從主干中分支出來。 在 gitk 中,它完全靠自己“浮動”; 它與樹干沒有共同的祖先。

對此的解決方案是:

  1. 查找在創建分支之前在主干上發生的最后一次提交的 SHA1。
  2. 在新分支上找到第一次提交的 SHA1(消息可能是“創建新分支,從主干@12345 復制”或其他內容)
  3. git diff-tree <sha1 from step 1> <sha1 from step 2> - 應該沒有輸出。 如果有輸出,您可能選擇了錯誤的提交。
  4. git checkout local-newbranch然后git rebase <sha1 from step 1> 這會將local-newbranch到新樹上,但remotes/newbranch仍將斷開連接。
  5. 轉到文件.git/refs/remotes/newbranch並編輯它以包含與當前指向的舊提交相對應的提交的完整 SHA1(在重新設置的newbranch )。 (或者也許使用git-update-ref refs/remotes/newbranch <new-SHA> 。謝謝你。)
  6. 下次你git svn dcommitnewbranch ,你會收到一堆關於它更新日志的消息。 我覺得這很正常。

我建議保持gitk --all打開並經常刷新它以跟蹤你在做什么。 我對 git 和 git svn 還是個新手,所以請建議改進​​這個方法。

vjangus 答案的簡化:

如果您在 SVN 中使用標准布局並完成了通常的 svn init,git-svn 將為您完成配置。 只是:

  1. 在 SVN 中查找分支副本修訂版
  2. 使用 git-svn 獲取該修訂版
  3. 創建新的本地分支跟蹤遠程

一個例子。 SVN 網址是svn+ssh://gil@svn.myplace.com/repo 我正在尋找的 SVN 分支是newbranch 本地 git 分支(跟蹤遠程newbranch )將是git-newbranch

第 1 步:找到分支副本修訂版

# svn log --stop-on-copy svn+ssh://gil@svn.myplace.com/repo/branches/newbranch | tail -4
    r7802 | someone | 2014-03-21 18:54:58 +0000 (Fri, 21 Mar 2014) | 1 line

    branching HEAD to newbranch
    ------------------------------------------------------------------------

所以SVN中的分支點是修訂版7802。

第 2 步:獲取修訂版本

# git svn fetch -r 7802
    Found possible branch point: svn+ssh://gil@svn.myplace.com/repo/trunk => svn+ssh://gil@svn.myplace.com/repo/branches/newbranch, 7801
    Found branch parent: (refs/remotes/trunk) 8dcf3c5793ff1a8a79dc94d268c91c2bf388894a
    Following parent with do_switch
    Successfully followed parent
    r7802 = 9bbd4194041675ca5c9c6f3917e05ca5654a8a1e (refs/remotes/newbranch)

git-svn 完成了所有工作,現在知道遠程:

# git show-ref | grep newbranch
    2df23af4733f36f5ad3c14cc1fa582ceeb3edb5c refs/remotes/newbranch

第 3 步:創建新的本地分支來跟蹤遠程分支:

# git checkout -b git-newbranch -t newbranch
    Checking out files: 100% (413/413), done.
    Branch git-newbranch set up to track local ref refs/remotes/newbranch.
    Switched to a new branch 'git-newbranch'

我還沒有找到關於此功能的任何文檔,但看起來 git svn 配置支持多個提取條目。 通過這種方式,您還可以單獨添加分支,而無需將另一個遠程 svn 存儲庫條目添加到您的配置中,也無需使用通配符來獲取某個目錄的所有分支。

假設您的 SVN 樹真的很糟糕,有很多分支,而它們的位置沒有任何邏輯,例如,具有包含更多分支的分支和子目錄。

IE

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

並且您只想手動選擇一些分支以包含到您的 git 存儲庫中。

您可以首先僅使用主干初始化您的存儲庫,而無需任何其他分支:

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

之后,您應該看到以下配置:

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

當您想從 MyRepo 獲取新分支時,您可以通過以下方式向配置添加新的獲取條目:

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

或者你可以在 .git/config 中編輯相同的配置

要在將新分支添加到配置后獲取新分支,只需運行:

git svn fetch -r 10000:HEAD

[編輯]有時似乎有必要使用 --all 參數運行 fetch 以獲取新添加的分支:

git svn fetch --all -r 10000:HEAD

您可以嘗試SubGit ,而不是處理 git-svn 怪癖。

必須將 SubGit 安裝到 Subversion 存儲庫中。 之后就可以使用標准的 git 工作流程而不是使用特殊的 git-svn 命令:

  1. 推送新提交:

    git-svn:

     $ git commit $ git svn rebase $ git svn dcommit

    子Git:

     $ git commit $ git push
  2. 獲取傳入的更改

    git-svn:

     $ git svn rebase

    子Git:

     $ git pull [--rebase]
  3. 創建一個新分支:

    git-svn:

     $ git svn branch foo $ git checkout -b foo -t remotes/foo $ git commit $ git svn dcommit

    子Git:

     $ git checkout -b foo $ git commit $ git push

有關更多詳細信息,請參閱SubGit 文檔

為了添加對我有幫助的 vjangus 的答案,我還發現添加 use gitrafts 以在適當的點將分支連接到主干很有用 - 允許 git 查看歷史記錄並正確執行合並。

這只是在.git/info/grafts中添加一行帶有哈希值的情況:

<initial branch commit> <parent commit in trunk>

例如。

378b0ae0902f5c2d2ba230c429a47698810532e5 6c7144991381ce347d4e563e9912465700be0638

歸功於http://evan-tech.livejournal.com/255341.html

(我將此添加為評論,但我沒有足夠的聲譽。)

如果您不使用有效布局簽出,您將無法簽出遠程分支。

這就是我所做的:

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

之后,您可以切換到遠程分支:

git checkout --track -b branch_name branch_name

然后您將自動切換到您的分支。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM