简体   繁体   English

有没有更好的方法来执行git stash pop,然后如果我不合并则丢弃隐藏的更改?

[英]Is there a better way of doing a git stash pop and then discard the stashed changes if I don't merge?

Sometimes I git pop and get things I don't want to merge and want to throw away. 有时我会git pop并获取我不想合并的东西,并且想扔掉。 Is there a better way throwing away that stash than what I'm currently doing: 有没有比我目前正在做的更好的方式抛弃那些藏匿物:

git stash pop

Decide I don't want the stash. 决定我不想藏匿。

git reset HEAD
git checkout -- .
git stash drop

If I pop but don't merge the stash stays, which is why I drop it. 如果我弹出但不合并藏匿,这就是我放弃它的原因。

You don't have to do the git reset HEAD and git checkout -- etc ( which in itself is verbose, as you can just do git reset --hard to get a clean working directory) to drop a stash. 您不必执行git reset HEADgit checkout -- etc(这本身就是冗长的,因为您可以执行git reset --hard来获取干净的工作目录)来删除存储。 You just have to do git stash drop and the stash will be dropped. 你只需要做git stash drop就会丢弃存储。 Note that when you pop and git can't apply cleanly, the stash is not popped and is still there. 请注意,当您弹出并且git无法干净地应用时,隐藏不会弹出并且仍然存在。 You are then dropping it after cleaning up, which is not necessary. 然后在清理后丢弃它,这是不必要的。

You might also want to use the branch option of git stash if you are facing lots of conflicts. 如果您遇到很多冲突,您可能还想使用git stashbranch选项。 It will create a new branch off the HEAD where you stashed. 它将在您藏匿的HEAD上创建一个新的分支。

Are there two stashes? 有两个藏匿处吗? How are you popping and dropping it? 你是如何弹出放弃它? (Or is this why you mention merge - it doesn't apply cleanly?) (或者这就是为什么你提到合并 - 它不适用干净?)

In any case, git reset HEAD; git checkout -- . 无论如何, git reset HEAD; git checkout -- . git reset HEAD; git checkout -- . , assuming you're at the top level of your repo, is the same thing as just git reset --hard - that makes your index and work tree match the current commit. 假设你处于你的仓库的顶层,就像git reset --hard - 这使你的索引和工作树与当前的提交相匹配。 ( HEAD is never necessary as an argument for git reset ; it's the default.) HEAD永远不需要作为git reset的参数;它是默认值。)

If the stash needs dropping, you'll still have to drop it, though. 如果存储需要丢失,你仍然必须放弃它。

git reset --hard HEAD git reset --hard HEAD

will get you back to a clean working directory and index that matches HEAD. 会让你回到一个干净的工作目录和与HEAD匹配的索引。 the only problem is if you 'git stash pop'd you'll lose this stash forever. 唯一的问题是,如果你坚持使用stst pop'd,你将永远失去这个藏品。

If you think you might still want this stash, use 'git stash apply' which keeps this stash in the stash stack. 如果您认为您可能仍然需要此存储,请使用“git stash apply”将此存储保留在存储堆栈中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM