git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
DSDirk Süsserott <newsletter@dirk.my1.cc>
Date
Dec 19, 2011, 16:18 UTC
Message-ID
<4EEF63CA.4030201@dirk.my1.cc>
In-Reply-To
<jckvpk$i8v$1@dough.gmane.org>

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
Previous: Bruce Stephens brsNext: DeMarcus
Message 5 of 9 in “How can I do an automatic stash when doing a checkout?”
  1. DeMarcusDec 18, 2011
  2. Bruce Stephens brsDec 18, 2011
  3. DeMarcusDec 18, 2011
  4. Bruce Stephens brsDec 18, 2011
  5. Dirk SüsserottDec 19, 2011
  6. DeMarcusDec 19, 2011
  7. Holger HellmuthDec 19, 2011
  8. DeMarcusDec 19, 2011
  9. Junio C HamanoDec 19, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.