如何反向申请藏匿? [英] How to reverse apply a stash?

查看:117
本文介绍了如何反向申请藏匿?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在git存储器中保存了一小块补丁。我已经使用 git stash apply 将它应用于我的工作副本。现在,我想通过反向应用修补程序来退出这些更改(有点像 git revert 会对存储进行操作)。



有谁知道如何做到这一点?



澄清:我的工作副本还有其他更改。我的具体情况很难描述,但你可以想象一些调试或实验代码存在。现在它在我的工作副本中与其他一些更改混合在一起,我希望看到效果以及没有从存储中进行更改。



它看起来不像目前,stash支持这个功能,但是 git stash apply --reverse 会是一个不错的功能。

解决方案

根据 git-stash联机帮助页,藏匿处表示为其记录工作目录状态的提交,并且其第一个父项是在创建存储时 HEAD 处的提交,并且 git stash show -p 为我们提供了存储器中记录的更改,作为隐藏状态与其原始父级之间的差异。 保留您的其他更改完好无损,请使用 git stash show -p | patch --reverse ,如下所示:

<$ p $在$ t $ / $
$ b $回声中初始化为空的Git仓库你好,世界d>消息

$ git add消息

$ git commit -am'初始提交'
[master(root-commit)]:created 1ff2478:初始提交
1个文件已更改,1个插入(+),0个删除( - )
创建模式100644消息

$ echo再次发送Hello>>消息

$ git stash

$ git status
#分支大师
没有提交(工作目录干净)

$ git stash apply
#对分支主
#已更改但未更新:
#(使用git add< file> ...更新将提交的内容)
#(使用git checkout - < file> ...放弃工作目录中的更改)

#修改:消息

没有更改添加到提交(使用git add和/或git commit -a)

$ echo你好所有>>消息

$ git diff
diff --git a / messages b / messages
index a5c1966..eade523 100644
--- a / messages
+++ b / messages
@@ -1 +1,3 @@
你好,世界
+再次您好!
+您好!全部

$ git隐藏show -p |修补程序 - 反向
修补文件信息
在#1成功使用fuzz 1.

$ git diff
diff --git a / messages b / messages
index a5c1966..364fc91 100644
--- a / messages
+++ b / messages
@@ -1 +1,2 @@
您好,全球
+您好!全部

编辑:



轻微改进是使用 git apply 代替补丁:

  git stash show -p | git apply --reverse 

或者,您也可以使用 git apply -R 作为 git apply --reverse 的缩写。

我一直在找这真的很方便最近......


I have a small patch saved away in my git stash. I've applied it to my working copy using git stash apply. Now, I'd like to back out those changes by reverse applying the patch (kind of like what git revert would do but against the stash).

Does anyone know how to do this?

Clarification: There are other changes in my working copy. My particular case is hard to describe but you can imagine some debugging or experimental code that's in the stash. Now it's mixed in my working copy with some other changes and I'd like to see the effect with and without the changes from the stash.

It doesn't look like stash supports this currently, but a git stash apply --reverse would be a nice feature.

解决方案

According to the git-stash manpage, "A stash is represented as a commit whose tree records the state of the working directory, and its first parent is the commit at HEAD when the stash was created," and git stash show -p gives us "the changes recorded in the stash as a diff between the stashed state and its original parent.

To keep your other changes intact, use git stash show -p | patch --reverse as in the following:

$ git init
Initialized empty Git repository in /tmp/repo/.git/

$ echo Hello, world >messages

$ git add messages

$ git commit -am 'Initial commit'
[master (root-commit)]: created 1ff2478: "Initial commit"
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 messages

$ echo Hello again >>messages

$ git stash

$ git status
# On branch master
nothing to commit (working directory clean)

$ git stash apply
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   messages
#
no changes added to commit (use "git add" and/or "git commit -a")

$ echo Howdy all >>messages

$ git diff
diff --git a/messages b/messages
index a5c1966..eade523 100644
--- a/messages
+++ b/messages
@@ -1 +1,3 @@
 Hello, world
+Hello again
+Howdy all

$ git stash show -p | patch --reverse
patching file messages
Hunk #1 succeeded at 1 with fuzz 1.

$ git diff
diff --git a/messages b/messages
index a5c1966..364fc91 100644
--- a/messages
+++ b/messages
@@ -1 +1,2 @@
 Hello, world
+Howdy all

Edit:

A light improvement to this is to use git apply in place of patch:

git stash show -p | git apply --reverse

Alternatively, you can also use git apply -R as a shorthand to git apply --reverse.

I've been finding this really handy lately...

这篇关于如何反向申请藏匿?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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