是git存储分支特定的还是整个存储库? [英] Is git stash branch-specific or for the whole repository?

查看:103
本文介绍了是git存储分支特定的还是整个存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进入一个分支并做了一些工作。我想进入另一个分支,但不想提交,所以我没有执行 git stash 。然后我做了 git checkout< otherbranch> 。我在那里做了一些工作,就像在第一个分支一样,我想在开始工作之前将其切换掉。所以我也做了 git stash 。我切换回第一个分支,并试图将它取消( git stash pop ),认为它会从该特定分支中获取存储。令我惊讶的是,它从< otherbranch> (最新保存)中释放了存储空间。我以为stash是特定于分支的,但是这种行为表明整个本地存储库只有一个存储。



git存储特定于分支还是整个存储库?如果是整个存储库,我可以传递选项使其成为分支特定的吗?

解决方案

要查看当前存储堆栈:

  git存储列表

要从堆栈中选择一个特定的存储器,请参阅 stash @ { number } 以上。



如果您希望行为是按分支的,您可以在分支上进行提交(或多个提交)。您可以随后提交提交(例如,使用 git reset - soft - 混合;请参阅 git reset documentation ;或者用 git rebase -i 来保留最终的真实提交,同时丢弃临时对象) 。为了真正模拟 git stash ,您至少需要两次提交,一次用于索引状态,另一次用于工作树状态如果你不打算保存和恢复索引状态,你可以在整个工作树状态下 git add -A 或者, git stash 是一个shell脚本,因此您可以非常容易地复制和修改它,使其在默认情况下按分支工作,例如, refs / pb-stash / branch 作为其工作名称空间,而不是单个全局 refs / stash code>为整个回购ð仍然能够通过明确地将其命名为使一个存储从一个分支到另一个。)


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 stash there 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.

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

解决方案

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.

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 --soft or --mixed; see the git reset documentation; or with git rebase -i to keep only the eventual "real" commit(s) while discarding the temporaries).

(To really emulate git stash you 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 -A the entire work-tree state and put that in the temporary commit. Alternatively, git stash is 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/branch as its working name-space, rather than the single global refs/stash for the entire repo. You'd still be able to bring a stash from one branch to another by naming it explicitly.)

这篇关于是git存储分支特定的还是整个存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆