本文翻译自:How to recover stashed uncommitted changes

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. 我的开发分支中有一些未提交的更改,我使用git stash它们,但是有些更改在那些被隐藏的更改中非常重要。 Is there any way to get back those changes? 有没有办法找回这些变化?

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? 如果可能的话,我是否有可能检索到新分支的藏匿更改?


#1楼

参考:https://stackoom.com/question/1HjY9/如何恢复隐藏的未提交更改


#2楼

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

#3楼

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 . 只需查看您想要更改的分支,然后git stash apply Then use git diff to see the result. 然后使用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— then use git stash drop to get rid of it. 完成所有更改之后 - apply看起来很好并且您确定不再需要存储 - 然后使用git stash drop来摆脱它。

I always suggest using git stash apply rather than git stash pop . 我总是建议使用git stash apply而不是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. 不同之处在于apply留下隐藏的东西,以便轻松重新apply ,或者查看等。如果pop能够提取藏匿,它会立即drop它,如果你突然意识到你想要将它提取到其他地方(在不同的分支中),或者使用--index ,或者某些这样的东西,这并不容易。 If you apply , you get to choose when to drop . 如果您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. 尽管如此,这一切都很小,对于git的新手,它应该是一样的。 (And you can skip all the rest of this!) (你可以跳过其余的所有内容!)


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. 至少有三到四种不同的“使用git stash的方法”。 The above is for "way 1", the "easy way": 以上是“方式1”,“简单的方法”:

  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 plain git stash , same thing). 运行git stash save (或纯git stash ,同样的事情)。 Check out the other branch and use git stash apply . 查看其他分支并使用git stash apply This gets git to merge in your earlier changes, using git's rather powerful merge mechanism. 使用git相当强大的合并机制,这可以让git在您之前的更改中合并。 Inspect the results carefully (with git diff ) to see if you like them, and if you do, use git stash drop to drop the stash. 仔细检查结果 (使用git diff )看看你是否喜欢它们,如果你喜欢它们,使用git stash drop来删除藏匿处。 You're done! 你完成了!

  2. 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, these changes, and apply your stash too. 现在,您希望保留甚至移动这些更改, 应用您的存储。

    You can in fact git stash save again, as git stash makes a "stack" of changes. 实际上你可以再次git stash save ,因为git stash会进行一系列更改。 If you do that you have two stashes, one just called stash —but you can also write stash@{0} —and one spelled stash@{1} . 如果你这样做,你有两个stash ,一个叫做stash - 但你也可以写一个stash@{0} stash@{1} - 和一个拼写的stash@{1} Use git stash list (at any time) to see them all. 使用git stash list (随时)查看全部。 The newest is always the lowest-numbered. 最新的始终是编号最低的。 When you git stash drop , it drops the newest, and the one that was stash@{1} moves to the top of the stack. 当你git stash drop ,它会丢弃最新的,而那个被stash@{1}那个移动到堆栈的顶部。 If you had even more, the one that was stash@{2} becomes stash@{1} , and so on. 如果你有更多,那个stash@{2}会变成stash@{1} ,依此类推。

    You can apply and then drop a specific stash, too: git stash apply stash@{2} , and so on. 您也可以apply然后drop特定存储: git stash apply stash@{2} ,依此类推。 Dropping a specific stash, renumbers only the higher-numbered ones. 删除特定的存储,只重新编号较高编号的存储。 Again, the one without a number is also stash@{0} . 同样,没有数字的那个也是stash@{0}

    If you pile up a lot of stashes, it can get fairly messy (was the stash I wanted stash@{7} or was it stash@{4} ? Wait, I just pushed another, now they're 8 and 5?). 如果你堆积了很多东西,它会变得相当混乱(我stash@{7}还是stash@{4} ?等等,我只是推了另一个,现在它们是8和5?) 。 I personally prefer to transfer these changes to a new branch, because branches have names, and cleanup-attempt-in-December means a lot more to me than stash@{12} . 我个人更喜欢将这些更改转移到一个新的分支,因为分支有名称, cleanup-attempt-in-December对我来说比stash@{12} (The git stash command takes an optional save-message, and those can help, but somehow, all my stashes just wind up named WIP on branch .) git stash命令接受一个可选的保存消息,这些可以提供帮助,但不知何故,我的所有git stash最终都会WIP on branch上命名为WIP on branch 。)

  3. (Extra-advanced) You've used git stash save -p , or carefully git add -ed and/or git rm -ed specific bits of your code before running git stash save . (超高级)你已经使用了git stash save -p ,或者在运行git stash save之前仔细git add -ed和/或git rm -ed特定的代码位。 You had one version in the stashed index/staging area, and another (different) version in the working tree. 您在stashed索引/暂存区域中有一个版本,在工作树中有另一个(不同的)版本。 You want to preserve all this. 你想保留所有这些。 So now you use git stash apply --index , and that sometimes fails with: 所以现在使用git stash apply --index ,有时会失败:

     Conflicts in index. Try without --index. 
  4. You're using git stash save --keep-index in order to test "what will be committed". 您正在使用git stash save --keep-index来测试“将要提交的内容”。 This one is beyond the scope of this answer; 这个超出了这个答案的范围; see this other StackOverflow answer instead. 请看其他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" is a 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. apply操作会查看提交更改的内容,并尝试在任何位置重复提交。 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. 存储仍然存在( 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. 只要你有藏匿,你就可以使用git stash show -p来查看git stash show -p的简化版本。 (This simplified version looks only at the "final work tree" changes, not the saved index changes that --index restores separately.) The command git stash apply , without --index , just tries to make those same changes in your work-directory now. (这个简化版本只关注“最终工作树”的变化, 而不是保存的索引更改--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 modified working directory (or at least, to try to apply it). apply命令很高兴将存储应用于修改的工作目录(或至少尝试应用它)。 You can, for instance, do this: 例如,您可以这样做:

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 directory and 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. 如果你从一个干净的目录开始,只是做几个git apply操作,很容易退出:使用git reset --hard回到干净状态,并更改你的apply操作。 (That's why I recommend starting in a clean working directory first, for these complicated cases.) (这就是为什么我建议首先在干净的工作目录中开始,对于这些复杂的情况。)


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 Stuff,并且你已经做了一个藏匿,并且想要git stash apply --index ,但是不再可能用--index应用保存的存储,因为分支已经分歧自从你保存它以来太多了。

This is what git stash branch is for. 这就是git stash branch的用途。

If you: 如果你:

  1. check out the exact commit you were on when you did the original stash , then 检查出你原始stash时你所做的确切提交 ,然后
  2. create a new branch, and finally 创建一个新的分支,最后
  3. git stash apply --index

the attempt to re-create the changes definitely will work. 尝试重新创建更改肯定起作用。 This is what git stash branch newbranch does. 这就是git stash branch newbranch所做的git stash branch newbranch (And it then drops the stash since it was successfully applied.) (然后它会成功应用后丢弃存储。)


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 before commit ing. 当你有更改时,你必须在commit之前git add (或“ git add ”)它们。
  • Thus, when you ran git stash , you might have edited both files foo and zorg , but only staged one of those. 因此,当你运行git stash ,你可能已经编辑了foozorg这两个文件,但只上了其中一个。
  • So when you ask to get the stash back, it might be nice if it git add s the add ed things and does not git add the non-added things. 因此,当你要求获取存储时,如果git addadd东西并且没有 git add非添加的东西可能会很好。 That is, if you add ed foo but not zorg back before you did the stash , it might be nice to have that exact same setup. 也就是说,如果您add编辑foo但不zorg回你做了前stash ,它可能是不错的,准确的相同的设置。 What was staged, should again be staged; 什么上演,应该再次上演; what was modified but not staged, should again be modified but not staged. 修改但未上演的内容应再次修改但不上演。

The --index flag to apply tries to set things up this way. apply--index标志尝试以这种方式设置。 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. 但是,如果您的工作树已add内容,您可以看到这里可能存在一些问题。 If you leave out --index , the apply operation does not attempt to preserve the whole staged/unstaged setup. 如果省略--index ,则apply操作不会尝试保留整个staged / unstaged设置。 Instead, it just invokes git's merge machinery, using the work-tree commit in the "stash bag" . 相反,它只是使用“存储袋”中的工作树提交来调用git的合并机制。 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. 如果你不关心保存staged / unstaged, --index会使git stash apply更容易做到。


#4楼

To make this simple, you have two options to reapply your stash: 为简单起见,您有两种方法可以重新应用您的存储:

  1. git stash pop - Restore back to the saved state, but it deletes the stash from the temporary storage. git stash pop - 恢复到已保存的状态,但它会从临时存储中删除存储。
  2. git stash apply - Restore back to the saved state and leaves the stash list for possible later reuse. git stash apply - 恢复到已保存状态并保留存储列表,以便以后重用。

You can read in more detail about git stashes in this article. 您可以在本文中阅读有关git stashes的更多详细信息。


#5楼

To check your stash content :- 检查您的藏匿内容: -

git stash list git stash list

apply a particular stash no from stash list:- 从藏匿列表中应用特定的藏匿号: -

git stash apply stash@{2} git stash apply stash @ {2}

or for applying just the first stash:- 或仅申请第一个藏匿处: -

git stash pop git stash pop

如何恢复隐藏的未提交更改相关推荐

  1. git撤消所有未提交或未保存的更改

    本文翻译自:git undo all uncommitted or unsaved changes I'm trying to undo all changes since my last commi ...

  2. 当前分支上有未提交的更改时签出另一个分支

    本文翻译自:Checkout another branch when there are uncommitted changes on the current branch Most of the t ...

  3. idea恢复被覆盖的本地未提交代码

    记录:更新代码代码导致本地未提交的代码被覆盖 报错提示: 解释bug的大概含义就是:本地代码和服务器上的代码有冲突,合并失败了.未提交的更改被暂时搁置到了一边,需要手动处理合并. 一开始没在意,结果一 ...

  4. 使用git恢复未提交的误删数据

    不小心将项目中一个文件夹删除还未提交,或者已经提交, 此时想要恢复数据该怎么办? 答案是git reflog,使用git reflog命令可以帮助恢复git误操作,进行数据恢复. 操作过程: 打开终端 ...

  5. git删除本地所有未提交的更改

    1.git 删除本地所有未提交的更改 git checkout . && git clean -xdf 一般 git clean都是配合git reset 使用的 如果你有的修改已经加 ...

  6. git如何撤销未提交的更改

    本文介绍了如何将未提交到暂存区的代码进行撤销 撤销指令 git restore <file>... 后面可以指定路径或者文件.例如: #撤销当前文件夹的修改,可以这样写: git rest ...

  7. GIT 无法拉取,因为存在未提交的更改。 在重新拉取前提交或撤消更改

    1.在vs-工具-NuGet包管理器, 打开程序包管理器控制台,可以输入git命令. 2.依次输入以下命令: git stash git stash pop git checkout git pull ...

  8. SQL Server 中的事务与事务隔离级别以及如何理解脏读, 未提交读,不可重复读和幻读产生的过程和原因...

    原本打算写有关 SSIS Package 中的事务控制过程的,但是发现很多基本的概念还是需要有 SQL Server 事务和事务的隔离级别做基础铺垫.所以花了点时间,把 SQL Server 数据库中 ...

  9. 如何使用'git reset --hard HEAD'恢复到之前的提交? [重复]

    本文翻译自:How do I use 'git reset --hard HEAD' to revert to a previous commit? [duplicate] This question ...

最新文章

  1. Android开发之策略模式初探
  2. uber在限制新司机加入了,看看新政策把
  3. 成功解决ImportError: cannot import name 'pywrap_tensorflow'
  4. php smarty 序号,php – Smarty缩进和代码格式
  5. Android事件机制全然解析
  6. 使用当前更改创建Git分支
  7. open live writer代码高亮插件设置
  8. mysql所选路径已经存在_Win10环境下安装压缩包版本MySQL-8.0.13
  9. 3.2.3节:特权级(转载)
  10. Selenium2+python自动化6-八种元素元素定位(Firebug和firepath)
  11. Matlab中CIC滤波器的应用
  12. bugku上disordered_zip
  13. vue报错“NavigationDuplicat: Avoided redundant navigation to current location”解决方法
  14. 2021厦大信息学院夏令营经历
  15. 模仿百思不得姐项目笔记
  16. 树形动态规划之树的最大独立集
  17. 【实用教程】让我来帮你一键下载公zhong号图文封面吧(文末附小白教程)
  18. 乐视:基于Docker的RDS,我们是这样做的
  19. 那些会休息的人是如何度过假期的?
  20. 电机FOC中的坐标变换(CLARK+PARK+公式推导+仿真+C语言实现)

热门文章

  1. 腾讯面试题:两个玻璃杯判断哪个楼层恰巧碎裂
  2. 自定义SeekBar
  3. 华硕主板BIOS设置U盘启动教程
  4. yum-downloadonly软件包
  5. tmux命令(自用)
  6. cisco路由器EIGRP基础配置命令
  7. android RecyclerView 频繁设置数据闪退问题
  8. python写helloworld代码_helloworld代码怎么写?
  9. linux下无法删除文件夹,Linux 文件/文件夹无法删除问题解决方案
  10. 2021年焊工(初级)模拟考试及焊工(初级)实操考试视频