如何在应用 git stash 时自动选择并应用当前检出分支的唯一存储?

huangapple go评论58阅读模式
英文:

When applying a git stash, how to automatically select and apply the only stash for the currently checked out branch?

问题

有很多分支的存储库可能会为每个分支保留一个存储,即表示该分支上的工作进展的多个存储。是否有一种方法可以切换到分支并只应用该分支的唯一存储,而无需首先选择它?

英文:

a repo with many branches might have one stash for each branch, ie, that many stashes, representing a wip on that branch. is there a way to switch to a branch and just apply the only stash for that branch, without having to select it first?

答案1

得分: 1

以下是翻译好的内容:

  1. Stashes aren't on branches. (存储区并不位于分支上。这是存储区提交的关键之一:因为它不位于任何分支上,所以可以轻松使用 git stash 在分支之间“移动”更改。在 git stash 工作的层次上,分支是无关紧要的—甚至不需要存在。)

  2. What this really means is that there is no such one-to-one correspondence: (这实际上意味着没有一对一的对应关系:)

    • It is true that any one given stash's default commit message is <code>WIP on branch</code>. (的确,任何一个给定的存储区的默认提交消息都是 <code>WIP on branch</code>。)

    • But you can have ten "WIP on master" and no "WIP on develop", or 2 "WIP on develop", one "WIP on master", and so on. (但你可以有十个“WIP on master”和没有“WIP on develop”,或者2个“WIP on develop”,一个“WIP on master”,依此类推。)

    • And, you can give any stash a different commit message at the time you create it. If you're going to use git stash,<sup>1</sup> this is probably a good idea. (而且,在创建存储区时,你可以为任何存储区提供一个不同的提交消息。如果你要使用 git stash1 这可能是个好主意。)

  3. All that aside, the short answer is no. The command probably should have an easier way to search git stash list output, but at least as of now (Git 2.24) it does not. See https://stackoverflow.com/q/11269256/1256452 for numerous ways to write your own searcher. (除此之外,简短的答案是不行。这个命令可能应该有一个更容易搜索 git stash list 输出的方法,但至少在目前(Git 2.24)还没有。请参阅 https://stackoverflow.com/q/11269256/1256452,了解编写自己的搜索器的多种方法。)

1 我建议不要经常使用 git stash。它太容易创建很多存储区,然后迷失在其中,而且它们并没有做任何通过创建普通提交无法完成的事情。请记住,git stash 只是创建了一些不在任何分支上的提交。每个存储区由两个或三个提交组成,以一种可以使 git stash apply 使用它们的方式创建,但 Git 的很多其他部分不能很好地使用它们。

英文:

Stashes aren't on branches. (That's about half the point of a stash commit: since it's not on any branch, it is easy to use git stash to "move" changes between branches. In the level at which git stash works, branches are irrelevant—they don't even need to exist.)

What this really means is that there is no such one-to-one correspondence:

  • It is true that any one given stash's default commit message is <code>WIP on branch</code>.
  • But you can have ten "WIP on master" and no "WIP on develop", or 2 "WIP on develop", one "WIP on master", and so on.
  • And, you can give any stash a different commit message at the time you create it. If you're going to use git stash,<sup>1</sup> this is probably a good idea.

All that aside, the short answer is no. The command probably should have an easier way to search git stash list output, but at least as of now (Git 2.24) it does not. See https://stackoverflow.com/q/11269256/1256452 for numerous ways to write your own searcher.


<sup>1</sup>I recommend not using git stash very much. It's way too easy to create a lot of them and then lose track of them, and they don't do anything you cannot do by making a regular commit. Remember, all git stash does is make some commits that are on no branch. Each stash consists of either two or three commits, that are made in a way such that git stash apply can use them, but a lot of the rest of Git can't use them very well.

huangapple
  • 本文由 发表于 2020年1月3日 23:23:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定