threads / discuss / 55628

[feature suggestion] Add 'git stash export', 'git stash import' commands to allow to backup stash externally

Subject: [feature suggestion] Add 'git stash export', 'git stash import' commands to allow to backup stash externally

## tl;dr

16 messages between May 5, 2021 and May 6, 2021.

replies: 15people: 6as markdown or json

Yuri· May 5, 2021, 19:51 UTC · lore

I work in a very large repository, but all of it except for my stashed items can be re-downloaded from the remote URL.

Stash though is much smaller in size and is especially valuable. Its loss would be very expensive.

Could you please consider adding 'git stash export', 'git stash import' commands so that in case of an accidental repository loss stash can be imported from the external backup?

Thanks,
Yuri
Randall S. Becker· May 5, 2021, 20:07 UTC · re: Yuri · lore

RE: [feature suggestion] Add 'git stash export', 'git stash import' commands to allow to backup stash externally

On May 5, 2021 3:52 PM, Yuri wrote:
Show 14 quoted lines
>Subject: [feature suggestion] Add 'git stash export', 'git stash import' commands
>to allow to backup stash externally
>
>I work in a very large repository, but all of it except for my stashed items can be
>re-downloaded from the remote URL.
>
>
>Stash though is much smaller in size and is especially valuable. Its
>loss would be very expensive.
>
>
>Could you please consider adding 'git stash export', 'git stash import'
>commands so that in case of an accidental repository loss stash can be
>imported from the external backup?
I would like to suggest another option: Teach clone and fetch to transfer stash commits. They are filtered out normally. It might be straightforward to preserve the stashed commits. A mirror approach is probably not appropriate since that needs the repo to be bare and Yuri's situation is unlikely to support that. So something like --include-stashes, which would be off by default.
As a side potential defect/bug, if you fetch refs/stash, git is happy to comply and reports the HEAD update and then loses it. git show-refs does not show the stash. What I saw on the fetch was (I only had untracked files in my stash to make things more painful) as follows in 2.29.1:
$ git fetch origin refs/stash
From /path-to-repo
 * branch              refs/stash -> FETCH_HEAD
-Randall
Yuri· May 5, 2021, 20:11 UTC · re: Randall S. Becker · lore

Re: [feature suggestion] Add 'git stash export', 'git stash import' commands to allow to backup stash externally

On 5/5/21 1:07 PM, Randall S. Becker wrote:
> I would like to suggest another option: Teach clone and fetch to transfer stash commits
This would make them public, which is not always desirable.
IMO both options should be implemented.
Yuri
Junio C Hamano· May 6, 2021, 02:22 UTC · re: Randall S. Becker · lore

Re: [feature suggestion] Add 'git stash export', 'git stash import' commands to allow to backup stash externally

"Randall S. Becker" <rsbecker@nexbridge.com> writes:
Show 6 quoted lines
> I would like to suggest another option: Teach clone and fetch to
> transfer stash commits. They are filtered out normally. It might
> be straightforward to preserve the stashed commits. A mirror
> approach is probably not appropriate since that needs the repo to
> be bare and Yuri's situation is unlikely to support that. So
> something like --include-stashes, which would be off by default.

It would unfortunately not work, since stash is not implemented as a set of regular refs. Instead its entries are kept in reflogs. And because the reflog is treated as an entierly local matter, reflog entries are not transfered across fetches and clones.

If stash are redesigned so that each of its entry becomes a separate ref (e.g. refs/stash/0 refs/stash/1 refs/stash/2 ...), then

    git bundle stash.bndl $(git for-each-ref --format='%(refname)' refs/stash/\*)

might become the beginning of a viable implementation of "stash export" (but note that the above does not list any stopping point of the history traversal, so it is just the beginning and does not make a practical solution).

Johannes Sixt· May 6, 2021, 06:07 UTC · re: Yuri · lore

Re: [feature suggestion] Add 'git stash export', 'git stash import' commands to allow to backup stash externally

Am 05.05.21 um 21:51 schrieb Yuri:
Show 6 quoted lines
> I work in a very large repository, but all of it except for my stashed
> items can be re-downloaded from the remote URL.
> 
> 
> Stash though is much smaller in size and is especially valuable. Its
> loss would be very expensive.

I would argue that you are doing something wrong if your stashes are particularly valuable. If you regularly revert temporary, but precious work, then you should commit the work and mark it with a branch tip or tag.

> Could you please consider adding 'git stash export', 'git stash import'
> commands so that in case of an accidental repository loss stash can be
> imported from the external backup?
-- Hannes
Yuri· May 6, 2021, 06:12 UTC · re: Johannes Sixt · lore

Re: [feature suggestion] Add 'git stash export', 'git stash import' commands to allow to backup stash externally

On 5/5/21 11:07 PM, Johannes Sixt wrote:
> I would argue that you are doing something wrong if your stashes are
> particularly valuable. If you regularly revert temporary, but precious
> work, then you should commit the work and mark it with a branch tip or tag.
>

No, it is work in progress. I begin to work on some modification or update, discover some issue, and stash the update until this issue is resolved,

It could take days, weeks, months to resolve them.
Yuri
Johannes Sixt· May 6, 2021, 06:43 UTC · re: Yuri · lore

Re: [feature suggestion] Add 'git stash export', 'git stash import' commands to allow to backup stash externally

Am 06.05.21 um 08:12 schrieb Yuri:
Show 12 quoted lines
> On 5/5/21 11:07 PM, Johannes Sixt wrote:
>> I would argue that you are doing something wrong if your stashes are
>> particularly valuable. If you regularly revert temporary, but precious
>> work, then you should commit the work and mark it with a branch tip or
>> tag.
>>
> 
> No, it is work in progress. I begin to work on some modification or
> update, discover some issue, and stash the update until this issue is
> resolved,
> 
> It could take days, weeks, months to resolve them.

I know. But, as I said, when you turn away from your current work, which is precious, then you should make a commit, in particular, when you can forsee that you might not be able to come back soon.

A 'git stash' is really only intended as a short-term make-the-worktree-temporarily-clean auxiliary storage. It is not intended as a long-term storage. For long-term storage, use branches. "Short term" is measured in seconds (rebase --autostash), minutes, perhaps hours, but certainly not weeks or months.

At least, that's how I understand (and use) it.
-- Hannes
Yuri· May 6, 2021, 06:58 UTC · re: Johannes Sixt · lore

Re: [feature suggestion] Add 'git stash export', 'git stash import' commands to allow to backup stash externally

On 5/5/21 11:43 PM, Johannes Sixt wrote:
> I know. But, as I said, when you turn away from your current work, which
> is precious, then you should make a commit, in particular, when you can
> forsee that you might not be able to come back soon.

How can I commit unfinished work? Commits are public, this would break things.

> For long-term storage, use branches.
Branches are too heavy-weight. They need merging. Sometimes several stash items need to be used together, which makes it inconvenient.
Yuri
Junio C Hamano· May 6, 2021, 08:38 UTC · re: Yuri · lore

Re: [feature suggestion] Add 'git stash export', 'git stash import' commands to allow to backup stash externally

Yuri <yuri@rawbw.com> writes:
Show 8 quoted lines
> On 5/5/21 11:43 PM, Johannes Sixt wrote:
>> I know. But, as I said, when you turn away from your current work, which
>> is precious, then you should make a commit, in particular, when you can
>> forsee that you might not be able to come back soon.
>
>
> How can I commit unfinished work? Commits are public, this would break
> things.

Unlike centralized SCM like SVN and CVS, commits in Git are private until you make them public by pushing, and you do not allow other people to fetch/pull from the repository you actively work in. If your commits become public immediately after you make them, perhaps there is a room in your workflow for vast improvement.

Yuri· May 6, 2021, 08:41 UTC · re: Junio C Hamano · lore

Re: [feature suggestion] Add 'git stash export', 'git stash import' commands to allow to backup stash externally

On 5/6/21 1:38 AM, Junio C Hamano wrote:
Show 5 quoted lines
> Unlike centralized SCM like SVN and CVS, commits in Git are private
> until you make them public by pushing, and you do not allow other
> people to fetch/pull from the repository you actively work in.  If
> your commits become public immediately after you make them, perhaps
> there is a room in your workflow for vast improvement.
Let's say I make commit1, commit2, commit3 and then commit4.
How can I push only commit1 and commit3, but not commit2 and commit4?
Can I permanently hold some commits from being pushed while pushing others?
Yuri
Junio C Hamano· May 6, 2021, 09:09 UTC · re: Yuri · lore

Re: [feature suggestion] Add 'git stash export', 'git stash import' commands to allow to backup stash externally

Yuri <yuri@rawbw.com> writes:
Show 12 quoted lines
> On 5/6/21 1:38 AM, Junio C Hamano wrote:
>> Unlike centralized SCM like SVN and CVS, commits in Git are private
>> until you make them public by pushing, and you do not allow other
>> people to fetch/pull from the repository you actively work in.  If
>> your commits become public immediately after you make them, perhaps
>> there is a room in your workflow for vast improvement.
>
>
> Let's say I make commit1, commit2, commit3 and then commit4.
>
>
> How can I push only commit1 and commit3, but not commit2 and commit4?
You don't.
> Can I permanently hold some commits from being pushed while pushing others?

Yes, you arrange these unrelated things (e.g. if commit1 and commit3 can become ready to be used without commit2 and commit4, there is *no* reason to build commit2 as a direct child of commit1 *AND* build commit3 as a direct child of commit2) into multiple branches, keep unrelated things separate and related things together, and push the branch commit1 & commit3 are on (obviously in your example they become ready to be consumed together while commit2 or commit4 are not, so they wouldn't be on the same commit as commit2 or commit4).

Felipe Contreras· May 6, 2021, 10:23 UTC · re: Yuri · lore

Re: [feature suggestion] Add 'git stash export', 'git stash import' commands to allow to backup stash externally

Yuri wrote:
Show 10 quoted lines
> On 5/6/21 1:38 AM, Junio C Hamano wrote:
> > Unlike centralized SCM like SVN and CVS, commits in Git are private
> > until you make them public by pushing, and you do not allow other
> > people to fetch/pull from the repository you actively work in.  If
> > your commits become public immediately after you make them, perhaps
> > there is a room in your workflow for vast improvement.
> 
> Let's say I make commit1, commit2, commit3 and then commit4.
> 
> How can I push only commit1 and commit3, but not commit2 and commit4?

You create a separate branch "for-upstream" based on the parent of commit 1.

Then you chery-pick commit 1 and commit 3.

You can leave the other branch as-is, and deal with that later, or you can rebaste that temporary branch on top of "for-upstream".

Or you can rebase first, create a new branch from tmp~2, and then push it.

> Can I permanently hold some commits from being pushed while pushing
> others?
Yes.
-- 
Felipe Contreras
Felipe Contreras· May 6, 2021, 08:40 UTC · re: Yuri · lore

Re: [feature suggestion] Add 'git stash export', 'git stash import' commands to allow to backup stash externally

Yuri wrote:
Show 6 quoted lines
> On 5/5/21 11:43 PM, Johannes Sixt wrote:
> > I know. But, as I said, when you turn away from your current work, which
> > is precious, then you should make a commit, in particular, when you can
> > forsee that you might not be able to come back soon.
> 
> How can I commit unfinished work?
Just like that: `git commit`.
That's what you are doing anyway, whether you realize or not.
> Commits are public, this would break things.

No. Commits are most definitely not public. Not unless you do `git push`, and only if you push to a public repository.

I have countless commits on local branches that nobody would every see. Many on a "tmp" branch.

I also have many private repositories, which contain passwords and other sensitive information. Again... Only for my eyes.

> > For long-term storage, use branches.
> 
> Branches are too heavy-weight.
No, each one is a single file consisting of 41 bytes.
A single stash takes about 200 bytes. Considerably more.
Compare:
 * .git/refs/heads/master
 * .git/logs/refs/stash
> They need merging. Sometimes several stash items need to be used
> together, which makes it inconvenient.
You mean like one stash item goes on top of another?
Like... Commits on a branch?

I think this boils down to a confussion of concepts. Stash items are commits. The only real conceptual difference is that you did not specify the message (-m tmp would do that trick).

Again. Check yourself; the commit is there:
 * git cat-file -p stash@{0}
 * git cat-file -p master
Cheers.
-- 
Felipe Contreras
Yuri· May 6, 2021, 08:45 UTC · re: Felipe Contreras · lore

Re: [feature suggestion] Add 'git stash export', 'git stash import' commands to allow to backup stash externally

On 5/6/21 1:40 AM, Felipe Contreras wrote:
Show 5 quoted lines
> No. Commits are most definitely not public. Not unless you do
> `git push`, and only if you push to a public repository.
>
> I have countless commits on local branches that nobody would every see.
> Many on a "tmp" branch.

So you advocate to use branches? If branches are same or better than stash, why was the stash feature created?

Maybe it should be removed in favor of branches?
Yuri
Felipe Contreras· May 6, 2021, 10:45 UTC · re: Yuri · lore

Re: [feature suggestion] Add 'git stash export', 'git stash import' commands to allow to backup stash externally

Yuri wrote:
Show 9 quoted lines
> On 5/6/21 1:40 AM, Felipe Contreras wrote:
> > No. Commits are most definitely not public. Not unless you do
> > `git push`, and only if you push to a public repository.
> >
> > I have countless commits on local branches that nobody would every see.
> > Many on a "tmp" branch.
> 
> So you advocate to use branches? If branches are same or better than 
> stash, why was the stash feature created?
Branches are for long-term sequences of useful commits.
Stashes are for short-term crappy commits.
They serve a different purpose.
-- 
Felipe Contreras
Theodore Ts'o· May 6, 2021, 14:10 UTC · re: Yuri · lore

Re: [feature suggestion] Add 'git stash export', 'git stash import' commands to allow to backup stash externally

On Thu, May 06, 2021 at 01:45:41AM -0700, Yuri wrote:
Show 5 quoted lines
> 
> So you advocate to use branches? If branches are same or better than stash,
> why was the stash feature created?
> 
> Maybe it should be removed in favor of branches?

Stash is actually *implemented* in terms of a branch and reflogs. It's syntactic sugar --- a user interface --- for a simple use case, which is designed for use by users who don't care to learn a richer, more complex set of commands, or who want to use for simple, temporary use cases.

So you can see the top-most stash by running the command:
   git log refs/stash

You can also see that these two commands produce essentially the same output:

   git stash list
   git reflog refs/stash

By the way, reflogs by default expire after 90 days. And if you do something like this:

  git reflog expire --expire=now --all
  git stash list

*Poof* Watch all of your stashed patches disappear. So if you are storing things that you consider especially valuable using git stash, I'd encourage you to rethink your strategy.

For anything that needs to be saved more than a few minutes, I tend to create explicit branches, which can be named, so it's obvious is going on. Those branches aren't pushed out publically by default, any more than the branch refs/stash is pushed out publically. But because they are branches, you can push them out explicitly, perhaps to a private remote repo, if you like.

You can then use git cherry-pick, git rebase, git rebase -i, git commit --amend, etc., to manage your work in progress. This gives much finer-grained control over how you manage your in-progress work, at the cost of needing to learn a more complex set of commands.

Cheers,
					- Ted

P.S. One hint: if the reason why you don't like using explicit branches is because you get confused what branch you are on, it's helpful to put your current branch on shell prompt. I do something like this:

<tytso@cwcc> {/usr/projects/e2fsprogs/e2fsprogs} (next) 1822% git checkout tt/pfsck Switched to branch 'tt/pfsck' <tytso@cwcc> {/usr/projects/e2fsprogs/e2fsprogs} (tt/pfsck) 1823%

One of the conventions I use is that [initials]/* for branch names that are generally never pushed out. "tt" is my own initials, for my own work. Other people's initials are used for when they send me branches to look at, that aren't quite ready to be merged into the master, next, or maint branches. Sometimes I will follow Junio's convention of merging other people's in-progress work that they've sent me into a pu (proposed updates) branch, for other people to test and review, and for integration testing. I tend not to do that often since e2fsprogs' development tends not to be as complex/dynamic as git's.

My shell prompt is accomplished through this in my .bashrc:
    if [ $UID = 0 ]; then
	u="${LOGNAME}.root"
	p="#"
    else
	u="$LOGNAME";
	p="%"
    fi
    if [ $SHLVL != 1 ]; then
	s=", level $SHLVL"
    fi
    h=${HOSTNAME}
    if [ -n "$debian_chroot" ]; then
	h=${debian_chroot}-CHROOT.${h}
    fi
    if test -f /etc/bash_completion ; then
	. /etc/bash_completion
    fi
    if test -f /usr/local/share/bash-completion/bash_completion.sh ; then
	. /usr/local/share/bash-completion/bash_completion.sh
    fi
    if test -f /usr/local/share/git-core/contrib/completion/git-prompt.sh ; then
	. /usr/local/share/git-core/contrib/completion/git-prompt.sh
    fi
    if type __git_ps1 >& /dev/null ; then
	PS1="<${u}@${h}> {\${PWD}}$s  \$(__git_ps1)\n\!$p "
    else
	PS1="<${u}@${h}> {\${PWD}}$s\n\!$p "
    fi
    unset u s h

← back to recent threads