# git-stash

6 messages from 2007-06-07 to 2007-06-08. Participants: Johannes Schindelin, Lars Hjemli, Bill Lear, Matthias Lederhofer.
Thread: https://gitlist.dev/t/8488

## Johannes Schindelin, 2007-06-07 22:28

Subject: git-stash
Message-ID: <Pine.LNX.4.64.0706072328000.4046@racer.site>
URL: https://gitlist.dev/e/Pine.LNX.4.64.0706072328000.4046%40racer.site

```
Hi,

I just was in the need for git-stash (for the 1e6th time this year), but 
instead of writing a script, I though I'd try the "!" convention for 
aliases. Works quite well for me:

git config alias.stash '!git diff-files --name-only -z | git update-index 
-z --stdin && tree=$(git-write-tree) && commit=$(echo stash $(date) | 
git-commit-tree $tree) && git-update-ref refs/heads/stash $commit && 
git-reset --hard'

(This is one long line.)

With this, I can now say "git stash" with a dirty working directory, and 
it will have the same effect as "git reset --hard", i.e. reset to the 
clean state in HEAD.

Except that all my changes are stashed away in the branch "stash". It is 
not really a branch, as I expect it to jump from loose end to loose end, 
like this:

A - B - C - D - E - F - G - H - ...  (master branch)
  \               \       \
    A'  ..          E'  ..  G'       (stash branch)

The ".." denote reflog connections: "git log stash" will show G' and as 
its parent G with its history, while "git reflog stash" will show G', and 
then E', and then A'.

Maybe it is not only useful for me...

Ciao,
Dscho

```

## Lars Hjemli, 2007-06-07 22:52

Subject: Re: git-stash
Message-ID: <8c5c35580706071552v3a099fdci1baec9101a40242b@mail.gmail.com>
URL: https://gitlist.dev/e/8c5c35580706071552v3a099fdci1baec9101a40242b%40mail.gmail.com
In-Reply-To: <Pine.LNX.4.64.0706072328000.4046@racer.site>

```
On 6/8/07, Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
> git-commit-tree $tree

I added -p HEAD here, and it's even nicer ;-)

Thanks!

--
larsh

```

## Johannes Schindelin, 2007-06-07 22:55

Subject: Re: git-stash
Message-ID: <Pine.LNX.4.64.0706072354370.4046@racer.site>
URL: https://gitlist.dev/e/Pine.LNX.4.64.0706072354370.4046%40racer.site
In-Reply-To: <8c5c35580706071552v3a099fdci1baec9101a40242b@mail.gmail.com>

```
Hi,

On Fri, 8 Jun 2007, Lars Hjemli wrote:

> On 6/8/07, Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
> > git-commit-tree $tree
> 
> I added -p HEAD here, and it's even nicer ;-)

Yep, you're right. I forgot that in my "final" version.

Thanks,
Dscho

```

## Lars Hjemli, 2007-06-07 23:38

Subject: Re: git-stash
Message-ID: <8c5c35580706071638m39bccc7xeee6b790c00af993@mail.gmail.com>
URL: https://gitlist.dev/e/8c5c35580706071638m39bccc7xeee6b790c00af993%40mail.gmail.com
In-Reply-To: <Pine.LNX.4.64.0706072328000.4046@racer.site>

```
On 6/8/07, Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
> I just was in the need for git-stash (for the 1e6th time this year), but
> instead of writing a script, I though I'd try the "!" convention for
> aliases.

Fwiw, here's a very simple unstash:

git config alias.unstash '!git diff --binary stash^ stash | git-apply -'

--
larsh

```

## Bill Lear, 2007-06-08 00:26

Subject: Re: git-stash
Message-ID: <18024.41550.399465.780261@lisa.zopyra.com>
URL: https://gitlist.dev/e/18024.41550.399465.780261%40lisa.zopyra.com
In-Reply-To: <Pine.LNX.4.64.0706072328000.4046@racer.site>

```
On Thursday, June 7, 2007 at 23:28:33 (+0100) Johannes Schindelin writes:
>Hi,
>
>I just was in the need for git-stash (for the 1e6th time this year), but 
>instead of writing a script, I though I'd try the "!" convention for 
>aliases. Works quite well for me:

Excellent work.  I do this sort of stuff all the time.  I called my
lame version "shelf", but this works for me ...


Bill

```

## Matthias Lederhofer, 2007-06-08 06:52

Subject: Re: git-stash
Message-ID: <20070608065245.GA27863@moooo.ath.cx>
URL: https://gitlist.dev/e/20070608065245.GA27863%40moooo.ath.cx
In-Reply-To: <Pine.LNX.4.64.0706072328000.4046@racer.site>

```
Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
> I just was in the need for git-stash (for the 1e6th time this year), but 
> instead of writing a script, I though I'd try the "!" convention for 
> aliases. Works quite well for me:

I have scripts saving and restoring the current state of the working
tree in the current branch using two commits: one is the current index
and the other one the index after adding all changes.

% tail -n 1000 save restore
==> save <==
#!/bin/sh
set -e
die() { echo "$0: $*" >&2; exit 1; }

parent=$(git rev-parse HEAD^0 2> /dev/null) || die requires at least one commit
tree=$(git write-tree)
commit=$(echo wip index | git commit-tree $tree -p $parent)

git diff-files --name-only -z | git update-index --remove -z --stdin
tree=$(git write-tree)
commit=$(echo wip working tree | git commit-tree $tree -p $commit)

git update-ref -m 'wip save' HEAD $commit
echo saved wip

==> restore <==
#!/bin/sh
set -e
die() { echo "$0: $*" >&2; exit 1; }

p=$(git rev-parse HEAD~1) || die requires at least 3 commits
pp=$(git rev-parse HEAD~2) || die requires at least 3 commits
git-read-tree --reset $p

git update-ref -m 'wip restore' HEAD $pp
echo restored wip

```
