threads / patch / 20490

patchDocument 'stash clear' recovery via unreachable commits

Subject: [PATCH] Document 'stash clear' recovery via unreachable commits

## tl;dr

4 messages between Aug 9, 2009 and Aug 9, 2009. Diffs are folded; open one to read it.

replies: 3people: 4as markdown or json

Thomas Rast· Aug 9, 2009, 00:47 UTC · lore

Add an example to the stash documentation that shows how to quickly find candidate commits among the 'git fsck --unreachable' output. Unless you have merges of branch names containing WIP, or edit your merge messages to say WIP, there will be no false positives.

Snippet written by Björn "doener" Steinbrink and me after zepolen_ asked on IRC.

Signed-off-by: Thomas Rast <trast@student.ethz.ch>
---
 Documentation/git-stash.txt |   17 ++++++++++++++++-
 1 files changed, 16 insertions(+), 1 deletions(-)
Show changes to Documentation/git-stash.txt +16 −1
diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt
index 1c64a02..2f5ca7b 100644
--- a/Documentation/git-stash.txt
+++ b/Documentation/git-stash.txt
@@ -114,7 +114,8 @@ no conflicts.
 
 clear::
 	Remove all the stashed states. Note that those states will then
-	be subject to pruning, and may be difficult or impossible to recover.
+	be subject to pruning, and may be impossible to recover (see
+	'Examples' below for a possible strategy).
 
 drop [-q|--quiet] [<stash>]::
 
@@ -217,6 +218,20 @@ $ edit/build/test remaining parts
 $ git commit foo -m 'Remaining parts'
 ----------------------------------------------------------------
 
+Recovering stashes that were cleared/dropped erroneously::
+
+If you mistakenly drop or clear stashes, they cannot be recovered
+through the normal safety mechanisms.  However, you can try the
+following incantation to get a list of stashes that are still in your
+repository, but not reachable any more:
++
+----------------------------------------------------------------
+git fsck --unreachable |
+grep commit | cut -d\  -f3 |
+xargs git log --merges --no-walk --grep=WIP
+----------------------------------------------------------------
+
+
 SEE ALSO
 --------
 linkgit:git-checkout[1],
-- 
1.6.4.195.g3a1c2
Junio C Hamano· Aug 9, 2009, 01:25 UTC · re: Thomas Rast · lore

Re: [PATCH] Document 'stash clear' recovery via unreachable commits

Thomas Rast <trast@student.ethz.ch> writes:
> Add an example to the stash documentation that shows how to quickly
> find candidate commits among the 'git fsck --unreachable' output.
Thanks.
> Unless you have merges of branch names containing WIP, or edit your
> merge messages to say WIP, there will be no false positives.

That may be true, but I suspect that people's stash entries that are worth saving are given their own messages with "git stash save 'message'" and do not necessarily say WIP. I wish if there were a better way to identify them, but I do not think of any offhand.

Björn Steinbrink· Aug 9, 2009, 02:21 UTC · re: Junio C Hamano · lore

Re: [PATCH] Document 'stash clear' recovery via unreachable commits

On 2009.08.08 18:25:55 -0700, Junio C Hamano wrote:
Show 14 quoted lines
> Thomas Rast <trast@student.ethz.ch> writes:
> 
> > Add an example to the stash documentation that shows how to quickly
> > find candidate commits among the 'git fsck --unreachable' output.
> 
> Thanks.
> 
> > Unless you have merges of branch names containing WIP, or edit your
> > merge messages to say WIP, there will be no false positives.
> 
> That may be true, but I suspect that people's stash entries that are worth
> saving are given their own messages with "git stash save 'message'" and do
> not necessarily say WIP.  I wish if there were a better way to identify
> them, but I do not think of any offhand.
Here's what I have now:
git fsck --unreachable | grep commit | cut -d\  -f3 |
xargs git log --no-walk --merges \
	--grep='^\(WIP on \|On \)\((no branch)\|[^ ]\+\):'

Should catch stashes with and without user-supplied messages and avoid most false-positives. Drop the "WIP on " option from the grep to capture just those that have a user-supplied message.

To try really really hard to get stash stuff only, one could add some extra stuff to verify that the second parent's commit message is "index on \2:" (\2 being the second group from the above --grep). But that's probably total overkill...

Björn
Sitaram Chamarty· Aug 9, 2009, 09:26 UTC · re: Junio C Hamano · lore

Re: [PATCH] Document 'stash clear' recovery via unreachable commits

2009/8/9 Junio C Hamano <gitster@pobox.com>
>
> Thomas Rast <trast@student.ethz.ch> writes:
Show 8 quoted lines
>
> > Unless you have merges of branch names containing WIP, or edit your
> > merge messages to say WIP, there will be no false positives.
>
> That may be true, but I suspect that people's stash entries that are worth
> saving are given their own messages with "git stash save 'message'" and do
> not necessarily say WIP.  I wish if there were a better way to identify
> them, but I do not think of any offhand.
gitk $(git fsck | grep commit | cut -f3 -d' ') --since='1 week ago'
The --since clause limits the display to stashes lost recently; adjust to taste.

A "stash" has a very recognisable, triangular, shape in the commit DAG -- with gitk you can visually find stashes really fast even if your time limit is too broad or you have too many other kinds of unreachable commits perhaps due to too many rebase etc.

← back to recent threads