threads / discuss / 29201

How can I do an automatic stash when doing a checkout?

Subject: How can I do an automatic stash when doing a checkout?

## tl;dr

9 messages between Dec 18, 2011 and Dec 19, 2011.

replies: 8people: 5as markdown or json

DeMarcus· Dec 18, 2011, 11:19 UTC · lore
Hi,

I'm new to git and currently I'm working with SVN and there it's a bit messier doing branching but once I've branched then all my development there is completely separated from any other branch. When I want to work on another branch I just change directory.

This is not how it works with git, where when I want to change branch I have to do a git checkout. However, that leaves all the modified and untracked files in the directory of the branch I switched to. This is seldom the behavior I want.

With the git stash command I can clean the directory the way I want but the stash command is not connected to a particular branch.

Is there a way to have git checkout do an automatic stash when doing a checkout to another branch, and then do an automatic git stash apply with the correct stash when changing back to the previous branch again?

Thanks, Daniel

Bruce Stephens brs· Dec 18, 2011, 11:45 UTC · re: DeMarcus · lore

Re: How can I do an automatic stash when doing a checkout?

DeMarcus <demarcus@hotmail.com> writes:
[...]
Show 12 quoted lines
> This is not how it works with git, where when I want to change branch
> I have to do a git checkout. However, that leaves all the modified and
> untracked files in the directory of the branch I switched to. This is
> seldom the behavior I want.
>
> With the git stash command I can clean the directory the way I want
> but the stash command is not connected to a particular branch.
>
> Is there a way to have git checkout do an automatic stash when doing a
> checkout to another branch, and then do an automatic git stash apply
> with the correct stash when changing back to the previous branch
> again?

You probably don't want to use stash. Just commit whatever partial work you've done.

You could also just checkout different branches in different directories. Nothing wrong with doing that in git.

[...]
DeMarcus· Dec 18, 2011, 15:10 UTC · re: Bruce Stephens brs · lore

Re: How can I do an automatic stash when doing a checkout?

Show 16 quoted lines
>> This is not how it works with git, where when I want to change branch
>> I have to do a git checkout. However, that leaves all the modified and
>> untracked files in the directory of the branch I switched to. This is
>> seldom the behavior I want.
>>
>> With the git stash command I can clean the directory the way I want
>> but the stash command is not connected to a particular branch.
>>
>> Is there a way to have git checkout do an automatic stash when doing a
>> checkout to another branch, and then do an automatic git stash apply
>> with the correct stash when changing back to the previous branch
>> again?
>
> You probably don't want to use stash. Just commit whatever partial work
> you've done.
>

It feels strange doing a commit of partial work. Some of the files may not even be supposed to be checked in.

> You could also just checkout different branches in different
> directories. Nothing wrong with doing that in git.
>
Ok thanks, that would give me the same behavior as I have today.

However, I can see some benefits with have everything in the same directory as git allows compared to other VCSs. And since the stashing feature is already there in git, it would be nice if the git checkout with some flag could use stashing automatically.

Bruce Stephens brs· Dec 18, 2011, 15:37 UTC · re: DeMarcus · lore

Re: How can I do an automatic stash when doing a checkout?

DeMarcus <demarcus@hotmail.com> writes:
[...]
> It feels strange doing a commit of partial work. Some of the files may
> not even be supposed to be checked in.

Well, maybe forget about files that you don't expect to keep, but committing partial work is fine. It's only in your local checkout, after all; once you've got something worth sharing you can rearrange the commits so they make sense.

[...]
Dirk Süsserott· Dec 19, 2011, 16:18 UTC · re: DeMarcus · lore

Re: How can I do an automatic stash when doing a checkout?

Am 18.12.2011 16:10 schrieb DeMarcus: [...]

Show 27 quoted lines
>>> With the git stash command I can clean the directory the way I want
>>> but the stash command is not connected to a particular branch.
>>>
>>> Is there a way to have git checkout do an automatic stash when doing a
>>> checkout to another branch, and then do an automatic git stash apply
>>> with the correct stash when changing back to the previous branch
>>> again?
>>
>> You probably don't want to use stash. Just commit whatever partial work
>> you've done.
>>
> 
> It feels strange doing a commit of partial work. Some of the files may
> not even be supposed to be checked in.
> 
>> You could also just checkout different branches in different
>> directories. Nothing wrong with doing that in git.
>>
> 
> Ok thanks, that would give me the same behavior as I have today.
> 
> However, I can see some benefits with have everything in the same
> directory as git allows compared to other VCSs. And since the stashing
> feature is already there in git, it would be nice if the git checkout
> with some flag could use stashing automatically.
> 
> 
DeMarcus,

probably a post-checkout hook could help you with autostashing, but that would need some scripting. Have a look at "git hooks --help". I sometimes use such a hook to auto-update submodules when checking out a branch. To be fair: I don't know how to identify the "right" stash then.

And also have a look at the script "git-new-workdir". It comes with git but in some contrib directory or so. It's not in the $PATH by default. It allows different working dirs for different branches; some of my co-workers use it and like it. It won't work with Windows, I guess, because it makes use of symlinks.

    Dirk
DeMarcus· Dec 19, 2011, 21:43 UTC · re: Dirk Süsserott · lore

Re: How can I do an automatic stash when doing a checkout?

On 2011-12-19 17:18, Dirk Süsserott wrote:
Show 38 quoted lines
> Am 18.12.2011 16:10 schrieb DeMarcus:
> [...]
>
>>>> With the git stash command I can clean the directory the way I want
>>>> but the stash command is not connected to a particular branch.
>>>>
>>>> Is there a way to have git checkout do an automatic stash when doing a
>>>> checkout to another branch, and then do an automatic git stash apply
>>>> with the correct stash when changing back to the previous branch
>>>> again?
>>>
>>> You probably don't want to use stash. Just commit whatever partial work
>>> you've done.
>>>
>>
>> It feels strange doing a commit of partial work. Some of the files may
>> not even be supposed to be checked in.
>>
>>> You could also just checkout different branches in different
>>> directories. Nothing wrong with doing that in git.
>>>
>>
>> Ok thanks, that would give me the same behavior as I have today.
>>
>> However, I can see some benefits with have everything in the same
>> directory as git allows compared to other VCSs. And since the stashing
>> feature is already there in git, it would be nice if the git checkout
>> with some flag could use stashing automatically.
>>
>>
>
> DeMarcus,
>
> probably a post-checkout hook could help you with autostashing, but that
> would need some scripting. Have a look at "git hooks --help".
> I sometimes use such a hook to auto-update submodules when checking out
> a branch. To be fair: I don't know how to identify the "right" stash then.
>

Thanks! I'll check that out. Maybe the right stash could be checked-in itself somehow.

Holger Hellmuth· Dec 19, 2011, 17:24 UTC · re: DeMarcus · lore

Re: How can I do an automatic stash when doing a checkout?

On 18.12.2011 16:10, DeMarcus wrote:
Show 6 quoted lines
>> You probably don't want to use stash. Just commit whatever partial work
>> you've done.
>>
>
> It feels strange doing a commit of partial work. Some of the files may
> not even be supposed to be checked in.

You have heard of "git commit --amend" ? Makes partial commits really easy to work with.

DeMarcus· Dec 19, 2011, 21:46 UTC · re: Holger Hellmuth · lore

Re: How can I do an automatic stash when doing a checkout?

On 2011-12-19 18:24, Holger Hellmuth wrote:
Show 12 quoted lines
> On 18.12.2011 16:10, DeMarcus wrote:
>>> You probably don't want to use stash. Just commit whatever partial work
>>> you've done.
>>>
>>
>> It feels strange doing a commit of partial work. Some of the files may
>> not even be supposed to be checked in.
>
> You have heard of "git commit --amend" ? Makes partial commits really
> easy to work with.
>
>

Yes, I know that one. It's just that I know myself, I don't really like to go back rearranging. I prefer to keep things spread out at first and then inserted properly when it's time. But maybe I'm just unused to the git workflow.

Junio C Hamano· Dec 19, 2011, 22:24 UTC · re: DeMarcus · lore

Re: How can I do an automatic stash when doing a checkout?

DeMarcus <demarcus@hotmail.com> writes:
> ... I don't really like to go back rearranging. I prefer to keep things
> spread out at first and then inserted properly when it's time. But maybe
> I'm just unused to the git workflow.
That does not have to do much with "the" git workflow.

If you are a kind of person who likes to work on N things concurrently without ever making and recording mistakes but would rather keep unfinished work without committing until done, you would need to have them in your working trees. By definition you would need to use N working trees, each of which permanently checks out its own branch, instead of a single working tree where you checkout one of N branches at a time to work on.

That is also a perfectly fine git workflow that we support with the contributed script git-new-workdir.

← back to recent threads