Gre*_*con 169
根据git-stash手册页,"存储被表示为一个提交,其树记录了工作目录的状态,其第一个父节点是HEAD创建存储时的提交",并git stash show -p给出了"记录在stas作为stashed状态和其原始父级之间的差异.
要保持其他更改不变,请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
Run Code Online (Sandbox Code Playgroud)
编辑:
对此的一个轻微改进就是用来git apply代替补丁:
git stash show -p | git apply --reverse
Run Code Online (Sandbox Code Playgroud)
或者,您也可以git apply -R用作速记git apply --reverse.
我最近发现这个非常方便...
小智 67
git checkout -f
Run Code Online (Sandbox Code Playgroud)
将删除任何非提交更改.
Cho*_*ith 19
从git手册页直接剪切粘贴 它的措辞明确,甚至包含别名;
取消应用藏匿 在你可能希望应用藏匿的变化,做一些工作,一些用例场景,但随后不应用最初来源于藏匿这些变化.Git没有提供这样的stash unapply命令,但是可以通过简单地检索与存储关联的补丁并反向应用它来实现效果:
$ git stash show -p stash@{0} | git apply -R
Run Code Online (Sandbox Code Playgroud)
同样,如果您没有指定存储,Git会采用最新的存储:
$ git stash show -p | git apply -R
Run Code Online (Sandbox Code Playgroud)
您可能想要创建别名并有效地向Git添加stash-unapply命令.例如:
$ git config --global alias.stash-unapply '!git stash show -p | git apply -R'
$ git stash apply
$ #... work work work
$ git stash-unapply
Run Code Online (Sandbox Code Playgroud)
小智 13
这已经过了,但如果我正确地解释了问题,我找到了一个简单的解决方案,请注意,这是我自己的术语中的解释:
git stash [save] 将保存当前更改并将当前分支设置为"清洁状态"
git stash list 给出类似的东西: stash@{0}: On develop: saved testing-stuff
git apply stash@{0}将像以前一样设置当前分支 stash [save]
git checkout .将当前分支设置为后 stash [save]
保存在存储中的代码不会丢失,可以git apply stash@{0}再次找到.
Anywhay,这对我有用!
如何反向应用存储?
除了其他人提到的,最简单的方法是先做
git reset HEAD
Run Code Online (Sandbox Code Playgroud)
然后签出所有本地更改
git checkout .
Run Code Online (Sandbox Code Playgroud)