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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2460558/
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 08:03:34  来源:igfitidea点击:

Creating a patch file from a diff of 2 folders

gitdiffpatch

提问by karatchov

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.

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

Whats 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.

将我的更改应用到新版本的最佳方法是什么?
我对 diff/patch 使用完全陌生,如果我能用 git 完成它,那就更好了。

回答by William Pursell

If you have two directories aand bthat are similar, and you want bto be the same as a, you can create and apply a patch with:

如果你有两个目录a,并b认为是相似的,你想b是一样的a,你可以创建并应用补丁有:

$ 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:

假设您有目录local(包含您本地版本的 upstream1.0)upstream1.0、 和upstream1.1。创建和应用您的更改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.

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

回答by niry

If the project is under git and you haven't committed your changes locally, you can simply do git diff > file.patchto get patchable diff data. If you have committed the changes locally, you can do git logto find the commit before you and than git diff commit_string > file.patch.

如果项目在 git 下并且您尚未在本地提交更改,则只需git diff > file.patch获取可修补的差异数据即可。如果您已在本地提交更改,则可以git log在您之前找到提交,然后再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.patchto create the patch file. In both cases you can try use patch later to apply the patch.

如果项目不在 git 下,或者如果您在没有克隆存储库的情况下 d/l 源代码(如标题所示),您可以使用diff -urN original_dir new_dir > file.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 也能够跟踪文件名更改。您需要大量了解 git 本身,并且需要一些时间才能正确使用 - 在开始使用它之前,您可能应该备份您的工作。

回答by hlovdal

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.

Git 支持检测文件的重命名,所以如果你很幸运,它会帮助你。以下是您应该做什么的大致草案。

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 存储库,现在是开始尝试合并更改的时候了:

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。假设 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

回答by hlovdal

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.

您可以尝试之前在这里向我建议的内容,一个有趣的“差异化”解决方案:首先克隆项目的最新版本。它不应该有任何本地更改。确保 .git 文件夹在那里。然后将您的工作树(即除 .git 文件夹之外的所有文件)复制到克隆的存储库。现在,如果您键入“git st”,您将看到所有已更改的内容。如果 git st 报告文件中没有真正更改的文件,您还需要整理间距和行尾(git config core.autocrlf ...)。

Now for each file in git st list, if you type git diff you'll see your changes.

现在对于 git st 列表中的每个文件,如果您键入 git diff 您将看到您的更改。

Then I would edit the files one by one, until git st looks like what I want to commit.

然后我会一一编辑文件,直到 git st 看起来像我想要提交的内容。

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.

我不会依赖制作补丁,因为它们非常挑剔。您很可能会得到一个“无法应用补丁”,而上面的解决方案为您提供了一个未暂存更改的列表,您可以按照您想要的任何顺序进行处理。

回答by Aj700

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

回答by Tallak Tveide

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

您可能应该查看git rebase. 也许即使是简单的git pull也会做你想做的。