Skip to contentSkip to conversation

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

DISCUSSION DeMarcus 5 participants 9 messages Jun 15, 2016

lore
Share & tools
lore

DeMarcus

author
#0 lore raw

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

#1 lore raw

DeMarcus <[email protected]> writes:

[...]

Quoted 5 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.

Quoted 7 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.

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

[...]

author
#2 lore raw
Quoted 8 lines
Quoted 8 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.

Quoted 8 lines
Quoted 4 lines

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.

Quoted 3 lines

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.

#3 lore raw

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

Quoted 19 lines
Quoted 11 lines
Quoted 7 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.

Quoted 3 lines

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

Quoted 8 lines

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

author
#4 lore raw
Quoted 31 lines · Dirk Süsserott

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

Quoted 19 lines
Quoted 11 lines
Quoted 7 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.

Quoted 3 lines

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

Quoted 8 lines

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.

Quoted 7 lines

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.

#5 lore raw
In reply to DeMarcus
Quoted 6 lines · DeMarcus
Quoted 3 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.

author
#6 lore raw
Quoted 5 lines · Holger Hellmuth
Quoted 4 lines · DeMarcus
Quoted 3 lines

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

Quoted 7 lines
Quoted 2 lines

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.

#7 lore raw
Quoted 3 lines · DeMarcus

... 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.

#8 lore raw
In reply to DeMarcus

DeMarcus <[email protected]> writes:

[...]

Quoted 2 lines

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.

[...]