Git 格式补丁与 svn 兼容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/708202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:18:32  来源:igfitidea点击:

Git format-patch to be svn compatible?

svngitformat-patch

提问by rip747

Is there any way to get a patch created with git format-patch to be svn compatible so that I can submit it to an svn repo?

有什么方法可以让使用 git format-patch 创建的补丁与 svn 兼容,以便我可以将其提交给 svn repo?

I'm working off an svn repo on github and want to submit my changes back to the main repo. I need to create a patch to do this, however the patch cannot be applied since git formats that patch differently then svn. Is there some secret I haven't discovered yet?

我正在 github 上处理一个 svn 存储库,并希望将我的更改提交回主存储库。我需要创建一个补丁来执行此操作,但是无法应用该补丁,因为 git 格式的补丁与 svn 不同。是不是有什么我还没发现的秘密?

UPDATE:Although currently there exists no script or native git way to do this, I did managed to find a post from earlier this year about how to manually accomplish this. I have followed the instructions and had success getting my git patches to work with svn.

更新:虽然目前不存在脚本或本地 git 方法来执行此操作,但我确实设法在今年早些时候找到了一篇关于如何手动完成此操作的帖子。我已经按照说明操作,并成功地让我的 git 补丁与 svn 一起工作。

If someone could take a stab at writing a script to accomplish this and contribute to the git project, I'm everyone would be much appreciated.

如果有人可以尝试编写脚本来完成此任务并为 git 项目做出贡献,那么我将不胜感激。

http://kerneltrap.org/mailarchive/git/2008/1/15/570308/thread#mid-570308

http://kerneltrap.org/mailarchive/git/2008/1/15/570308/thread#mid-570308

采纳答案by Nicholas Smith

I always have to Google this but the way I've found that works perfectly (for me) is:

我总是不得不谷歌这个,但我发现完美的方式(对我来说)是:

  • Create the patch with git diff --no-prefix master..branch > somefile.diff, the master and branch part are optional, depends how you want to get your diffs.
  • Send it wherever and apply with patch -p0 < somefile.diff.
  • 创建补丁git diff --no-prefix master..branch > somefile.diff,主和分支部分是可选的,取决于你想如何获得你的差异。
  • 将其发送到任何地方并使用patch -p0 < somefile.diff.

It always seems to work fine for me and seems to be the simplest method that I've come across.

它对我来说似乎总是很好,而且似乎是我遇到的最简单的方法。

回答by Wallace Wong

The short answer is patch -p1 -i {patch.file}.

简短的回答是patch -p1 -i {patch.file}

Please refer to this blog for details: Creating Subversion patches with git.

有关详细信息,请参阅此博客:使用 git 创建 Subversion 补丁

回答by Christoph

Here's a helper script for making a diff against the the latest svn changeset and the given commit: http://www.mail-archive.com/[email protected]/msg00864.html

这是一个帮助脚本,用于对最新的 svn 变更集和给定的提交进行比较:http: //www.mail-archive.com/[email protected]/msg00864.html

#!/bin/sh
#
# git-svn-diff
# Generate an SVN-compatible diff against the tip of the tracking branch
TRACKING_BRANCH=`git config --get svn-remote.svn.fetch | sed -e 's/.*:refs\/remotes\///'`
REV=`git svn find-rev $(git rev-list --date-order --max-count=1 $TRACKING_BRANCH)`
git diff --no-prefix $(git rev-list --date-order --max-count=1 $TRACKING_BRANCH) $* |
sed -e "s/^+++ .*/&    (working copy)/" -e "s/^--- .*/&    (revision $REV)/" \
-e "s/^diff --git [^[:space:]]*/Index:/" \
-e "s/^index.*/===================================================================/"

回答by Norman Ramsey

SVN probably cannot understand the output of git diff -p, but you can resort to brute force:

SVN 可能无法理解 的输出git diff -p,但您可以诉诸暴力:

  1. Make two clones of your repo
  2. In one clone check out your latest stuff
  3. In the other clone checkout whatever is equivalent to the svn upstream. If you have planned ahead you have a copy of svn upstream on its own branch, or you have tagged the last svn version. If you have not planned ahead, use the date or gitk to find the git SHA1 hash that most closely approximates the svn state.
  4. Now compute a real patch by running diff -rover the two clones.
  1. 制作你的回购的两个克隆
  2. 在一个克隆中查看您的最新内容
  3. 在另一个克隆结帐中,任何等同于 svn 上游的东西。如果你已经提前计划好了,你在它自己的分支上有一个 svn upstream 的副本,或者你已经标记了最后一个 svn 版本。如果您没有提前计划,请使用日期或 gitk 查找最接近 svn 状态的 git SHA1 哈希。
  4. 现在通过运行diff -r两个克隆来计算一个真正的补丁。

回答by Bert Huijben

Subversion < 1.6 doesn't have patch support. It looks like Subversion 1.7 will allow applying patches and the git/hg extensions to unified diff are on our TODO list.

Subversion < 1.6 没有补丁支持。看起来 Subversion 1.7 将允许应用补丁,并且统一差异的 git/hg 扩展在我们的 TODO 列表中。

回答by VonC

It is indeed a feature requestearly 2008

这确实是2008 年初的功能请求

Linus Torvalds said at the time:

Linus Torvalds 当时说:

So I would argue that you need something stronger to say "don't do a git diff", and that should also disallow rename detection at a minimum.
Quite frankly, any program that is so stupid as to not accept current git patches (ie TortoiseSVN), then we damn well shouldn't just disable the most trivial part of it. We should make sure that we do not enable anyof the rather important extensions:
even if TtheitroadtoiseSVN would ignore them, if ignoring them means that it mis-understands the diff, it shouldn't be allowed at all.

所以我认为你需要更强大的东西来说“不要做 git diff”,这也应该至少禁止重命名检测。
坦率地说,任何愚蠢到不接受当前 git 补丁(即 TortoiseSVN)的程序,那么我们该死的不应该只是禁用它最琐碎的部分。我们应该确保我们没有启用任何相当重要的扩展:
即使 TtheitroadtoiseSVN 会忽略它们,如果忽略它们意味着它误解了差异,那么它根本不应该被允许。

That may be why

这可能就是为什么

 git-format-patch: add --no-binary to omit binary changes in the patch.

has been introduced in Git1.5.6 in May/July 2008 (I have not tested it though)

已于 2008 年 5 月/7 月在 Git1.5.6 中引入(不过我还没有测试过)

回答by Ismail Hawayel

Make sure your changes are committed and rebased on top of your local git branch, from git bash run:

确保您的更改已提交并重新基于本地 git 分支,来自 git bash run:

git show --pretty >> myChangesFile.patch

git show --pretty >> myChangesFile.patch

回答by Leandro Gomez

The accepted answer provided by Nicholas works fine, except when a) binary files exist in the diff or b) you are working in windows Git and have directories with spaces. To get that resolved I had to add a nested git diff command to ignore binaries and sed command to escape the spaces. It's a bit cumbersome to write, so I created an alias:

Nicholas 提供的公认答案工作正常,除非 a) 差异中存在二进制文件或 b) 您在 Windows Git 中工作并且目录带有空格。为了解决这个问题,我不得不添加一个嵌套的 git diff 命令来忽略二进制文件和 sed 命令来转义空格。写起来有点麻烦,所以我创建了一个别名:

[alias]
svnpatch = "!f() { git diff --name-only --no-prefix master... | grep -Ev \"\.sdf|\.Doc|\.dll|\.zip|\.exe\" | sed 's_\s_\\\\ _g'  | xargs git diff --no-prefix master... > .patch; echo "Created .patch"; }; f"

If you then type:

如果然后键入:

git svnpatch Feature123

...a patch file Feature123.patch will be created with the differences between the merge base of branch master and branch Feature123.

...一个补丁文件 Feature123.patch 将被创建,其中包含分支 master 和分支 Feature123 的合并基础之间的差异。