git stash drop:如何在不删除最新 X 的情况下删除旧的隐藏状态?

git stash drop: How can I delete older stashed states without dropping the latest X?

我已经发现:

git stash list

...列出我所有的藏品。

git stash show -p stash@{0} --name-only

列出该存储中的所有文件(此处为位置 0 的最新存储)。

现在我有一个项目,其中包含数百个不再需要的旧隐藏更改。我知道我可以将它们全部删除:

git stash clear

...或像这样删除单个存储(之后删除具有 87 个存储的存储):

git stash drop stash@{87}

但是我想删除藏品 3-107。我尝试了一个冒险的猜测:

git stash drop stash@{3-107} -- does not work

我该怎么做?

编辑:我们必须向后循环,因为删除存储会更改所有存储的索引。

git stash drop一次不接受超过一个修订;

$ git stash drop stash@\{{4..1}\}
Too many revisions specified: 'stash@{4}' 'stash@{3}' 'stash@{2}' 'stash@{1}'

您可以通过 shell 中的循环实现此目的。例如 bash;

$ for i in {4..1}; do
>     git stash drop stash@{$i};
> done
Dropped stash@{4} (175f810a53b06da05752b5f08d0b6550ca10dc55)
Dropped stash@{3} (3526a0929dac4e9042f7abd806846b5d527b0f2a)
Dropped stash@{2} (44357bb60f406d29a5d39ea0b5586578223953ac)
Dropped stash@{1} (c97f46ecab45846cc2c6138d7ca05348293344ce)

你可以试试这个:

i=3; while [ $i -lt 104 ]; do git stash pop stash@{3}; i=$(( $i + 1 )); done

始终丢弃 3,因为当您丢弃 3 时,原来的 4 现在是 3,依此类推,您会继续丢弃 stash@{3}。无论哪种方式请格外小心使用!

示例存储列表:

您可以使用 git stash list 命令查看您的隐藏列表。

stash@{0}: On main: cell click away deselet bug fix
stash@{1}: On main: stop propagation added
stash@{2}: On main: Split layout new plan button cover from drawing fixed
stash@{3}: On main: free trial changes
stash@{4}: On main: auto start free trial
stash@{5}: On main: fixed stage size with zoom
stash@{6}: On main: stage resize
stash@{7}: On main: resize half done
stash@{8}: On main: resize events removed
stash@{9}: On new-navigation-menu: project changes
stash@{10}: On main: fixed height drawing stage
stash@{11}: On main: plan image size change fail
stash@{12}: On main: plan configure model changes done
stash@{13}: On main: after merge plan sheet model
stash@{14}: On main: enter key scale model bug fixed

藏品需要保留:从 0 到 10

藏品需要移除:从 11 到 14

(使用 Windows 终端)

git stash drop stash@{11}

我重复这个命令4次。 (您也可以使用 while 循环)

1st Time : stash@{11} romoved.

2nd Time : stash@{12} romoved.

3rd Time : stash@{13} romoved.

4th Time : stash@{14} romoved.

更新:

使用 for 循环(Windows 命令提示符)

for /L %n in (14,-1,11) do git stash drop stash@{%n}

完成:)

使用 bash while 你可以重复一个命令直到它失败 所以如果你想删除上面所有隐藏的项目,包括 X

while $(git stash drop stash@{X}); do :; done

当从 stash 中移除一项时,其他项向前移动,因此重复将从 X 到最后移除所有项