Reputation: 19376
What I already have discoverd:
git stash list
... for listing all my stashes.
git stash show -p stash@{0} --name-only
To list all files within that stash (here the latest stash at position 0).
Now I have a project with hundreds of old stashed changes which will not be needed anymore. I know I could delete them all:
git stash clear
... or delete single stashes like this (deletes the stash with 87 stashes afterwards):
git stash drop stash@{87}
However I would like to delete the stashes 3-107. With a risky guess I tried:
git stash drop stash@{3-107} -- does not work
How can I do this?
Upvotes: 7
Views: 6039
Reputation: 1098
Had issues with the answers here, this got it working for me:
for ((i=3; i<=23; i++)); do
git stash drop stash@{3}
done
Just update 3
with the first index you want to delete, and 23
with the last index.
Upvotes: 1
Reputation: 9270
With bash while you can repeat a command until it fails so if you want to drop all stashed items above and including X
while $(git stash drop stash@{X}); do :; done
when removing one item from stash the others step forward, so repeating will remove all from X to the end
Upvotes: 2
Reputation: 1051
Example Stash List:
You can use git stash list
command to see your 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
Stashes need to keep : From 0 to 10
Stashes need to remove : From 11 to 14
(Using Windows Terminal)
git stash drop stash@{11}
I repeat this command 4 times. (You will be able to use a while loop as well)
1st Time : stash@{11} romoved.
2nd Time : stash@{12} romoved.
3rd Time : stash@{13} romoved.
4th Time : stash@{14} romoved.
Update:
Using for loop (Windows Command Prompt)
for /L %n in (14,-1,11) do git stash drop stash@{%n}
Done :)
Upvotes: 3
Reputation: 30212
You might try this:
i=3; while [ $i -lt 104 ]; do git stash pop stash@{3}; i=$(( $i + 1 )); done
Always drop 3 because when you drop 3 what was 4 is now 3 and so on so you keep on dropping stash@{3}. Either way use with EXTREME care!
Upvotes: 4
Reputation: 4580
Edit: We have to loop backwards because removing a stash changes the index of all stashes.
git stash drop
doesn't accept more than one revision at a time;
$ git stash drop stash@\{{4..1}\}
Too many revisions specified: 'stash@{4}' 'stash@{3}' 'stash@{2}' 'stash@{1}'
You could achieve this with a loop in your shell. For example in 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)
Upvotes: 7