threads / discuss / 57235

feature request: add [-f|--from <rootbranch>] to git stash branch

Subject: feature request: add [-f|--from <rootbranch>] to git stash branch

## tl;dr

3 messages between Jan 13, 2022 and Jan 14, 2022.

replies: 2people: 3as markdown or json

Colin Kennedy· Jan 13, 2022, 16:29 UTC · lore
Current syntax of git stash branch is:
branch <branchname> [<stash>]
Proposed syntax is:
branch <branchname> [-f|--from <rootbranch>] [<stash>]
the from flag specifies a branch from which the new branch should be derived.
Example usage:
git stash branch remove-commented-code --from main
This simplifies a workflow which is common for me. I am working N-commits deep in some feature branch when I discover a low hanging fruit. Dealing with this low hanging fruit is not appropriate for the current feature branch. It's most appropriate to deal with "directly" on a fresh branch from main. Because I am untidy, I often have a bit of mess in between my staging area, and unstaged changes, and this creates friction when I attempt to stash this low hanging fruit, pop back to main, create the new branch, pop the stashed low hanging fruit, etc etc.
The proposed flag gives a one-liner to apply stashed patches wherever, no matter your current location, which I think is an improvement.
Happy to have feedback on the flag. Happy for someone else to implement it. Also happy to consider taking a crack at it myself if it seems like something people would be happy to include. (NB: I haven't written or even read any c, or c++ for that matter, in probably 15 years. So, you know).

thanks, Colin

Philip Oakley· Jan 14, 2022, 14:53 UTC · re: Colin Kennedy · lore

Re: feature request: add [-f|--from <rootbranch>] to git stash branch

Hi, some minor comments, in-line.
On 13/01/2022 16:29, Colin Kennedy wrote:
Show 7 quoted lines
> Current syntax of git stash branch is:
>
> branch <branchname> [<stash>]
>
> Proposed syntax is:
>
> branch <branchname> [-f|--from <rootbranch>] [<stash>]

Just to say that the `-f` is typically reserved/used for the --force option if available.

Show 8 quoted lines
>
> the from flag specifies a branch from which the new branch should be derived.
>
> Example usage:
>
> git stash branch remove-commented-code --from main
>
> This simplifies a workflow which is common for me. I am working N-commits deep in some feature branch when I discover a low hanging fruit. Dealing with this low hanging fruit is not appropriate for the current feature branch. It's most appropriate to deal with "directly" on a fresh branch from main. Because I am untidy, 

Maybe an example that shows the level of untidiness might help to clarify the suggestion, as to what is included/not included, and how it makes it easy later when the stash is recovered.

Show 8 quoted lines
> I often have a bit of mess in between my staging area, and unstaged changes, and this creates friction when I attempt to stash this low hanging fruit, pop back to main, create the new branch, pop the stashed low hanging fruit, etc etc.
>
> The proposed flag gives a one-liner to apply stashed patches wherever, no matter your current location, which I think is an improvement.
>
> Happy to have feedback on the flag. Happy for someone else to implement it. Also happy to consider taking a crack at it myself if it seems like something people would be happy to include. (NB: I haven't written or even read any c, or c++ for that matter, in probably 15 years. So, you know).
>
> thanks,
> Colin
Junio C Hamano· Jan 14, 2022, 19:02 UTC · re: Philip Oakley · lore

Re: feature request: add [-f|--from <rootbranch>] to git stash branch

Philip Oakley <philipoakley@iee.email> writes:
Show 13 quoted lines
> Hi, some minor comments, in-line.
>
> On 13/01/2022 16:29, Colin Kennedy wrote:
>> Current syntax of git stash branch is:
>>
>> branch <branchname> [<stash>]
>>
>> Proposed syntax is:
>>
>> branch <branchname> [-f|--from <rootbranch>] [<stash>]
>
> Just to say that the `-f` is typically reserved/used for the --force
> option if available.
True.

Also, a dashed option and its argument should come before the non-dashed arguments, so if it were a good idea to introduce a new option, it should come after "git stash branch" and "<branchname>", not after "<branchname>".

Most importantly, this particular new option is unnecessary and is probably unwanted.

The original "git stash branch" relieves the user from finding out the commit where the stash was taken from, which is its value over its long-hand equivalent, i.e.

	git checkout -b <name> refs/stash^ &&
	git stash pop

The long-hand version forces the user to know that the first parent of the commit that represents a stash entry is the original commit the stash entry was taken from. With "git stash branch <name>", the user does not need to know it.

But the --from discards the only reason why "stash branch" subcommand needs to exist. It would be equivalent to

	git checkout -b <name> <rootbranch> &&
	git stash pop

and it is not helping the user, unlike the original that allowed the user to be oblivious to the internal implementation detail of a stash entry.

Or am I failing to see some obvious improvements over the longhand?

Worse, the application and discarding of the stash entry done by the "git stash branch" is guaranteed to apply cleanly, as we'd be applying the stash to its exact original state. The trees of <rootbranch> and refs/stash^, on the other hand, can be vastly different, and applying the stash on top of <rootbranch> commit can fail.

In short, I am not enthused.
Thanks.

← back to recent threads