Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many / how long are stashes saved by git?

Tags:

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.

like image 203
math Avatar asked Oct 04 '14 07:10

math


People also ask

Where are git stashes saved?

All are stored in . git/refs/stash . git stash saves stashes indefinitely, and all of them are listed by git stash list . Please note that dropping or clearing the stash will remove it from the stash list, but you might still have unpruned nodes with the right data lying around.

Are git stashes saved locally?

Note that the stash is local to your Git repository; stashes are not transferred to the server when you push.

Does git stash persist?

Yes, the stash is persisted to disk, and thus survives reboot.

What happens if you git stash multiple times?

If you made two stashes, then just call git stash pop twice. As opposed to git stash apply , pop applies and removes the latest stash. You can also reference a specific stash, e.g.


2 Answers

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.

like image 163
jub0bs Avatar answered Oct 23 '22 07:10

jub0bs


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

like image 26
John Zwinck Avatar answered Oct 23 '22 07:10

John Zwinck