# RE: Git stash certain files

3 messages from 2021-08-31 to 2021-08-31. Participants: Caleb Dougherty, Randall S. Becker.
Thread: https://gitlist.dev/t/56419

## Caleb Dougherty, 2021-08-31 21:17

Subject: RE: Git stash certain files
Message-ID: <BN6PR17MB3185652B92D61670BC92ABA5FECC9@BN6PR17MB3185.namprd17.prod.outlook.com>
URL: https://gitlist.dev/e/BN6PR17MB3185652B92D61670BC92ABA5FECC9%40BN6PR17MB3185.namprd17.prod.outlook.com
In-Reply-To: <BN6PR17MB31854E1EB7F8D358266B2AA8FECC9@BN6PR17MB3185.namprd17.prod.outlook.com>

```
Hi,

Let me know if I should post this somewhere else, but having used git now for a couple years in my work environment, and having come from years of TFS usage, I still find that I want the ability to "shelve" certain file changes for later.

My workflow is to create a personal branch and make changes to different parts of our codebase, and then stage certain files (not all) that are ready, and then commit them.  Sometimes I need to undo certain files in my working directory but keep the changes for later.

Git stash will kind of allow that, but it is messy since it snapshots all my checked out files and I have to do several commands to get the operation of "just stash these few files."

Here is my "shelve" command.  Stage the files you want to shelve and then:
                git commit -m '%1'
                git switch -C shelveset/%username%/!shelvename!
                git switch @{-1}
                git reset --keep HEAD~

Here is my "unshelve" command:
	set branch=shelveset/%username%/%1
	git cherry-pick %branch%
	git reset HEAD~
	git branch -D %branch%

It would be nice if this were built into the stage command as an option (to only stash staged files), or perhaps a new shelve/unshelve set of commands could be added.  The additional niceties of shelve/unshelve is that it is on a branch that can be pushed to a remote (so I don't lose it in a moment of absentmindedness or computer failure) and potentially unshelved by someone else ("Hey Joe, take a look at my code on shelveset xyz").

P.s. I cannot easily use the usual git workflow where you only do X feature change on X feature branch.  I work on multiple features in parallel and cannot be switching branches frequently or I will incur too much overhead (not only running the commands to do the switch).  

Regards,
Caleb


```

## Randall S. Becker, 2021-08-31 22:01

Subject: RE: Git stash certain files
Message-ID: <058f01d79eb3$c5001310$4f003930$@nexbridge.com>
URL: https://gitlist.dev/e/058f01d79eb3%24c5001310%244f003930%24%40nexbridge.com
In-Reply-To: <BN6PR17MB3185652B92D61670BC92ABA5FECC9@BN6PR17MB3185.namprd17.prod.outlook.com>

```
On August 31, 2021 5:18 PM, Caleb Dougherty wrote:
>Let me know if I should post this somewhere else, but having used git now for a couple years in my work environment, and having
come
>from years of TFS usage, I still find that I want the ability to "shelve" certain file changes for later.
>
>My workflow is to create a personal branch and make changes to different parts of our codebase, and then stage certain files (not
all) that
>are ready, and then commit them.  Sometimes I need to undo certain files in my working directory but keep the changes for later.
>
>Git stash will kind of allow that, but it is messy since it snapshots all my checked out files and I have to do several commands to
get the
>operation of "just stash these few files."
>
>Here is my "shelve" command.  Stage the files you want to shelve and then:
>                git commit -m '%1'
>                git switch -C shelveset/%username%/!shelvename!
>                git switch @{-1}
>                git reset --keep HEAD~
>
>Here is my "unshelve" command:
>	set branch=shelveset/%username%/%1
>	git cherry-pick %branch%
>	git reset HEAD~
>	git branch -D %branch%
>
>It would be nice if this were built into the stage command as an option (to only stash staged files), or perhaps a new
shelve/unshelve set of
>commands could be added.  The additional niceties of shelve/unshelve is that it is on a branch that can be pushed to a remote (so I
don't
>lose it in a moment of absentmindedness or computer failure) and potentially unshelved by someone else ("Hey Joe, take a look at my
>code on shelveset xyz").
>
>P.s. I cannot easily use the usual git workflow where you only do X feature change on X feature branch.  I work on multiple
features in
>parallel and cannot be switching branches frequently or I will incur too much overhead (not only running the commands to do the
switch).

Have a look at git stash push -- <pathspec>... I think that might do what you want.
-Randall


```

## Caleb Dougherty, 2021-08-31 22:09

Subject: RE: Git stash certain files
Message-ID: <BN6PR17MB318540A1376CA1FACDE809DFFECC9@BN6PR17MB3185.namprd17.prod.outlook.com>
URL: https://gitlist.dev/e/BN6PR17MB318540A1376CA1FACDE809DFFECC9%40BN6PR17MB3185.namprd17.prod.outlook.com
In-Reply-To: <058f01d79eb3$c5001310$4f003930$@nexbridge.com>

```
Hi Randall,

Thanks for the suggestion.  I'm afraid it doesn't help that much because I can only use it from the command line.  Usually I stage my files using some UI, like SourceTree.  Otherwise it would be a lot of typing and or copying multiple paths.  Think along the lines of, I modified 10 files, and they are distributed across 5 directories with deep paths.

Regards,
Caleb

-----Original Message-----
From: Randall S. Becker <rsbecker@nexbridge.com> 
Sent: Tuesday, August 31, 2021 3:02 PM
To: Caleb Dougherty <caleb_dougherty@keysight.com>; git@vger.kernel.org
Subject: RE: Git stash certain files

CAUTION: This message originates from an external sender.

On August 31, 2021 5:18 PM, Caleb Dougherty wrote:
>Let me know if I should post this somewhere else, but having used git 
>now for a couple years in my work environment, and having
come
>from years of TFS usage, I still find that I want the ability to "shelve" certain file changes for later.
>
>My workflow is to create a personal branch and make changes to 
>different parts of our codebase, and then stage certain files (not
all) that
>are ready, and then commit them.  Sometimes I need to undo certain files in my working directory but keep the changes for later.
>
>Git stash will kind of allow that, but it is messy since it snapshots 
>all my checked out files and I have to do several commands to
get the
>operation of "just stash these few files."
>
>Here is my "shelve" command.  Stage the files you want to shelve and then:
>                git commit -m '%1'
>                git switch -C shelveset/%username%/!shelvename!
>                git switch @{-1}
>                git reset --keep HEAD~
>
>Here is my "unshelve" command:
>       set branch=shelveset/%username%/%1
>       git cherry-pick %branch%
>       git reset HEAD~
>       git branch -D %branch%
>
>It would be nice if this were built into the stage command as an option 
>(to only stash staged files), or perhaps a new
shelve/unshelve set of
>commands could be added.  The additional niceties of shelve/unshelve is 
>that it is on a branch that can be pushed to a remote (so I
don't
>lose it in a moment of absentmindedness or computer failure) and 
>potentially unshelved by someone else ("Hey Joe, take a look at my code on shelveset xyz").
>
>P.s. I cannot easily use the usual git workflow where you only do X 
>feature change on X feature branch.  I work on multiple
features in
>parallel and cannot be switching branches frequently or I will incur 
>too much overhead (not only running the commands to do the
switch).

Have a look at git stash push -- <pathspec>... I think that might do what you want.
-Randall


```
