问题
I want to remove all stash'es, except most recent, from git stash list
.
E.g. I want to remove stash 1 to 3 in a single git command:
stash@{0}: On master: Test related changes
stash@{1}: On master: Tets
stash@{2}: On master: Integrate bunyan logging and http2
stash@{3}: On master: Integrate bunyan logging and http2
I checked this answer https://stackoverflow.com/a/5737041/3878940, but its applicable to delete only a single stash. Is there any git command to delete a range of stash
es?
回答1:
Short answer: no.
Slightly longer answer: no, but it's trivial. You want to drop stashes 1, 2, and 3. When you drop stash #1, stashes 2 and 3 become stashes 1 and 2 respectively. When you drop the new stash #1, stash #2 (which was #3 originally) becomes stash #1. Therefore, to drop three stashes, starting with #1, simply drop stash #1 three times.
回答2:
If you want to delete stash 1 to 3,just go to shell and type the following command:
for n in {1..3}
do
git stash drop stash@{1}
done
Output
Dropped stash@{1} (79f369e9c4ce8348af8bd2da63f384cc7d02655e)
Dropped stash@{1} (744d2fc40e25f2db1bdc182d41f6eb9134957df4)
Dropped stash@{1} (7f9989207a675549866ab1fc7b15082eb4161e9f)
As git stash uses stack structure, each time you drop nth index, stack indexes decreases by 1. So eventually, you end up dropping stashes 1 to 3. So, like this you can also drop a stash of length n just iterating like :
for n in {1..n}
do
git stash drop stash@{1}
done
回答3:
I would add something extra to below provided answer
If you want to log the index of dropped stashes as well
You can use descending order and $n
as shown below
for n in {3..1}; do git stash drop stash@{$n}; done;
The output will be something like..
Dropped stash@{3} (beeaf3629b53d612b1a3fabb7b0c4f77d17e9b71)
Dropped stash@{2} (5a687ca9a93b3190882a9af2c15134e75f063f41)
Dropped stash@{1} (e33dfc42d03166def5dbb60baa975efb21b27f07)
来源:https://stackoverflow.com/questions/41855523/how-to-remove-range-of-git-stash