sourcegit icon indicating copy to clipboard operation
sourcegit copied to clipboard

Add Discard command for Staged file(s) as well

Open goran-w opened this issue 10 months ago • 15 comments

Sometimes I have staged changes, perhaps as a result of some command, which I then decide should be completely undone (i.e discarded). I then find it cumbersome that I have to first Unstage them and only then I'm able to actually Discard them.

This can be rather frustrating, since I then have to re-find the changed file(s) among potentially a long list of other Unstaged changes. Worst case, I may have carefully mult-selected a scattered bunch of Staged files, only to have to re-multi-select this scattered bunch again (now among the Unstaged files).

I therefore would suggest implementing a Discard command for Staged file(s) as well. It would be very convenient, more consistent, and the same confirmation dialog would apply.

goran-w avatar Jan 28 '26 14:01 goran-w

Since SourceGit does not provide a function like GitHub Desktop that moves changed files to the recycle bin first and then discards local changes, once a user discards some uncommitted changes, they cannot be recovered. Therefore, my suggestion is not to perform the Unstage + Discard operation on each file separately. Instead, when we need to discard a part of the changes from the staged changes:

  1. Unstage all unnecessary change blocks at once
  2. After confirming that the changes in STAGED are correct, commit them
  3. After the commit is completed, decide whether to discard the previously unstaged changes all at once (using the Discard All operation) or keep them for the next commit

love-linger avatar Feb 08 '26 03:02 love-linger

As I described, that workflow could sometimes become rather tedious, since multi-selection is lost when doing Unstage. I think it SHOULD be OK to have this command there, since:

  • It's not really any big difference from what can already be done for Unstaged changes!
  • There would be the same warning/confirmation popup, before performing the destructive action.
  • It would be more consistent to be able to use the Discard command on Unstaged and Staged changes alike.

(This inconsistency of NOT finding the Discard command in the Staged context-menu, when I was intently looking for it, has "stumped" me multiple times already - especially since some Git commands will automatically stage changes, which may have been temporary or supposed to be discarded later...)

goran-w avatar Feb 09 '26 07:02 goran-w

I don't think this is a consistency issue, because the status of these changes has already changed. You can intuitively see that the context menus for unstaged changes and staged changes are completely different.

that workflow could sometimes become rather tedious, since multi-selection is lost when doing Unstage

I don't understand. If you regard the Unstage context-menu entry as a Discard operation, then the only difference from the real discarding function is that the changes are not removed from the worktree, but they no longer affect your commits. Isn't it better to decide whether you need these changes after you finish committing?

I think this is also the reason why other software like VSCode and Fork do not provide a direct discard function for staged changes.

VSCode

Image

Fork

Image

love-linger avatar Feb 09 '26 08:02 love-linger

The main issue is this:

  • If I've already multi-selected a whole bunch of scattered items (under Staged), because I've decided that they should all be discarded, then after doing Unstage I would have to REPEAT my previous (carefully done) multi-selection a second time (this time under Unstaged, possibly scattered differently among other unstaged items) in order to actually perform the intended Discard operation.

I don't really see why I shouldn't be allowed to perform this as a single action instead (with the usual warning- and confirmation-popup applied).

goran-w avatar Feb 09 '26 12:02 goran-w

There is no prompt when you are disarding a hunk in a file...

love-linger avatar Feb 10 '26 13:02 love-linger

There is no prompt when you are disarding a hunk in a file...

Oh - well, I'm NOT (at all) asking for an option to Discard hunks in a Staged diff, but ONLY to have the action Discard in the context menu for a Staged file! I.e, for Staged files I want similar options as for Unstaged files:

  • For Unstaged items : Stage - Discard - Stash
  • For Staged items : Unstage - Discard - Stash
Image Image

The new Discard action for Staged files should then reuse the same warning popup as when discarding Unstaged files:

Image

goran-w avatar Feb 11 '26 08:02 goran-w

Ok, I can imagine that if we allow directly discarding staged files, someone will create a new feature request that, for consistency, we should also allow discarding part of the changes (hunks) in selected staged file.

love-linger avatar Feb 11 '26 08:02 love-linger

If so, I could help argue against that feature request! I don't see any reason to add that.

To me, it ONLY makes sense to Discard whole files which have become Staged (perhaps automatically or unintentionally, for example by applying a large Stash).

goran-w avatar Feb 11 '26 08:02 goran-w

There are other reasons why we do not allow discarding staged files

  • If a file is partially staged, it's hard to only discard the staged part. Although SourceTree and GitExtension support directly discarding staged changes, in this case, all changes to the entire file will be discarded (including unstaged and staged ones). If users do not understand this clearly, it is easy for them to make incorrect operations. Image

  • When Amend is enabled, it's hard to check which file (and which parts in it) can be dicarded in STAGED area.

love-linger avatar Feb 11 '26 08:02 love-linger

  • If a file is partially staged, it's hard to only discard the staged part.
  • When Amend is enabled, it's hard to check which file (and which parts in it) can be dicarded in STAGED area.

Oh, I see! Thank you for explaining, that makes sense, and I agree that for these specific cases the feature would be risky.

However, given these complications - couldn't we still implement a (staged-item) Discard context-command, but one which is ONLY enabled (and perhaps ONLY visible) for Staged items where the following 2 conditions hold true?

  • The item has no Unstaged counterpart
  • The Amend checkbox is not currently enabled

SourceTree and GitExtension support directly discarding staged changes

💥 Oh, they do? That's interesting! Also makes my feature request not so far-fetched, then...😉😅

goran-w avatar Feb 11 '26 13:02 goran-w

If a file is partially staged, it's hard to only discard the staged part.

GitExtensions just shows an error message on any conflicts. And you have to unstage the required lines to discard them. But in most cases there are no conflicts.

tnodir avatar Mar 02 '26 07:03 tnodir

don't over complicate things. There is a reason git itself requires 2 steps to do this action and i think this discussions shows them pretty well. The context menu's are already starting to get pretty big. If you need to make a feature this complex just to reduce 2 clicks into 1, mind you where one of the clicks means you changed you mind on wanting it included in the project, then i think the feature might not be needed.

you don't have to agree with me here, just giving my opinion. I'll be fine either way, it's only 1 click difference anyways

mithom avatar Mar 03 '26 15:03 mithom

If you need to make a feature this complex just to reduce 2 clicks into 1, mind you where one of the clicks means you changed you mind on wanting it included in the project, then i think the feature might not be needed.

🤷‍♂️ Just to clarify, the reason is NOT mainly to reduce 2 clicks into 1 - instead it's about avoiding having to repeat a (potentially large and scattered) multi-selection (since selection is lost when unstaging, and there may already/still be unstaged files as well). While I agree that my example is a special/rare case, at the same time it would make sense to have the two context menus more similar (where discard would then be available in both menus). Also, (as already mentioned) some git actions will automatically stage files "in bulk", which means I may not have intended to include all these files from the start (hence the need for multi-selection).

goran-w avatar Mar 03 '26 19:03 goran-w

I recently stumbled upon this issue while migrating from sourcetree to sourcegit. And so far i love this tool, it's so fast and smooth, and merge conflict window reminds me of jetbrains. It's perfect so far.

Then i noticed that if i need to discard some staged files, the command doesnt show up in the dropdown. I wonder if it's possible to add a Discard command that does 2 steps under the hood:

  • it unstages the file
  • it discards it

This way the functionality will behave similar to just doing those 2 steps manually.

And related to the Amend checkbox, i think the unstage+discard logic should work out even if Amend checkbox is enabled or not .It's up to the developer to be careful if he wants to discard amended changes or not. AND worst case the developer can always uncheck the Amend checkbox afterwards and bring back its accidentally deleted changes. This is another thing i love about this Amend checkbox, that it allows me to go back and forth between changes.

redsigma avatar Apr 03 '26 12:04 redsigma

Maybe we can simply reset (not discard) all changes, whether they are staged or unstaged. However, this operation should be named “Reset to…” so the user clearly understands that it will reset the file to a specific commit.

haoyuenan avatar Apr 09 '26 07:04 haoyuenan