我可以从 git-diff 获得补丁兼容的输出吗?

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

Can I get a patch-compatible output from git-diff?

git

提问by Malvolio

I am doing something very simple wrong. I'm trying to prepare an ordinary patch file, so I can reapply some changes:

我正在做一些非常简单的错误。我正在尝试准备一个普通的补丁文件,因此我可以重新应用一些更改:

$ git diff > before
$ git diff something_here > save.patch
$ git checkout . 
$ patch < save.patch
$ git diff > after
$ diff before after
$

With something_hereblankit almost works, but the file names aren't right. I think I'm just I'm missing some option.

something_here空白几乎工作,但文件名是不正确的。我想我只是缺少一些选择。

In real life, I am going to do a merge after the checkout, so the patch might fail there, but you see what I'm getting at.

在现实生活中,我将在结帐后进行合并,因此补丁可能会在那里失败,但您明白我在做什么。

EditMy fault here for asking the wrong question. The actual question is, I want to save my changes away, do a merge, then re-apply the changes, if possible? I asked it the wrong way because I am usedto using patch to solve these sorts of problems and git difflooked like that's what it wanted me to do.

在这里编辑我的错,提出错误的问题。实际问题是,如果可能,我想保存更改,进行合并,然后重新应用更改?我问错了,因为我习惯于使用补丁来解决这类问题,git diff看起来这就是它想要我做的。

Charles Bailey's commenthad the right answer. For me, git-apply is the right thing to do (git-stash looks more heavy-weight than I need and rebasing and bundles is definitely beyond my current skill level.) I'm going to accept the answer Charles gave (because you can't accept a comment). Thanks for all the suggestions.

查尔斯·贝利 (Charles Bailey) 的评论给出了正确答案。对我来说, git-apply 是正确的做法(git-stash 看起来比我需要的更重要,并且重新定位和捆绑绝对超出了我目前的技能水平。)我将接受查尔斯给出的答案(因为你不能接受评论)。感谢所有的建议。

Edit, 6 years laterAs anyone familiar with the subject knows, I over-estimated the difficulty of git stash. Pretty much every day or so, I will use the following sequence:

编辑,6 年后熟悉该主题的任何人都知道,我高估了git stash. 几乎每天左右,我都会使用以下顺序:

$ git stash
$ git merge
$ git stash pop

采纳答案by CB Bailey

If you want to use patch you need to remove the a/b/prefixes that git uses by default. You can do this with the --no-prefixoption (you can also do this with patch's -poption):

如果你想使用补丁,你需要删除a/b/git 默认使用的前缀。您可以使用--no-prefix选项执行此操作(您也可以使用补丁-p选项执行此操作):

git diff --no-prefix [<other git-diff arguments>]

Usually though, it is easier to use straight git diffand then use the output to feed to git apply.

但是,通常更容易使用直接git diff然后使用输出馈送到git apply.

Most of the time I try to avoid using textual patches. Usually one or more of temporary commits combined with rebase, git stashand bundles are easier to manage.

大多数时候我尽量避免使用文本补丁。通常一个或多个临时提交与 rebase 相结合,git stash并且 bundle 更易于管理。

For your use case I think that stashis most appropriate.

对于您的用例,我认为这stash是最合适的。

# save uncommitted changes
git stash

# do a merge or some other operation
git merge some-branch

# re-apply changes, removing stash if successful
# (you may be asked to resolve conflicts).
git stash pop

回答by ndim

Just use -p1: you will need to use -p0in the --no-prefixcase anyway, so you can just leave out the --no-prefixand use -p1:

只需使用-p1:无论如何您都需要-p0--no-prefix案例中使用,因此您可以省略--no-prefix并使用-p1

$ git diff > save.patch
$ patch -p1 < save.patch

$ git diff --no-prefix > save.patch
$ patch -p0 < save.patch

回答by Henrik Gustafsson

The git diffs have an extra path segment prepended to the file paths. You can strip the this entry in the path by specifying -p1 with patch, like so:

git diffs 在文件路径前面有一个额外的路径段。您可以通过使用补丁指定 -p1 来去除路径中的 this 条目,如下所示:

patch -p1 < save.patch

回答by Matej

  1. I save the diff of the current directory (including uncommitted files) against the current HEAD.
  2. Then you can transport the save.patchfile to wherever (including binary files).
  3. On your target machine, apply the patch using git apply <file>
  1. 我保存当前目录(包括未提交的文件)与当前 HEAD 的差异。
  2. 然后您可以将save.patch文件传输到任何地方(包括二进制文件)。
  3. 在您的目标机器上,使用 git apply <file>

Note: it diff's the currently staged files too.

注意:它也与当前暂存的文件不同。

$ git diff --binary --staged HEAD > save.patch
$ git reset --hard
$ <transport it>
$ git apply save.patch

回答by slowstart

A useful trick to avoid creating temporary patch files:

避免创建临时补丁文件的有用技巧:

git diff | patch -p1 -d [dst-dir]