从 2 个文件夹的差异创建补丁文件
我对开源项目进行了一些更改,但没有花时间创建适当的补丁文件。
现在,该项目的维护者发布了新版本,在新的和编辑的文件中,有一堆重命名的文件。
将我的更改应用到新版本的最佳方法是什么?
我对 diff/patch 使用完全陌生,如果我能用 git 完成它,那就更好了。
I made some changes to an open source project without taking time to create proper patch files.
Now, the maintainer of the project released a new version, and among new and edited files, there are a bunch of renamed files.
What is the best way to apply my changes to the new version ?
I'm completely new to diff/patch use, and If I can get it done with git, it would be better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您有两个相似的目录
a
和b
,并且您希望b
与a
相同,您可以创建并应用补丁:假设您有目录
local
(包含本地版本的upstream1.0)、upstream1.0
和upstream1。 1.
.要创建并将更改应用到upstream1.1
:检查补丁文档,并尝试说服上游维护者使用 git。如果您可以使用 git 工具来处理本地存储库,事情就会简单得多。
If you have two directories
a
andb
that are similar, and you wantb
to be the same asa
, you can create and apply a patch with:Suppose you have directories
local
(containing your local version of upstream1.0),upstream1.0
, andupstream1.1
. To create and apply your changes toupstream1.1
:Check the documentation for patch, and try to convince the upstream maintainers to use git. Things will be much simpler if you can use git tools to work with your local repository.
如果项目位于 git 下并且您尚未在本地提交更改,则只需执行
git diff >; file.patch
获取可修补的差异数据。如果您已在本地提交更改,则可以执行 git log 来查找之前的提交,然后再执行 git diff commit_string > 。文件.补丁。如果项目不在 git 下,或者如果您在没有克隆存储库的情况下获取源代码(如标题所示),则可以使用 diff -urN original_dir new_dir >; file.patch 创建补丁文件。在这两种情况下,您都可以稍后尝试使用 patch 来应用补丁。
但是,请考虑使用 git 工具将更改与新版本合并,因为 git 也能够跟踪文件名更改。您需要大量了解 git 本身,并且需要一些时间才能正确使用 - 您可能应该在开始使用之前备份您的工作。
If the project is under git and you haven't committed your changes locally, you can simply do
git diff > file.patch
to get patchable diff data. If you have committed the changes locally, you can dogit log
to find the commit before you and thangit diff commit_string > file.patch
.If the project isn't under git, or if you d/l source without cloning the repository (as the title suggests), you can use
diff -urN original_dir new_dir > file.patch
to create the patch file. In both cases you can try use patch later to apply the patch.However, consider using git tools to merge your changes with the new version, since git is able to track filename changes too. You'll need to learn great deal about git itself, and it will take you some time to get it right - you should probably backup your work before starting to play with it.
Git 支持检测文件重命名,所以如果你幸运的话它会帮助你。以下是您应该执行的操作的大致草案。
导入原始版本:
现在您可以在单独的分支中应用原始更改:
然后更新到较新的版本:
到目前为止,所有内容都已签入 git 存储库,现在是开始尝试合并更改的时候了:
好运气好...
如果您想手动合并文件,我真的建议使用 KDiff3。假设 file1.c 来自 open-source-project-0.1,file2.c 来自 open-source-project-0.2,file3.c 来自您的更改,运行
Git has support for detecting renaming of files, so if you are lucky it will help you with that. The following is an approximate draft of what you should do.
Import the original version:
Now you can apply your original changes in a separate branch:
Then you update to the newer version:
So far everything is checked in into the git repository, now is the time to start to try to merge your changes:
Good luck...
If you want to merge files manually I really recommend using KDiff3. Assuming file1.c is from open-source-project-0.1, file2.c from open-source-project-0.2 and file3.c from your changes, run
由于声誉较低,我无法发表评论。所以我会添加一个新答案。我尝试了上面的答案之一仍然存在空白行问题。不过下面的方法对我有用。
I couldnt comment due to low reputation. So I will add a new answer. I tried one of the answers above still had issues with blank lines. Below method worked for me though.
您可以尝试之前向我建议的一些方法,一个有趣的“差异”解决方案:首先克隆该项目的最新版本。它不应该有任何本地更改。确保 .git 文件夹存在。然后将您的工作树(即除 .git 文件夹之外的所有文件)复制到克隆的存储库中。现在,如果您输入“git st”,您将看到更改的所有内容。如果 git st 报告的文件实际上没有更改,您还需要整理间距和行结尾(git config core.autocrlf ...)。
现在,对于 git st 列表中的每个文件,如果您输入 git diff 您将看到您的更改。
然后我会一一编辑文件,直到 git st 看起来像我想要提交的内容。
我不会依赖于制作补丁,因为它们非常挑剔。您很可能会收到“无法应用补丁”的消息,而上面的解决方案为您提供了未暂存更改的列表,您可以按照您想要的任何顺序进行操作。
You could try something that was suggested to me here before, an interesting "diffing" solution: first clone the latest version of the project. It shouldn't have any local changes. Make sure the .git folder is there. Then copy your working tree, that is all your files EXCEPT the .git folder, to the cloned repo. Now if you type "git st" you will see everything that was changed. You'll need to sort out also the spacing and line endings (git config core.autocrlf ...) if git st reports files that don't really have changes in them.
Now for each file in git st list, if you type git diff you'll see your changes.
Then I would edit the files one by one, until git st looks like what I want to commit.
I wouldn't rely on making patches because they are extremely picky. You'll most likely get a "can't apply patch", whereas the solution above gives you a list of unstaged changes which you can work on in any order you want.
现在我们想要比较两个文件夹
html1
和html2
目录。在html1
中,CSS 发生了变化。在
html2
中添加了另一个index2.html
。现在,我们想要从html2
到html1
$ git diff html2/ html1/ >补丁/htmlchanges.patch
Now we want to take diff of two folders
html1
andhtml2
directories. Inhtml1
the CSS is changed.In
html2
anotherindex2.html
is added. Now, we want to take diff fromhtml2
tohtml1
$ git diff html2/ html1/ > patches/htmlchanges.patch
您可能应该查看
git rebase
。也许即使是一个简单的 git pull 也能满足您的需求。You should probably be looking at
git rebase
. Perhaps even a simplegit pull
will do what you want.