26

I'm very new to git and have some question about stashing. If I've worked on a branch but was not able to get to a position where I can commit the branch, stashing is the right thing to use. My questions regarding stashing are:

  1. How many stashes are saved?
  2. How long are these stashes saved?
  3. Do they just temporarily save the work such that the changes are lost when you reboot your computer?

If someone could quickly help clarifying these would be really appreciated.

Alexis King
  • 43,109
  • 15
  • 131
  • 205
math
  • 1,868
  • 4
  • 26
  • 60
  • 3
    1. As many as you create. 2. As long as you don't *drop*, *pop*, or *clear* them. 3. No; stashes will still be accessible after a reboot. See http://git-scm.com/book/en/Git-Tools-Stashing – jub0bs Oct 04 '14 at 07:58
  • 2
    Downvoter, the question may be more subtle than you think... – jub0bs Oct 05 '14 at 13:32
  • 1
    In git you are almost never in a position where you can't commit. I don't personally find stash to be all that useful. – Andrew C Oct 05 '14 at 14:56

2 Answers2

38

1 - How many stashes are saved?

Stashes don't appear out of thin air; only if you create them, using

git stash

or, equivalently,

git stash save

So how many are saved? As many as you create.

2 - How long are these stashes saved?

This question looks innocent, but the answer is actually quite subtle. There are two aspects to consider here: 1) the stash reflog, and 2) the repository's object database.

When you create a stash, Git

  • adds an entry to the stash reflog,
  • creates two (three if you use the --include-untracked flag) commit objects in the repository's database: one that corresponds to the WIP (work in progress) in your working tree, and another that corresponds to the state of your staging area (a.k.a. index).

Edit: those commit objects are bona fide commits, as can be verified by running git cat-file -t on them. They just happen to not be reachable from any branch; see torek's comment.

By default, Git's garbage collection will automatically delete reflog entries that are older than 90 days; you can specify a different "lifetime" for stash reflog entries, by running

git config gc.refs/stash.reflogexpire <lifetime>

However, as torek notes, Git handles stashes specially – even though old reflog entries are normally deleted automatically, Git will never delete reflog entries for refs/stash unless you explicitly tell it to.

In other words, Git will not delete stashes on its own; a stash will remain in your local repository as long as you don't voluntarily

  • drop it, using

    git stash drop <stash-reference>
    

    which deletes the specified stash entry from the stash reflog;

  • pop it, using

    git stash pop <stash-reference>
    

    which applies the specified stash and then deletes the corresponding entry from the stash reflog; or

  • run

    git stash clear
    

which deletes all entries of the stash reflog (be careful with that one).

However, be aware that those three actions only affect the stash reflog. In particular, they do not immediately cause the associated "WIP" and "index" objects to be deleted from your repository's database; they merely make those objects unreachable. The latter will remain in "repository limbo" for a while, until they eventually get garbage-collected and die a "true death".

That's a useful thing to know: if you accidentally drop a stash, you might still be able to retrieve it from the entrails of your repo, if you can remember or identify the SHAs of its two objects (WIP and index).

3 - Do they just temporarily save the work such that the changes are lost when you reboot your computer?

No. Stashes are no different from any other commit objects; a reboot has no effect on them.

eeowaa
  • 431
  • 3
  • 15
jub0bs
  • 60,866
  • 25
  • 183
  • 186
  • 5
    Side note: they're not just "commit-like objects", they are in fact commits: they're just commits that are on *no* branch. I'd say "creates two commits" rather than "creates two commit-like objects". And, the `stash` ref has a special unusual default expiry time of "never". – torek Apr 06 '15 at 19:53
  • 2
    @torek I had to double check with `git cat-file -t` but you're right about those objects being commits. However, where in the doc is it mentioned that the `stash` ref doesn't expire? – jub0bs Apr 06 '15 at 21:17
  • 10
    It's documented poorly-at-best, perhaps not at all other than in the release notes. In `builtin/reflog.c` is this bit, though: `if (!strcmp(ref, "refs/stash")) {` with code to set the expiry to "never", and the comment: "If unconfigured, make stash never expire". It's not entirely clear to me what the additional code (not shown here) inside that `if` is testing, though, before it sets the expiry to "never". – torek Apr 06 '15 at 21:53
  • 4
    @torek That's a great find. Here's a [link](https://github.com/git/git/blob/6a269e52a5d0c82567c5b39cc194cc90f9fa9156/builtin/reflog.c#L497-L506) to the actual lines of code you're referring to on Github. – nonsensickle Apr 05 '16 at 07:10
  • 1
    @torek The other lines are checking whether the user explicitly passed the following flags: `[--expire= – DylanYoung Sep 14 '18 at 14:05
  • I know this is an old question, but what about unreachable stashes, will they be deleted if I run `git gc`? For example: I stash on a branch A, I switch branches, and I delete branch A → the stash may now be unreachable. Will it still be kept in tact after a `git gc --agressive`? – D. Kovács Mar 02 '21 at 15:31
3

Git stashes are saved until your hard disk dies (unlike commits, which are usually transmitted to some other computer via git push so will outlive a hard disk failure).

You can have as many stashes as you want. Get rid of old ones when you feel like it by running git stash drop or git stash clear (read the docs for those).

John Zwinck
  • 239,568
  • 38
  • 324
  • 436