git - 如何在没有共同历史记录的情况下重新集成 svn 和 git 存储库?

标签 git git-svn

我有一个基于 github 的 git 存储库,它代表了到一定程度的开发,然后是一个 svn 存储库,未使用 git svn 初始化,有进一步的开发。我想将 svn 更改引入 git 存储库,开始使用 git 存储库进行开发,并使用 git svn dcommit 推送更改。这可能吗?值得推荐吗?

这是我的具体情况:

我们在这里开始开发 WordPress 插件:

http://github.com/mrdoornbos/wpconfidentcaptcha

Master位于ef82b94a1232b44aae3e,github中没有进行进一步的更改。

当我们向 wp-plugins.org 的申请被接受时,为我们创建了一个空的 svn 存储库:

http://svn.wp-plugins.org/wp-confident-captcha/trunk@278927

经过一些修改的文件随后被复制到 (r256425) 中。进行了进一步的更改,最后一个更改为 r278935。

我想要的是将 SVN 更改以及 git svn 元数据应用于 master。

这是我目前所掌握的内容(大约需要 4 分钟):

git clone git://github.com/mrdoornbos/wpconfidentcaptcha.git github_cc
cd github_cc
git svn init --stdlayout --prefix="svn/" http://svn.wp-plugins.org/wp-confident-captcha
git svn fetch --revision 256362:278935

这会将我的 github 树放在 origin/master 中,将我的 svn 树放在 svn/trunk 中(以及所有标签在它们自己的/svn 分支中)。 origin/master 和 svn/trunk 之间没有共同的祖先。我不知道从这里去哪里,或者是否有办法将更改从 svn/trunk 获取到 origin/master,以便两个存储库的头具有相同的文件,并让 git svn dcommit 从 origin 工作/大师。

从新的 github 存储库开始似乎是最直接的方法,而且我不会因为失去早期历史而感到悲伤。但是,似乎应该有一种方法可以使其与现有的 github 存储库一起工作。

(编辑:看起来这已经被问为 How to merge two branches without a common ancestor? ,但没有 git filter-branch 示例需要使其工作。与那个问题不同,这些是公共(public) svn 和 git 存储库,因此带有工作脚本的答案是可能的。)

最佳答案

这对我有用:

  1. 将 git 和 svn 历史记录导入到一个存储库中,
  2. 使用移植和过滤分支将 svn 树附加到 git 头,并且
  3. 重置 git-svn 元数据以使用新历史记录。

导入历史记录

这部分已经在问题中描述过:

$ git clone git://github.com/mrdoornbos/wpconfidentcaptcha.git github_cc
$ cd github_cc
$ git svn init --stdlayout --prefix="svn/" http://svn.wp-plugins.org/wp-confident-captcha
$ git svn fetch --revision 256362:278935 # Takes about 4 minutes

现在历史记录如下所示(括号中的友好提交名称):

$ git log --oneline --graph svn/trunk
* d9c713a (svn-z) Bump stable to 1.5.4
* 3febe34 (svn-y) Set display style to modal
... (other commits in svn tree)
* 2687d6a (svn-b) initial checkin
* 5c48853 (svn-a) adding wp-confident-captcha by mrdoornbos

$ git log --oneline --graph master
* ef82b94 (git-z) putting js file back
... (other commits in git tree)
* 8806456 (git-a) initial import

存储库中基本上有两个独立的历史,需要一些体操才能加入它们。

嫁接、 merge 和过滤以重写历史

在第 2 部分中,我使用移植使最后一个 git 提交成为第一个 git 的父级 svn 提交:

$ GRAFT_PARENT_GIT=`git log --pretty=format:'%H' -1 master`
$ GRAFT_FIRST_SVN=`git log --pretty=format:'%H' svn/trunk | tail -n1`
$ echo $GRAFT_FIRST_SVN $GRAFT_PARENT_GIT > .git/info/grafts
$ cat .git/info/grafts
5c48853d69cac0a4471fe96debb6ab2e2f9fb604 ef82b94a1232b44aae3ee5a998c2fa33acb6dcb0

现在 merge 非常顺利:

$ git merge svn/trunk
Updating ef82b94..d9c713a
Fast-forward
 .gitignore                                   |    3 -
(rest of merge lines removed)

$ git log --oneline --graph master
* d9c713a (svn-z) Bump stable to 1.5.4
* 3febe34 (svn-y) Set display style to modal
... (other commits in svn tree)    
* 2687d6a (svn-b) initial checkin
* 5c48853 (svn-a) adding wp-confident-captcha by mrdoornbos
* ef82b94 (git-z) putting js file back

$ git svn info
Path: .
URL: http://svn.wp-plugins.org/wp-confident-captcha/trunk
Repository Root: http://svn.wp-plugins.org
Repository UUID: b8457f37-d9ea-0310-8a92-e5e31aec5664
Revision: 278935
Node Kind: directory
Schedule: normal
Last Changed Author: Confident Technologies
Last Changed Rev: 278935
Last Changed Date: 2010-08-21 00:04:49 -0500 (Sat, 21 Aug 2010)

这可行,但移植不会被推送到仓库。如果我坚持移植策略,那么其他所有想要使用 svn 存储库的人都必须自己重新创建移植。这很容易编写脚本,但在这种情况下我可以使用 git filter-branch 做得更好。该命令用于重写 git 历史记录,并且有一些非常强大的选项。然而,默认命令正是我想要的:重新计算提交哈希值,考虑到移植物添加的任何“假” parent :

$ git filter-branch master
Rewrite d9c713a99684e07c362b213f4eea78ab1151e0a4 (71/71)
Ref 'refs/heads/master' was rewritten

$ git log --oneline --graph master
* 51909da (svn-z') Bump stable to 1.5.4
* 7669355 (svn-y') Set display style to modal
... (other re-hashed commits in svn tree)  
* aed5656 (svn-b') initial checkin
* 0a079cf (svn-a') adding wp-confident-captcha by mrdoornbos
* ef82b94 (git-z) putting js file back

现在 git 历史记录看起来像是一个正确的更改序列,其他人将看到相同的序列,而不会弄乱移植。

重新创建 git-svn 元数据

Git 很高兴,但 git-svn 却不高兴:

$ git svn info
Unable to determine upstream SVN information from working tree history

$ git log --oneline --graph svn/trunk
* d9c713a (svn-z) Bump stable to 1.5.4
* 3febe34 (svn-y) Set display style to modal

git-svn 保留它自己的关于提交的元数据(在 .git/svn/* 中),并查看 refspec refs/remotes/svn/trunk 分支(在 git svn init 期间在配置中设置)来确定svn head commit 是。我需要将 svn trunk 指向新提交,然后重新创建元数据。这是我不能 100% 确定的部分,但它对我有用:

$ GIT_NEW_SVN_TRUNK=`git log --pretty=format:'%H' -1 master`
$ echo $GIT_NEW_SVN_TRUNK
51909da6a235b3851d5f76a44ba0e2d128ded465
$ git update-ref --no-deref refs/remotes/svn/trunk $GIT_NEW_SVN_TRUNK
$ rm -rf .git/svn  # Clear the metadata cache
$ git svn info     # Force a rebuild of the metadata cache
Migrating from a git-svn v1 layout...
Data from a previous version of git-svn exists, but
  .git/svn
  (required for this version (1.7.3.1) of git-svn) does not exist.
Done migrating from a git-svn v1 layout
Rebuilding .git/svn/refs/remotes/svn/trunk/.rev_map.b8457f37-d9ea-0310-8a92-e5e31aec5664 ...
r256362 = 0a079cfe51e4641da31342afb88f8b47a0b3f2f3
r256425 = aed565642990be56edc5d1d6be7fa9075bab880d
(...more lines omitted)
r278933 = 766935586d22770c3ef536442bb9e57ca3708118
r278935 = 51909da6a235b3851d5f76a44ba0e2d128ded465
Done rebuilding .git/svn/refs/remotes/svn/trunk/.rev_map.b8457f37-d9ea-0310-8a92-e5e31aec5664
Path: .
URL: http://svn.wp-plugins.org/wp-confident-captcha/trunk
(...and the rest of the git svn info output from above)

在克隆上重新创建 git-svn 元数据

如果有人从我的 git 存储库进行克隆,他们会以提交消息的形式获得大部分 git-svn 元数据,但不足以自己使用 git-svn。大多数人不需要,但有一天我需要设置一台新计算机或培训我的替代者。这对我有用:

$ cd ..
$ git clone github_cc github_cc2
$ cd github_cc2
$ git svn init --stdlayout --prefix="svn/" http://svn.wp-plugins.org/wp-confident-captcha
$ git update-ref --no-deref refs/remotes/svn/trunk 51909da6a235b3851d5f76a44ba0e2d128ded465
$ git svn info
Rebuilding .git/svn/refs/remotes/svn/trunk/.rev_map.b8457f37-d9ea-0310-8a92-e5e31aec5664 ...
r256362 = 0a079cfe51e4641da31342afb88f8b47a0b3f2f3
r256425 = aed565642990be56edc5d1d6be7fa9075bab880d
(...more lines omitted)
r278933 = 766935586d22770c3ef536442bb9e57ca3708118
r278935 = 51909da6a235b3851d5f76a44ba0e2d128ded465
Done rebuilding .git/svn/refs/remotes/svn/trunk/.rev_map.b8457f37-d9ea-0310-8a92-e5e31aec5664
Path: .
URL: http://svn.wp-plugins.org/wp-confident-captcha/trunk
(...and the rest of the git svn info output from above)

现在 svn trunk 已准备就绪。为了获取标签,我必须重新获取:

$ git svn fetch -r256362:278935
(Lots of output, seemed to be about 4 minutes again
$ git svn rebase # Fetch the rest of svn history and update metadata

我不确定在树中有更多历史记录后这个确切的顺序是否有效。

我在 git svn rebase 期间收到一些消息:

W: Refspec glob conflict (ref: refs/remotes/svn/trunk):
expected path: wp-confident-captcha/branches/trunk
    real path: wp-confident-captcha/trunk
Continuing ahead with wp-confident-captcha/trunk

我通过在 .git/config 中手动设置 svn 配置来修复这些问题:

[svn-remote "svn"]
  url = http://svn.wp-plugins.org
  fetch = wp-confident-captcha/trunk:refs/remotes/svn/trunk
  branches = wp-confident-captcha/branches/*:refs/remotes/svn/branches/*
  tags = wp-confident-captcha/tags/*:refs/remotes/svn/tags/*

摘要

要使 git svn rebasegit svn dcommit 正常工作,需要做很多工作。我学到了很多关于 git 和 git svn 的知识,但我不相信最终目标值得。对于此用例(偶尔将 svn 存储库更新到 git 存储库的 HEAD),某些自定义脚本可能更有效。

关于git - 如何在没有共同历史记录的情况下重新集成 svn 和 git 存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3864934/

相关文章:

git - 使用 git-svn : Pull, merge 还是 rebase ?

git - 如何将具有历史记录的 SVN 存储库的一部分迁移到新的 Git 存储库?

git - 如何在 git bitbucket 中克隆一个特定的分支

git svn show-ignore 给出错误 "command returned error: 1"

git - 如何将 Google Analytics 添加到 Github 存储库

git - 这个基于审查的 Git 工作流可以由 Gerrit 执行吗?

git - 拒绝 git commit 并显示与上次提交相同的消息?

git-svn密码更改

github 徽章和分支

git - 虚拟环境文件夹似乎由 git 跟踪