如何删除 git 中的选择性存储?

How can I remove selective stashes in git?

我有藏品清单,想有选择地删除其中一些。

对于我的示例,我想删除 stash@{1}、stash@{3}、stash@{4}、stash@6}

$git stash list

stash@{1}: Tests On branch1-om: Test for #8
stash@{2}: WIP On branch1-om: WIP for #12
stash@{3}: Temp on branch1-om: 0a447303 Add Unit Tests for the HttpClient
stash@{4}: To delete stash: 233abc813c fix
stash@{5}: WIP on branchn-test-om: 4a42e4 WIP: Commit
stash@{6}: On branch-test-om: projects deleted/modified when rebuilt
stash@{7}: My configurations: Apply my local dev configurations

我当前的步骤非常重复:

$git stash drop stash@{1}

$git stash list

stash@{1}: WIP On branch1-om: WIP for #12
stash@{2}: Temp on branch1-om: 0a447303 Add Unit Tests for the HttpClient
stash@{3}: To delete stash: 233abc813c fix
stash@{4}: WIP on branchn-test-om: 4a42e4 WIP: Commit
stash@{5}: On branch-test-om: projects deleted/modified when rebuilt
stash@{6}: My configurations: Apply my local dev configurations

$git stash drop stash@{2}

$git stash list

stash@{1}: WIP On branch1-om: WIP for #12
stash@{2}: To delete stash: 233abc813c fix
stash@{3}: WIP on branchn-test-om: 4a42e4 WIP: Commit
stash@{4}: On branch-test-om: projects deleted/modified when rebuilt
stash@{5}: My configurations: Apply my local dev configurations

$git stash drop stash@{2}

$git stash list

stash@{1}: WIP On branch1-om: WIP for #12
stash@{2}: WIP on branchn-test-om: 4a42e4 WIP: Commit
stash@{3}: On branch-test-om: projects deleted/modified when rebuilt
stash@{4}: My configurations: Apply my local dev configurations

$git stash drop stash@{3}

$git stash list

stash@{1}: WIP On branch1-om: WIP for #12
stash@{2}: WIP on branchn-test-om: 4a42e4 WIP: Commit
stash@{4}: My configurations: Apply my local dev configurations

问题是从顶部移除藏匿处会重新编号。但是因为您可以在任何位置删除它们,所以您不需要表现得好像 stashes 只有类似堆栈的访问权限。

从最远的地方放下它们,以保持数字完整:

for N in 6 4 3 1; do git stash drop stash@\{$N}; done

不幸的是,git stash drop 似乎不支持多个 stash 作为参数:

% git stash drop stash@{{0},{2}}
Too many revisions specified: 'stash@{0}' 'stash@{2}'

我最好的猜测是:

i=0; for s in 0 2; do git stash drop stash@{$((s-i))}; i=$((i+1)); done

因为 git stash drop 更新了 stash 索引,即使你不查看 stash 列表(这是奇怪的 tbh)

编辑:尽管这仅适用于您希望按升序删除它们的情况,否则 9000 的解决方案要简单得多。