Elijah Lynn
Elijah Lynn

Reputation: 13528

Is there a shortcut format for specifying a git <stash>? e.g. stash@{3}

Is there a shortcut format for specifying a git <stash>? e.g. stash@{3}

Like:

git apply @3

Instead of:

git apply stash@{3}

I did not see anything in the man file:

When no <stash> is given, stash@{0} is assumed, otherwise <stash> must be a reference of the form stash@{<revision>}.

Upvotes: 0

Views: 84

Answers (2)

torek
torek

Reputation: 489688

In fact, the man page lies a bit: you can give any "sufficiently stash-like" specifier. Actual stash refs, like stash and stash@{3}, are always "stash-like" by definition. However, any object name-able by $REV where:

  • $REV^2 exists
  • $REV, $REV^1, $REV:, $REV^1:, and $REV^2: are all parse-able

is considered to be a stash. The colon suffix turns a commit ID into a tree ID (making sure it exists), and:

  • the stash itself is whatever ID $REV parses-out to
  • the work-tree commit is $REV
  • the "base" commit is $REV^1
  • the work-tree tree is $REV:
  • the base tree is $REV^1:
  • the index-tree is $REV^2:

If $REV^3 exists, it is the untracked/ignored files commit and its tree is $REV^3:.

What this means is that git stash will believe that any "real" merge commit is a stash. (But applying them as stashes is weird at best. :-) )

If you want to be able to name a saved stash later by a short name, you can give it another name, e.g., a tag-name:

git tag foo stash@{3}

Note that this copies the value of stash@{3}, not the name: if you next push another stash, the commit (now referenced by a tag) will match stash@{4}. You can see this by using git rev-parse:

git rev-parse foo; git rev-parse stash@{3}

will print the same giant SHA-1 value twice before you stash something new, and then:

git rev-parse foo; git rev-parse stash@{4}

will do the same after you stash something else, pushing another stash on the "stash stack".

You can create names outside the branch and tag space (or even outside refs/ entirely) with git update-ref, and they work:

git update-ref refs/jinkies/scooby stash
git stash show jinkies/scooby

but I don't recommend doing this "manually", it's too easy to goof up here. Using tags (and naming them such that you can remember what they were for, should you come across a leftover stash tag weeks or months after making a bunch in a coding frenzy) is probably more sensible.

Upvotes: 2

Roberto Bonvallet
Roberto Bonvallet

Reputation: 33379

Actually stash@{n} is the shortcut for a commit object. In a repo of mine, I did this:

git show stash@{1}

amd I got this:

commit 24774ab81ad6c8f0b071363f62bf438572a2b286
Merge: 1c47ddf 31ae746
Author: Roberto Bonvallet <roberto@...>
Date:   Fri Oct 18 13:15:44 2013 -0300

So, in this case, stash@{0} is a shortcut for 24774ab81ad6c8f0b071363f62bf438572a2b286.

Upvotes: 0

Related Questions