Pull in the oldest stash

99 Views Asked by At

Using following stash list how you pull in the oldest stash while ensuring that you maintain it in the stash.

The list is :

stash@{0}: WIP on feature/feature1: 2f78364 New text
stash@{1}: WIP on feature/feature1: 2f78364 New text 

Options are:

  1. git stash apply stash@{0}
  2. git stash apply stash@{1}
  3. git stash pop stash@{0}
  4. git stash pop stash@{1}

I answered as 1, but the correct answer is 2. By framing the question as oldest stash, I thought the first entry which is stash@{0} is the oldest entry which is apparently not. Correct answer is 2.

How?

2

There are 2 best solutions below

1
knittl On

stash@{0} is the most recent (newest) stash, stash@{1} is "one older", stash@{2} is even older. The oldest stash is the stash with the highest number.

That's how Git defines it, there is no "how"; Quote from the docs

stash@{0} is the most recently created stash, stash@{1} is the one before it, stash@{2.hours.ago} is also possible

0
Guildenstern On

This seems to work.

#!/usr/bin/env bash
off_by_one=$(wc -l <$(git rev-parse --show-toplevel)/.git/logs/refs/stash)
oldest=$(($off_by_one-1))
git stash apply stash@{$oldest}