How many / how long are stashes saved by git?
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:
- How many stashes are saved?
- How long are these stashes saved?
- 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.
2 Solutions collect form web for “How many / how long are stashes saved by git?”
1 – How many stashes are saved?
Stashes don’t appear out of thin air; only if you create them, using
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-untrackedflag) 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>
Other than the garbage-collection mechanism described above, Git will not delete stashes on its own; a stash will remain in your local repository (for at least 90 days) as long as you don’t voluntarily
drop it, using
git drop <stash-reference>
which deletes the specified stash entry from the stash reflog;
pop it, using
git pop <stash-reference>
which applies the specified stash and then deletes the corresponding entry from the stash reflog; or
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.
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).