git stash 是特定于分支的还是针对整个存储库的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20526355/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:27:26  来源:igfitidea点击:

Is git stash branch-specific or for the whole repository?

gitgit-stash

提问by amphibient

I went into a branch and did some work. I wanted to go into another branch but didn't want to commit so I did git stash. Then I did git checkout <otherbranch>. I did some work there and, just like in the first branch, I wanted to switch out of it before committing the work. So I did git stashthere too. I switched back to the first branch and tried to unstash it (git stash pop) thinking it would get the stash from that specific branch. I was surprised that it unstashed the stash from <otherbranch>(latest stashed). I was under the impression that stash is branch-specific but this behavior indicates that there is only one stash for the whole local repository.

我走进一家分店,做了一些工作。我想进入另一个分支,但不想提交,所以我做了git stash。然后我做到了git checkout <otherbranch>。我在那里做了一些工作,就像在第一个分支中一样,我想在提交工作之前退出它。所以我也去了git stash那里。我切换回第一个分支并尝试解开它 ( git stash pop) 认为它会从该特定分支中获取存储。我很惊讶它从<otherbranch>(最新的藏匿处)中取出藏匿处。我的印象是 stash 是特定于分支的,但这种行为表明整个本地存储库只有一个 stash。

Is git stashbranch-specific or for the whole repository? If it is for the whole repository, can I pass options to it to make it branch-specific?

git stash特定于分支还是针对整个存储库?如果是针对整个存储库,我可以将选项传递给它以使其特定于分支吗?

采纳答案by torek

To see the current stash stack:

要查看当前的存储堆栈:

git stash list

To pick a specific stash from the stack, refer to it by the stash@{number}shown by the above.

要从堆栈中选择特定的存储,请通过上面所示的方式引用它。stash@{number}

If you want the behavior to be per-branch, you can just make a commit (or multiple commits) on the branch. You can always "unmake" the commit(s) later (e.g., with git reset, either --softor --mixed; see the git reset documentation; or with git rebase -ito keep only the eventual "real" commit(s) while discarding the temporaries).

如果您希望行为是针对每个分支的,您只需在分支上进行一次提交(或多次提交)。您可以稍后“取消”提交(例如,使用git reset、 或者--soft--mixed;请参阅git reset 文档;或者git rebase -i在丢弃临时文件时仅保留最终的“真实”提交)。

(To really emulate git stashyou need at least two commits, one for the index state and one for the work-tree state. If you're not planning to save and restore the index state, though, you can just git add -Athe entire work-tree state and put that in the temporary commit. Alternatively, git stashis a shell script so you could copy and modify it pretty easily to make it work per-branch by default, using, e.g., refs/pb-stash/branchas its working name-space, rather than the single global refs/stashfor the entire repo. You'd still be able to bring a stash from one branch to another by naming it explicitly.)

(要真正模拟git stash您至少需要两次提交,一次用于索引状态,一次用于工作树状态。不过,如果您不打算保存和恢复索引状态,您可以只保存git add -A整个工作树状态并提出,在临时提交,或者git stash是一个shell脚本,所以你可以复制并修改它很容易让每个分支上工作在默认情况下,使用,例如,它的工作名称空间,而不是单一的全球性的整个存储库。您仍然可以通过明确命名将存储从一个分支带到另一个分支。)refs/pb-stash/branchrefs/stash

回答by abasterfield

No and No. git stash is per-repository.

不,不。 git stash 是每个存储库。

Hereis a nice page on how to use it.

是一个关于如何使用它的不错的页面。

回答by Trevor Boyd Smith

git stashis not per-branch.

git stash不是每个分支。

  • Instead of git stash(which can be lost easily when you have lots of stashes and branches)
  • I suggest doing a git committo save the unfinished code in your branch and when you are ready to finish the code do a git reset ${COMMIT_HASH_VALUE}to get the unfinished code back
  • git commitand git resetwhen used togethercorrectly can simulate a git stashfor a specific branch
  • 而不是git stash(当你有很多藏匿处和树枝时很容易丢失)
  • 我建议做一个git commit将未完成的代码保存在你的分支中,当你准备好完成代码时,做一个git reset ${COMMIT_HASH_VALUE}让未完成的代码回来
  • git commit并且正确git reset使用可以模拟git stash特定分支的 a

Here is a common real-life scenario that demonstrates the value and the usage the commitand resetcommands:

下面是一个常见的现实场景,演示了commitreset命令的值和用法:

  • you are working on feature branch X and your code doesn't even compile or pass the tests
  • there is a bug that is higher priority than the current new feature and so you must start work immediately on the bug fix
  • rather than do a git stash (and the stash gets lost in the mix because you have many stashes and many branches)
  • you can do a git commiton feature branch X
    • write down the COMMIT_HASH_VALUEfor later
  • checkout a new branch Y for the hot fix
  • finish the hot fix on branch Y (do a merge request to get the hot fix into the baseline and delete the hot fix branch)
  • then checkout the feature branch X again
  • to pop your unfinished work that didn't compile or pass testing --> just do a git reset ${COMMIT_HASH_VALUE}
  • 你在功能分支 X 上工作,你的代码甚至没有编译或通过测试
  • 存在比当前新功能优先级更高的错误,因此您必须立即开始修复错误
  • 而不是做一个 git stash (并且 stash 在混合中丢失了,因为你有很多 stash 和许多分支)
  • 你可以git commit在功能分支 X 上 做一个
    • 写下以COMMIT_HASH_VALUE备后用
  • 签出一个新的分支 Y 以进行热修复
  • 完成分支 Y 上的热修复(做一个合并请求将热修复进入基线并删除热修复分支)
  • 然后再次检出功能分支 X
  • 弹出未编译或未通过测试的未完成工作 --> 只需执行 git reset ${COMMIT_HASH_VALUE}

(FYI the default for git resetis --mixed)

(FYI默认git resetIS --mixed

回答by Adam

I am not sure why every answer here suggest to emulate stash with commit+reset. Stash is perfectly fine to use, especially when working on multiple branches. I also do not want to commit when I work on multiple branches, because I want that all modified changes are still highlighted in my editor when I return.

我不确定为什么这里的每个答案都建议用commit+模拟 stash reset。Stash 非常好用,尤其是在多个分支上工作时。当我在多个分支上工作时,我也不想提交,因为我希望在我返回时所有修改过的更改仍然在我的编辑器中突出显示。

So here is the stash workflow:

所以这里是 stash 工作流程:

Whenever you have to switch the branch and your not ready to commit, save your changes to the stack

每当您必须切换分支而您还没有准备好提交时,请将您的更改保存到堆栈中

git stash save "Your custom stash message"

When you return to a branch, checkout the stash

当你返回一个分支时,检查存储

git stash list

enter image description here

在此处输入图片说明

If you on branch FixIssue0203you could use use git stash popbecause this will apply the top stash@{0}and remove it from stash.

如果您在分支上,FixIssue0203您可以使用 usegit stash pop因为这将应用顶部stash@{0}并将其从存储中删除。

However, if your in branch ImproveReadmeyou should first apply the stash 1 git stash apply stash@{1}and then remove stash 1 from stack git stash drop stash@{1}.

但是,如果您在分支中,ImproveReadme您应该首先应用 stash 1 git stash apply stash@{1},然后从 stack 中删除 stash 1 git stash drop stash@{1}

That's it!

就是这样!