Files
zed/crates/git
Stephen Zuniga 1416404b88 git_ui: Keep staged changes when discarding from the unstaged section (#65559)
## Summary

Closes #63047

In other IDEs like VSCode (and its variants) I got used to being able to
discard unstaged changes of a file while leaving its staged changes in
place, but Zed currently behaves differently and caused me to lose whole
files a few times. I think this is unexpected behavior, so this PR
updates how the git panel handles discarding changes to something I
personally find more intuitive in the hopes that other people do too.

@mmalk3 reproduced [the
behavior](https://github.com/zed-industries/zed/issues/63047#issuecomment-5386884858)
and recorded this video which I'm reusing:


https://github.com/user-attachments/assets/0f54db8e-0015-497f-896e-ae3954055e3c

#63073 fixed this, but was an incomplete experience IMO and was never
merged. I'm taking a slightly more holistic approach so that nothing
about the experience feels buggy. These felt like the minimum required
behavior changes around the original bug fix:
1. When selecting a file that has both staged and unstaged changes, two
rows were selected. Leaving that in place made it unclear what was
actually going to be affected by the "discard" action, so I adjusted
selections to be scoped to their panel. You can still select a file that
exists in both sections by clicking it in both panels if you wanted to.
2. "Trash Untracked Files" would trash staged files that were untracked.
I updated the action to only affect untracked files that are also
unstaged, but I'm open to reverting this so I added it as a separate
commit.

## Testing

1. The original bug fix: discarding unstaged changes leaves the staged
changes in place.


https://github.com/user-attachments/assets/e89da0c3-b14f-47aa-88b6-0e344ef9375b

2. All changes can still be discarded by trashing the changes from the
staged panel. I could see arguments for this being a different
experience but it's the existing behavior so I didn't want to remove it.


https://github.com/user-attachments/assets/47844acb-064e-4ae8-9cac-87d1a426c6f6

3. This is the new multi-select behavior. If the same file is in both
the staged and unstaged panels, selecting one row does not automatically
select the other -- but you can still select both. When you select both,
it'll behave the same as if you selected the staged file and discard all
changes.


https://github.com/user-attachments/assets/f01e4b82-1e72-4860-8709-254a416ca9cd

4. This is the new "Trash untracked" behavior. Only unstaged files that
are also untracked can be trashed via this action. If the untracked file
is staged, it's protected (including the unstaged changes). If there are
no untracked files or they are all staged (partially or completely), the
button is disabled.


https://github.com/user-attachments/assets/6a477a40-5d94-4dea-9bc1-3e280a1c579b

## Self-review

- [x] I've reviewed my diff for quality, security, reliability, and
performance.
- [x] UI changes follow the
[checklist](https://zed.dev/docs/development/ui-checklist).
- [x] Tests cover the new or changed behavior.

Release Notes:

- Fixed discarding unstaged changes in the git panel also discarding
staged changes

---------

Co-authored-by: Smit Barmase <heysmitbarmase@gmail.com>
2026-10-21 11:56:51 +00:00
..