-1

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
  • The stash@{1} is older than stash@{1} by looking id you can tell and the question says pull in the oldest stash, so how stash@{1} can be the old. @knittl Commented May 9, 2023 at 15:35
  • Just an advise: You can live without stashing for most use cases. Instead, even if it's just something small, create a branch. You can cherry-pick commits between branches and rebase them when the main branch evolves. Commented May 9, 2023 at 15:37

2 Answers 2

2

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

1
  • Okay was not aware that start@{0} will be most recent, I was thinking, {0} is the first and new will be {1}. Thanks for clearing that up. @Knittl Commented May 11, 2023 at 13:08
-1

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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.