我可以从git-diff获得补丁兼容的输出吗? [英] Can I get a patch-compatible output from git-diff?

查看:75
本文介绍了我可以从git-diff获得补丁兼容的输出吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一件很简单的事情。我试图准备一个普通的补丁文件,所以我可以重新应用一些更改:

  $ git diff>在
$之前git diff something_here> save.patch
$ git checkout。
$ patch< save.patch
$ git diff>在
$之前后

something_here 空白它几乎可行,但文件名称不正确。我认为我只是我错过了一些选择。



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

编辑
我错在这里提出错误的问题。实际的问题是,我想保存我的更改,进行合并,然后重新应用更改(如果可能)?我问了它错误的方式,因为我用 来使用补丁来解决这些问题,并且 git diff 看起来像是我希望的那样做。



Charles Bailey的 comment 有正确的答案。对我来说,git-apply是正确的事情(git-stash看起来比我需要的重量更重,而rebasing和bundles绝对超出了我目前的技能水平。)我会接受Charles给出的答案(因为你不能接受评论)。感谢您的所有建议。



6年后编辑
正如熟悉该主题的人所知,我高估了 git stash 的难度。几乎每一天,我都会使用以下顺序:

  $ git stash 
$ git merge
$ git stash pop


解决方案

如果您想使用你需要删除git默认使用的 a / b / 前缀。你可以用 - no-prefix 选项来做到这一点(你也可以用补丁的 -p 选项来做到这一点) :

  git diff --no-prefix [<其他git-diff参数>] 
git diff
会比较容易,然后使用输出提供给 git apply



大部分时间我尽量避免使用文本补丁。通常,一个或多个临时提交与rebase, git stash 和捆绑包结合在一起更易于管理。



用例我认为 stash 是最合适的。

 #save uncommitted更改
git存储

#执行合并或其他操作
git合并某些分支

#重新应用更改,如果成功则删除存储
#(可能会要求您解决冲突)。
git stash pop


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_here blank it almost works, but the file names aren't right. I think I'm just I'm missing some option.

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.

Edit My 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 used to using patch to solve these sorts of problems and git diff looked like that's what it wanted me to do.

Charles Bailey's comment had 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.

Edit, 6 years later As 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:

$ git stash
$ git merge
$ git stash pop

解决方案

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-prefix option (you can also do this with patch's -p option):

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

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

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

For your use case I think that stash is most appropriate.

# 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

这篇关于我可以从git-diff获得补丁兼容的输出吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆