Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

git stash - 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:

  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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...