根据当前工作目录中的更改创建一个 git 补丁

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

Create a git patch from the changes in the current working directory

gitgit-patch

提问by vrish88

Say I have uncommitted changes in my working directory. How can I make a patch from those without having to create a commit?

假设我的工作目录中有未提交的更改。如何在无需创建提交的情况下从这些补丁中制作补丁?

采纳答案by sigjuice

git difffor unstaged changes. git diff --cachedfor staged changes.

git diff对于未分阶段的更改。 git diff --cached用于分阶段更改。

回答by jcarballo

If you haven't yet commited the changes, then:

如果您尚未提交更改,则:

git diff > mypatch.patch

But sometimes it happens that part of the stuff you're doing are new files that are untracked and won't be in your git diffoutput. So, one way to do a patch is to stage everything for a new commit (git addeach file, or just git add .) but don't do the commit, and then:

但有时您正在做的部分工作是未跟踪的新文件,不会出现在您的git diff输出中。因此,打补丁的一种方法是为新提交(git add每个文件,或只是git add .)暂存所有内容,但不进行提交,然后:

git diff --cached > mypatch.patch

Add the 'binary' option if you want to add binary files to the patch (e.g. mp3 files):

如果要将二进制文件添加到补丁中(例如 mp3 文件),请添加 'binary' 选项:

git diff --cached --binary > mypatch.patch

You can later apply the patch:

您可以稍后应用补丁:

git apply mypatch.patch

Note: You can also use --stagedas a synonym of --cached.

注意:您也可以--staged用作 的同义词--cached

回答by Merlyn Morgan-Graham

git diffand git applywill work for text files, but won't work for binary files.

git diff并且git apply适用于文本文件,但不适用于二进制文件。

You can easily create a full binary patch, but you will have to create a temporary commit. Once you've made your temporary commit(s), you can create the patch with:

您可以轻松创建完整的二进制补丁,但您必须创建一个临时提交。完成临时提交后,您可以使用以下命令创建补丁:

git format-patch <options...>

After you've made the patch, run this command:

制作补丁后,运行以下命令:

git reset --mixed <SHA of commit *before* your working-changes commit(s)>

This will roll back your temporary commit(s). The final result leaves your working copy (intentionally) dirty with the same changes you originally had.

这将回滚您的临时提交。最终结果使您的工作副本(有意地)与您最初所做的更改相同。

On the receiving side, you can use the same trick to apply the changes to the working copy, without having the commit history. Simply apply the patch(es), and git reset --mixed <SHA of commit *before* the patches>.

在接收方,您可以使用相同的技巧将更改应用到工作副本,而无需提交历史记录。只需应用补丁,然后git reset --mixed <SHA of commit *before* the patches>

Note that you might have to be well-synced for this whole option to work. I've seen some errors when applying patches when the person making them hadn't pulled down as many changes as I had. There are probably ways to get it to work, but I haven't looked far into it.

请注意,您可能必须保持同步才能使整个选项正常工作。我在应用补丁时看到了一些错误,因为制作补丁的人并没有像我那样进行那么多的更改。可能有办法让它工作,但我还没有深入研究它。



Here's how to create the same patches in Tortoise Git (not that I recommend using that tool):

以下是在 Tortoise Git 中创建相同补丁的方法(我不建议使用该工具):

  1. Commit your working changes
  2. Right click the branch root directory and click Tortoise Git-> Create Patch Serial
    1. Choose whichever range makes sense (Since: FETCH_HEADwill work if you're well-synced)
    2. Create the patch(es)
  3. Right click the branch root directory and click Tortise Git-> Show Log
  4. Right click the commit beforeyour temporary commit(s), and click reset "<branch>" to this...
  5. Select the Mixedoption
  1. 提交您的工作更改
  2. 右键单击分支根目录,然后单击Tortoise Git->Create Patch Serial
    1. 选择任何有意义的范围(SinceFETCH_HEAD如果您同步良好,将起作用)
    2. 创建补丁
  3. 右键单击分支根目录,然后单击Tortise Git->Show Log
  4. 临时提交之前右键单击提交,然后单击reset "<branch>" to this...
  5. 选择Mixed选项

And how to apply them:

以及如何应用它们:

  1. Right click the branch root directory and click Tortoise Git-> Apply Patch Serial
  2. Select the correct patch(es) and apply them
  3. Right click the branch root directory and click Tortise Git-> Show Log
  4. Right click the commit beforethe patch's commit(s), and click reset "<branch>" to this...
  5. Select the Mixedoption
  1. 右键单击分支根目录,然后单击Tortoise Git->Apply Patch Serial
  2. 选择正确的补丁并应用它们
  3. 右键单击分支根目录,然后单击Tortise Git->Show Log
  4. 右键单击补丁提交之前的提交,然后单击reset "<branch>" to this...
  5. 选择Mixed选项

回答by Ionel Sirbu

To create a patch with both modified & new files (staged) you can run:

要使用修改后的文件和新文件(暂存)创建补丁,您可以运行:

git diff HEAD > file_name.patch

回答by Eugen Konkov

I like:

我喜欢:

git format-patch HEAD~<N>

where <N>is number of last commits to save as patches.

哪里<N>是最后一次提交以保存为补丁的数量。

The details how to use the command are in the DOC

如何使用该命令的详细信息在DOC中

UPD
Hereyou can find how to apply them then.

UPD
在这里您可以找到如何应用它们。

UPDFor those who did not get the idea of format-patch
Add alias:

UPD对于那些没有想到format-patch
添加别名的人:

git config --global alias.make-patch '!bash -c "cd ${GIT_PREFIX};git add .;git commit -m ''uncommited''; git format-patch HEAD~1; git reset HEAD~1"'

Then at any directory of your project repository run:

然后在项目存储库的任何目录中运行:

git make-patch

This command will create 0001-uncommited.patchat your current directory. Patch will contain all the changes and untracked files that are visible to next command:

此命令将0001-uncommited.patch在您的当前目录中创建。补丁将包含对下一个命令可见的所有更改和未跟踪的文件:

git status .

回答by gitster

If you want to do binary, give a --binaryoption when you run git diff.

如果你想做二进制,--binary运行时给出一个选项git diff

回答by Anshu Kumar

We could also specify the files, to include just the files with relative changes, particularly when they span multiple directories e.x.

我们也可以指定文件,只包含有相对变化的文件,特别是当它们跨越多个目录时,例如

git diff ~/path1/file1.ext ~/path2/file2.ext...fileN.ext > ~/whatever_path/whatever_name.patch

I found this to be not specified in the answers or comments, which are all relevant and correct, so chose to add it. Explicit is better than implicit!

我发现答案或评论中没有说明这一点,这些都是相关且正确的,所以选择添加它。显式优于隐式!