从 2 个文件夹的差异创建补丁文件

发布于 2024-08-25 19:42:14 字数 157 浏览 7 评论 0原文

我对开源项目进行了一些更改,但没有花时间创建适当的补丁文件。

现在,该项目的维护者发布了新版本,在新的和编辑的文件中,有一堆重命名的文件。

将我的更改应用到新版本的最佳方法是什么?
我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

七颜 2024-09-01 19:42:14

如果您有两个相似的目录 ab,并且您希望 ba 相同,您可以创建并应用补丁:

$ diff -ur b a > ba.diff
$ patch -i ba.diff

假设您有目录 local(包含本地版本的upstream1.0)、upstream1.0upstream1。 1..要创建并将更改应用到 upstream1.1

$ diff -ur upstream1.0 local > my.diff
$ cd upstream1.1
$ patch -i ../my.diff 

检查补丁文档,并尝试说服上游维护者使用 git。如果您可以使用 git 工具来处理本地存储库,事情就会简单得多。

If you have two directories a and b that are similar, and you want b to be the same as a, you can create and apply a patch with:

$ diff -ur b a > ba.diff
$ patch -i ba.diff

Suppose you have directories local (containing your local version of upstream1.0), upstream1.0, and upstream1.1. To create and apply your changes to upstream1.1:

$ diff -ur upstream1.0 local > my.diff
$ cd upstream1.1
$ patch -i ../my.diff 

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.

灰色世界里的红玫瑰 2024-09-01 19:42:14

如果项目位于 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 do git log to find the commit before you and than git 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.

冰雪之触 2024-09-01 19:42:14

Git 支持检测文件重命名,所以如果你幸运的话它会帮助你。以下是您应该执行的操作的大致草案。

导入原始版本:

tar zxvf open-source-project-0.1.tar.gz 
mv open-source-project-0.1 open-source-project
cd open-source-project
git init
git add .
git commit -m "Initial checkin of open-source-project-0.1"
git tag open-source-project-0.1

现在您可以在单独的分支中应用原始更改:

git checkout -b mychanges
cp /somewhere/where/your/changes/files/are/* .
git diff
git add .
git commit -m "My changes"
git tag my_changes_001

然后更新到较新的版本:

git checkout master
tar zxvf open-source-project-0.2.tar.gz 
mv open-source-project-0.2/* .
rmdir open-source-project-0.2
git add .
git commit -m "Update to open-source-project-0.2"
git tag open-source-project-0.2

到目前为止,所有内容都已签入 git 存储库,现在是开始尝试合并更改的时候了:

git checkout -b merge_test open-source-project-0.2
git pull . my_changes_001

好运气好...

如果您想手动合并文件,我真的建议使用 KDiff3。假设 file1.c 来自 open-source-project-0.1,file2.c 来自 open-source-project-0.2,file3.c 来自您的更改,运行

kdiff3 -o merged_file.c file1.c file2.c 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:

tar zxvf open-source-project-0.1.tar.gz 
mv open-source-project-0.1 open-source-project
cd open-source-project
git init
git add .
git commit -m "Initial checkin of open-source-project-0.1"
git tag open-source-project-0.1

Now you can apply your original changes in a separate branch:

git checkout -b mychanges
cp /somewhere/where/your/changes/files/are/* .
git diff
git add .
git commit -m "My changes"
git tag my_changes_001

Then you update to the newer version:

git checkout master
tar zxvf open-source-project-0.2.tar.gz 
mv open-source-project-0.2/* .
rmdir open-source-project-0.2
git add .
git commit -m "Update to open-source-project-0.2"
git tag open-source-project-0.2

So far everything is checked in into the git repository, now is the time to start to try to merge your changes:

git checkout -b merge_test open-source-project-0.2
git pull . my_changes_001

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

kdiff3 -o merged_file.c file1.c file2.c file3.c
倾城°AllureLove 2024-09-01 19:42:14

由于声誉较低,我无法发表评论。所以我会添加一个新答案。我尝试了上面的答案之一仍然存在空白行问题。不过下面的方法对我有用。

diff -uraBN upstream local > filename.patch
cd upstream
patch --dry-run -p1 -i filename.patch #highly recommended recommended
patch -p1 -i filename.patch

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.

diff -uraBN upstream local > filename.patch
cd upstream
patch --dry-run -p1 -i filename.patch #highly recommended recommended
patch -p1 -i filename.patch
海之角 2024-09-01 19:42:14

您可以尝试之前向我建议的一些方法,一个有趣的“差异”解决方案:首先克隆该项目的最新版本。它不应该有任何本地更改。确保 .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.

两相知 2024-09-01 19:42:14
Folder 1: 
 index1.html
 css/style.css
 js/

Folder 2:
 index1.html
 index2.html
 css/style.css
 js/

现在我们想要比较两个文件夹 html1html2 目录。在 html1 中,CSS 发生了变化。

html2 中添加了另一个 index2.html。现在,我们想要从 html2html1

$ git diff html2/ html1/ >补丁/htmlchanges.patch

Folder 1: 
 index1.html
 css/style.css
 js/

Folder 2:
 index1.html
 index2.html
 css/style.css
 js/

Now we want to take diff of two folders html1 and html2 directories. In html1 the CSS is changed.

In html2 another index2.html is added. Now, we want to take diff from html2 to html1

$ git diff html2/ html1/ > patches/htmlchanges.patch

写下不归期 2024-09-01 19:42:14

您可能应该查看git rebase。也许即使是一个简单的 git pull 也能满足您的需求。

You should probably be looking at git rebase. Perhaps even a simple git pull will do what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文