git 如何恢复隐藏的未提交更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19003009/
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 recover stashed uncommitted changes
提问by Aswathy P Krishnan
I had some uncommitted changes in my development branch and I stashed them using git stash
, but there were some changes which were very important among those stashed ones. Is there any way to get back those changes?
我的开发分支中有一些未提交的更改,我使用 隐藏了它们git stash
,但是在这些隐藏的更改中,有一些更改非常重要。有什么办法可以恢复这些变化吗?
Also, I have made some changes on top of the stashed code files since.
此外,从那以后,我对隐藏的代码文件进行了一些更改。
Is there any chance I can retrieve the stashed changes to a new branch if possible?
如果可能的话,我是否有机会将隐藏的更改检索到新分支?
回答by torek
The easy answer to the easy question is git stash apply
简单问题的简单答案是 git stash apply
Just check out the branch you want your changes on, and then git stash apply
. Then use git diff
to see the result.
只需检查您想要更改的分支,然后git stash apply
. 然后使用git diff
查看结果。
After you're all done with your changes—the apply
looks good and you're sure you don't need the stash any more—thenuse git stash drop
to get rid of it.
在您完成所有更改后 -apply
看起来不错并且您确定不再需要藏匿处 -然后使用git stash drop
它来摆脱它。
I always suggest using git stash apply
rather than git stash pop
. The difference is that apply
leaves the stash around for easy re-try of the apply
, or for looking at, etc. If pop
is able to extract the stash, it will immediately also drop
it, and if you the suddenly realize that you wanted to extract it somewhere else (in a different branch), or with --index
, or some such, that's not so easy. If you apply
, youget to choose when to drop
.
我总是建议使用git stash apply
而不是git stash pop
. 不同之处在于,apply
将 stash 留在周围以便于重试apply
或查看等。如果pop
能够提取 stash,它也会立即提取drop
它,如果您突然意识到您想将它提取到某个地方else(在不同的分支中),或 with --index
,或类似的,这并不容易。如果你apply
,你可以选择什么时候去drop
。
It's all pretty minor one way or the other though, and for a newbie to git, it should be about the same. (And you can skip all the rest of this!)
不管怎样,这都是很小的事情,对于 git 新手来说,它应该大致相同。(你可以跳过剩下的所有内容!)
What if you're doing more-advanced or more-complicated stuff?
如果你正在做更高级或更复杂的事情怎么办?
There are at least three or four different "ways to use git stash", as it were. The above is for "way 1", the "easy way":
至少有三到四种不同的“使用 git stash 的方法”。以上是“方式1”,“简单的方式”:
You started with a clean branch, were working on some changes, and then realized you were doing them in the wrong branch. You just want to take the changes you have now and "move" them to another branch.
This is the easy case, described above. Run
git stash save
(or plaingit stash
, same thing). Check out the other branch and usegit stash apply
. This gets git to merge in your earlier changes, using git's rather powerful merge mechanism. Inspect the results carefully(withgit diff
) to see if you like them, and if you do, usegit stash drop
to drop the stash. You're done!You started some changes and stashed them. Then you switched to another branch and started more changes, forgetting that you had the stashed ones.
Now you want to keep, or even move, thesechanges, andapply your stash too.
You can in fact
git stash save
again, asgit stash
makes a "stack" of changes. If you do that you have two stashes, one just calledstash
—but you can also writestash@{0}
—and one spelledstash@{1}
. Usegit stash list
(at any time) to see them all. The newest is always the lowest-numbered. When yougit stash drop
, it drops the newest, and the one that wasstash@{1}
moves to the top of the stack. If you had even more, the one that wasstash@{2}
becomesstash@{1}
, and so on.You can
apply
and thendrop
a specific stash, too:git stash apply stash@{2}
, and so on. Dropping a specific stash, renumbers only the higher-numbered ones. Again, the one without a number is alsostash@{0}
.If you pile up a lot of stashes, it can get fairly messy (was the stash I wanted
stash@{7}
or was itstash@{4}
? Wait, I just pushed another, now they're 8 and 5?). I personally prefer to transfer these changes to a new branch, because branches have names, andcleanup-attempt-in-December
means a lot more to me thanstash@{12}
. (Thegit stash
command takes an optional save-message, and those can help, but somehow, all my stashes just wind up namedWIP on branch
.)(Extra-advanced) You've used
git stash save -p
, or carefullygit add
-ed and/orgit rm
-ed specific bits of your code before runninggit stash save
. You had one version in the stashed index/staging area, and another (different) version in the working tree. You want to preserve all this. So now you usegit stash apply --index
, and that sometimes fails with:Conflicts in index. Try without --index.
You're using
git stash save --keep-index
in order to test "what will be committed". This one is beyond the scope of this answer; see this other StackOverflow answerinstead.
您从一个干净的分支开始,正在进行一些更改,然后意识到您在错误的分支中进行了更改。您只想采取您现在所做的更改并将它们“移动”到另一个分支。
这是上面描述的简单情况。运行
git stash save
(或普通的git stash
,同样的事情)。检查另一个分支并使用git stash apply
. 这让 git 使用 git 相当强大的合并机制合并到您之前的更改中。 仔细检查结果(使用git diff
),看看您是否喜欢它们,如果喜欢,请使用git stash drop
删除藏匿处。你完成了!你开始了一些改变并把它们藏起来。然后你切换到另一个分支并开始更多的更改,忘记了你有隐藏的那些。
现在您想保留甚至移动这些更改,并应用您的存储。
你实际上可以
git stash save
再一次,那样git stash
做一个“堆栈”的改变。如果你这样做,你有两个 stash,一个只是调用stash
- 但你也可以写stash@{0}
- 一个拼写stash@{1}
。使用git stash list
(随时)查看所有内容。最新的总是编号最小的。当您 时git stash drop
,它会丢弃最新的,并且stash@{1}
移动到堆栈顶部的那个。如果你有更多,那个stash@{2}
变成stash@{1}
,依此类推。你也可以
apply
然后drop
一个特定的 stash:git stash apply stash@{2}
,等等。丢弃特定的藏匿处,仅对编号较高的物品重新编号。同样,没有数字的那个也是stash@{0}
。如果你堆积了很多储藏,它会变得相当混乱(是我想要的储藏
stash@{7}
还是它stash@{4}
?等等,我只是推了另一个,现在它们是 8 和 5?)。我个人更喜欢将这些更改转移到新分支,因为分支有名称,而且cleanup-attempt-in-December
对我来说比stash@{12}
. (该git stash
命令需要一个可选的保存消息,这些消息会有所帮助,但不知何故,我所有的藏匿处都以名为WIP on branch
.)(特高级)您已经使用
git stash save -p
,或仔细git add
-ed和/或git rm
运行在你的代码的-ed特定位git stash save
。您在隐藏的索引/暂存区域中有一个版本,在工作树中有另一个(不同的)版本。你想保留这一切。所以现在你使用git stash apply --index
, 有时会失败:Conflicts in index. Try without --index.
您正在使用
git stash save --keep-index
以测试“将提交的内容”。这个超出了这个答案的范围;请参阅其他 StackOverflow 答案。
For complicated cases, I recommend starting in a "clean" working directory first, by committing any changes you have now (on a new branch if you like). That way the "somewhere" that you are applying them, has nothing else in it, and you'll just be trying the stashed changes:
对于复杂的情况,我建议首先从“干净”的工作目录开始,通过提交您现在所做的任何更改(如果您愿意,可以在新分支上)。这样你正在应用它们的“某处”就没有其他东西了,你只会尝试隐藏的更改:
git status # see if there's anything you need to commit
# uh oh, there is - let's put it on a new temp branch
git checkout -b temp # create new temp branch to save stuff
git add ... # add (and/or remove) stuff as needed
git commit # save first set of changes
Now you're on a "clean" starting point. Or maybe it goes more like this:
现在您处于“干净”的起点。或者它更像是这样:
git status # see if there's anything you need to commit
# status says "nothing to commit"
git checkout -b temp # optional: create new branch for "apply"
git stash apply # apply stashed changes; see below about --index
The main thing to remember is that the "stash" isa commit, it's just a slightly "funny/weird" commit that's not "on a branch". The apply
operation looks at what the commit changed, and tries to repeat it wherever you are now. The stash will still be there (apply
keeps it around), so you can look at it more, or decide this was the wrong place to apply
it and try again differently, or whatever.
要记住的主要事情是“stash”是一个提交,它只是一个稍微“有趣/奇怪”的提交,而不是“在一个分支上”。该apply
操作查看提交更改的内容,并尝试在您现在所在的任何地方重复它。藏匿处仍将在那里(apply
保留它),因此您可以更多地查看它,或者确定这是错误的地方,apply
然后以不同的方式再试一次,或者其他什么。
Any time you have a stash, you can use git stash show -p
to see a simplified version of what's in the stash. (This simplified version looks only at the "final work tree" changes, notthe saved index changes that --index
restores separately.) The command git stash apply
, without --index
, just tries to make those samechanges in your work-directory now.
任何时候您有一个藏匿处,您都可以使用它git stash show -p
来查看藏匿处内容的简化版本。(此简化版本仅查看“最终工作树”更改,而不是--index
单独恢复的已保存索引更改。)命令git stash apply
,而没有--index
,现在只是尝试在您的工作目录中进行相同的更改。
This is true even if you already have some changes. The apply
command is happy to apply a stash to a modifiedworking directory (or at least, to try to apply it). You can, for instance, do this:
即使您已经进行了一些更改,也是如此。该apply
命令很乐意将 stash 应用到修改后的工作目录(或至少尝试应用它)。例如,您可以这样做:
git stash apply stash # apply top of stash stack
git stash apply stash@{1} # and mix in next stash stack entry too
You can choose the "apply" order here, picking out particular stashes to apply in a particular sequence. Note, however, that each time you're basically doing a "git merge", and as the merge documentation warns:
您可以在此处选择“应用”顺序,挑选特定的藏匿处以特定顺序应用。但是请注意,每次您基本上都在执行“git merge”,并且正如合并文档所警告的那样:
Running git merge with non-trivial uncommitted changes is discouraged: while possible, it may leave you in a state that is hard to back out of in the case of a conflict.
不鼓励将 git merge 与非平凡的未提交更改一起运行:虽然可能,但它可能会让您处于在发生冲突时难以退出的状态。
If you start with a clean directoryand are just doing several git apply
operations, it's easy to back out: use git reset --hard
to get back to the clean state, and change your apply
operations. (That's why I recommend starting in a clean working directory first, for these complicated cases.)
如果您从一个干净的目录开始,并且只是在执行几个git apply
操作,则很容易退出:用于git reset --hard
返回到干净状态,并更改您的apply
操作。(这就是为什么我建议首先从一个干净的工作目录开始,对于这些复杂的情况。)
What about the very worst possible case?
最坏的情况呢?
Let's say you're doing Lots Of Advanced Git Stuff, and you've made a stash, and want to git stash apply --index
, but it's no longer possible to apply the saved stash with --index
, because the branch has diverged too much since the time you saved it.
假设你正在做很多高级 Git 东西,你已经做了一个 stash,并且想要git stash apply --index
,但是不再可能应用保存的 stash --index
,因为自从你保存它以来,分支已经分歧太多了。
This is what git stash branch
is for.
这git stash branch
就是为了。
If you:
如果你:
- check out the exact commityou were on when you did the original
stash
, then - create a new branch, and finally
git stash apply --index
- 检查您在执行原始提交时所做的确切提交
stash
,然后 - 创建一个新分支,最后
git stash apply --index
the attempt to re-create the changes definitely willwork. This is what git stash branch newbranch
does. (And it then drops the stash since it was successfully applied.)
重新创建更改的尝试肯定会奏效。这就是它的作用。(然后它会丢弃存储,因为它已成功应用。)git stash branch newbranch
Some final words about --index
(what the heck is it?)
关于--index
(到底是什么?)
What the --index
does is simple to explain, but a bit complicated internally:
它的--index
作用很容易解释,但内部有点复杂:
- When you have changes, you have to
git add
(or "stage") them beforecommit
ing. - Thus, when you ran
git stash
, you mighthave edited both filesfoo
andzorg
, but only staged one of those. - So when you ask to get the stash back, it might be nice if it
git add
s theadd
ed things and does notgit add
the non-added things. That is, if youadd
edfoo
but notzorg
back before you did thestash
, it might be nice to have that exact same setup. What was staged, should again be staged; what was modified but not staged, should again be modified but not staged.
- 当您有更改时,您必须
git add
在commit
ing之前(或“暂存”)它们。 - 因此,当您运行 时
git stash
,您可能已经编辑了两个文件foo
和zorg
,但仅暂存其中一个。 - 因此,当您要求取回 stash 时,如果它
git add
是add
ed 的东西而不git add
是非添加的东西,那可能会很好。也就是说,如果您在执行之前add
edfoo
但没有zorg
返回stash
,那么拥有完全相同的设置可能会很好。上演的,应该再上演;已修改但未上演的内容应再次修改但未上演。
The --index
flag to apply
tries to set things up this way. If your work-tree is clean, this usually just works. If your work-tree already has stuff add
ed, though, you can see how there might be some problems here. If you leave out --index
, the apply
operation does not attempt to preserve the whole staged/unstaged setup. Instead, it just invokes git's merge machinery, using the work-tree commit in the "stash bag". If you don't care about preserving staged/unstaged, leaving out --index
makes it a lot easier for git stash apply
to do its thing.
试图以这种方式设置事物的--index
标志apply
。如果您的工作树是干净的,这通常会起作用。但是,如果您的工作树已经有东西add
ed,您可以看到这里可能存在一些问题。如果您省略--index
,则apply
操作不会尝试保留整个暂存/未暂存设置。相反,它只是调用 git 的合并机制,使用"stash bag" 中的工作树提交。如果您不关心保留暂存/未暂存,则省略--index
它会使git stash apply
做它的事情更容易。
回答by Stefano Falasca
git stash pop
will get everything back in place
将使一切恢复原状
as suggested in the comments, you can use git stash branch newbranch
to apply the stash to a new branch, which is the same as running:
正如评论中所建议的,您可以使用git stash branch newbranch
将存储应用于新分支,这与运行相同:
git checkout -b newbranch
git stash pop
回答by Nesha Zoric
To make this simple, you have two options to reapply your stash:
为简单起见,您有两个选项可以重新应用您的存储:
git stash pop
- Restore back to the saved state, but it deletes the stash from the temporary storage.git stash apply
- Restore back to the saved state and leaves the stash list for possible later reuse.
git stash pop
- 恢复到保存状态,但会从临时存储中删除存储。git stash apply
- 恢复到已保存的状态并保留存储列表以供以后重复使用。
You can read in more detail about git stashesin this article.
您可以在本文中阅读有关git stashes 的更多详细信息。
回答by Shivansh Rajolia - HeLleR
To check your stash content :-
要检查您的藏匿内容:-
git stash list
git 存储列表
apply a particular stash no from stash list:-
从存储列表中应用特定的存储编号:-
git stash apply stash@{2}
git stash 应用 stash@{2}
or for applying just the first stash:-
或仅应用第一个储藏:-
git stash pop
git stash pop
Note:git stash pop will remove the stash from your stash list whereas git stash apply wont. So use them accordingly.
注意:git stash pop 将从您的存储列表中删除存储,而 git stash apply 不会。所以相应地使用它们。
回答by Zack
On mac this worked for me:
在 mac 上这对我有用:
git stash list(see all your stashs)
git stash list(查看你所有的藏品)
git stash list
git stash apply (just the number that you want from your stash list)
git stash apply(只是你想要从你的藏匿列表中得到的数字)
like this:
像这样:
git stash apply 1
回答by S.Sandeeptha
you can stash the uncommitted changes using "git stash" then checkout to a new branch using "git checkout -b " then apply the stashed commits "git stash apply"
您可以使用“git stash”隐藏未提交的更改,然后使用“git checkout -b”签出到新分支,然后应用隐藏的提交“git stash apply”