git 如何反向应用存储?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1020132/
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
How to reverse apply a stash?
提问by Pat Notz
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).
我在 git stash 中保存了一个小补丁。我已经使用git stash apply
. 现在,我想通过反向应用补丁来取消这些更改(有点像git revert
除了针对 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.
说明:我的工作副本中还有其他更改。我的特殊情况很难描述,但您可以想象一些在存储中的调试或实验代码。现在它在我的工作副本中与一些其他更改混合在一起,我想看看有和没有来自 stash 的更改的效果。
It doesn't look like stash supports this currently, but a git stash apply --reverse
would be a nice feature.
看起来 stash 目前不支持这个,但是git stash apply --reverse
一个不错的功能。
采纳答案by Greg Bacon
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.
根据git-stash 手册页,“存储表示为一个提交,其树记录了工作目录的状态,其第一个父项是HEAD
创建存储时的提交”,并git stash show -p
为我们提供“记录在stash 作为隐藏状态与其原始父级之间的差异。
To keep your other changes intact, use git stash show -p | patch --reverse
as in the following:
要保持其他更改不变,请使用git stash show -p | patch --reverse
如下:
$ 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 apply
代替补丁:
git stash show -p | git apply --reverse
Alternatively, you can also use git apply -R
as a shorthand to git apply --reverse
.
或者,您也可以将其git apply -R
用作git apply --reverse
.
I've been finding this really handy lately...
我最近发现这个真的很方便......
回答by Jakub Nar?bski
git stash
[save]
takes your working directory state, and your index state, and stashes them away, setting index and working area to HEAD
version.
git stash
[save]
获取您的工作目录状态和索引状态,并将它们隐藏起来,将索引和工作区域设置为HEAD
版本。
git stash apply
brings back those changes, so git reset --hard
would remove them again.
git stash apply
带回这些更改,因此git reset --hard
将再次删除它们。
git stash pop
brings back those changes and removes top stashed change, so git stash [save]
would return to previous (pre-pop) state in this case.
git stash pop
带回这些更改并删除顶部隐藏的更改,因此git stash [save]
在这种情况下将返回到之前的(预弹出)状态。
回答by salman
git checkout -f
will remove any non-commit changes.
将删除任何非提交更改。
回答by Choco Smith
The V1 git man page had a reference about un-applying a stash. The excerpt is below.
V1 git 手册页有一个关于取消应用存储的参考。摘录如下。
The newer V2 git man pagedoesn't include any reference to un-applying a stash but the below still works well
较新的 V2 git 手册页不包含任何对取消应用存储的引用,但以下内容仍然有效
Un-applying a StashIn some use case scenarios you might want to apply stashed changes, do some work, but then un-apply those changes that originally came from the stash. Git does not provide such a stash un-apply command, but it is possible to achieve the effect by simply retrieving the patch associated with a stash and applying it in reverse:
取消应用藏匿在您可能希望应用藏匿变化一些用例场景,做了一些工作,但后来不应用最初来源于藏匿这些变化。Git 没有提供这样的 stash un-apply 命令,但是可以通过简单地检索与 stash 关联的补丁并反向应用它来实现效果:
$ git stash show -p stash@{0} | git apply -R
Again, if you don't specify a stash, Git assumes the most recent stash:
同样,如果你没有指定一个 stash,Git 会假设最近的 stash:
$ git stash show -p | git apply -R
You may want to create an alias and effectively add a stash-unapply command to your Git. For example:
您可能希望创建一个别名并有效地将 stash-unapply 命令添加到您的 Git。例如:
$ git config --global alias.stash-unapply '!git stash show -p | git apply -R'
$ git stash apply
$ #... work work work
$ git stash-unapply
回答by Slim Sim
This is long over due, but if i interpret the problem correctly i have found a simple solution, note, this is an explanation in my own terminology:
这早就到期了,但如果我正确解释了问题,我找到了一个简单的解决方案,请注意,这是我自己的术语解释:
git stash [save]
will save away current changes and set your current branch to the "clean state"
git stash [save]
将保存当前更改并将当前分支设置为“干净状态”
git stash list
gives something like: stash@{0}: On develop: saved testing-stuff
git stash list
给出类似的东西: stash@{0}: On develop: saved testing-stuff
git apply stash@{0}
will set current branch as beforestash [save]
git apply stash@{0}
将像以前一样设置当前分支stash [save]
git checkout .
Will set current branch as afterstash [save]
git checkout .
将当前分支设置为之后stash [save]
The code that is saved in the stash is not lost, it can be found by git apply stash@{0}
again.
保存在 stash 中的代码不会丢失,可以git apply stash@{0}
再次找到。
Anywhay, this worked for me!
无论如何,这对我有用!
回答by Achal
How to reverse apply a stash?
如何反向应用存储?
Apart from what others have mentioned, easiest way is first do
除了其他人提到的,最简单的方法是先做
git reset HEAD
and then checkout all local changes
然后签出所有本地更改
git checkout .
回答by MHosafy
In addition to @Greg Bacon answer, in case binary files were added to the index and were part of the stash using
除了@Greg Bacon 的回答,如果二进制文件被添加到索引中并且是使用的存储的一部分
git stash show -p | git apply --reverse
may result in
可能会导致
error: cannot apply binary patch to '<YOUR_NEW_FILE>' without full index line
error: <YOUR_NEW_FILE>: patch does not apply
Adding --binary
resolves the issue, but unfortunately haven't figured out why yet.
添加--binary
解决了问题,但不幸的是还没有弄清楚原因。
git stash show -p --binary | git apply --reverse
回答by lifesoordinary
This is in addition to the above answers but adds search for the git stash based on the message as the stash number can change when new stashes are saved. I have written a couple of bash functions:
这是对上述答案的补充,但会根据消息添加对 git stash 的搜索,因为在保存新的 stash 时,stash 编号可能会更改。我写了几个 bash 函数:
apply(){
if [ "" ]; then
git stash apply `git stash list | grep -oPm1 "(.*)(?=:.*:.*.*)"`
fi
}
remove(){
if [ "" ]; then
git stash show -p `git stash list | grep -oPm1 "(.*)(?=:.*:.*.*)"` | git apply -R
git status
fi
}
- Create stash with name (message)
$ git stash save "my stash"
- To appply named
$ apply "my stash"
- To remove named stash
$ remove "my stash"
- 使用名称创建存储(消息)
$ git stash save "my stash"
- 申请命名
$ apply "my stash"
- 删除命名存储
$ remove "my stash"
回答by Amanpreet Singh
You can follow the image i shared to unstash if u accidentally tapped stashing.
如果你不小心点击了藏匿,你可以按照我分享的图像来解藏。