How many / how long are stashes saved by git?

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).


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.

Tags:

Git

Git Stash