lazygit icon indicating copy to clipboard operation
lazygit copied to clipboard

[Feature Request] Discard staged only

Open MuhammadSawalhy opened this issue 4 years ago • 12 comments

Is your feature request related to a problem? Please describe.

I am in a situation where I need the staged and keep the unstaged changes unstaged.

We may also need to do this with a single file that is partially staged.

Describe the solution you'd like

  1. In the reset menu options which opens by pressing D, we should find an option to discard staged changes only.
  2. It is reasonable also to have this option for the discard changes for one file menu, which opened by pressing d

Describe alternatives you've considered

patch -p1 -R < <(git diff --cached) && git reset

Additional context

image

image

MuhammadSawalhy avatar Feb 21 '22 13:02 MuhammadSawalhy

@MuhammadSawalhy that code snippet you posted looks a little gnarly. Do you know of any other way to achieve the same result?

jesseduffield avatar Mar 27 '22 07:03 jesseduffield

Do you know of any other way to achieve the same result?

I searched google for help and got this StackOverflow answer

git stash save --keep-index
git reset --hard
git stash pop

MuhammadSawalhy avatar Mar 27 '22 21:03 MuhammadSawalhy

In my case the above solution doesn't work for me, I just copy-pasted it here without trying it. So I think we can go with another trick

git commit -m "temp commit"
git stash
git reset --hard HEAD^
git stash pop

MuhammadSawalhy avatar Mar 29 '22 20:03 MuhammadSawalhy

We already have the stash only staged changes provided by #1870 so this could technically be done by:

  1. stash only staged changes
  2. drop stash

@jesseduffield I could write it up and open a PR if that sounds like a reasonable approach, what do you think?

mark2185 avatar May 07 '22 17:05 mark2185

Sounds good to me @mark2185

jesseduffield avatar May 08 '22 00:05 jesseduffield

@MuhammadSawalhy could you give latest master a try?

The feature you requested should be there, if that's the case feel free to close the issue :)

mark2185 avatar Jun 13 '22 21:06 mark2185

With my poor connection, it seems that I am cloning tons of files 😅

image

MuhammadSawalhy avatar Jun 13 '22 22:06 MuhammadSawalhy

Discarding only staged changes is working 🎉, but there is no option to discard only staged changes of a specific file:

image

MuhammadSawalhy avatar Jun 13 '22 22:06 MuhammadSawalhy

You're right, if a file is partially staged we currently offer to discard either everything or just the unstaged changes.

I'll take a look into that and get back to you.

mark2185 avatar Jun 14 '22 12:06 mark2185

Hi there, can we prioritize this issue? I would like to be able to "Drop Staged changes" only, without having to drop everything that is unstaged for a single file.

Right now I cant "drop a staged hunk/selection"s with a single keypress as well, first those hunks/selections have to be unstaged, then I need to reselect them and can discard. This flow needs to improve significantly.

izelnakri avatar Apr 08 '24 22:04 izelnakri

@izelnakri it's not clear to me how to implement either of those features: if you would like to do some digging and propose a solution using git CLI commands, or find another open source git UI that supports the feature, that will help move this issue along.

Specifically regarding dropping a staged hunk, that sounds especially tricky because the hunk's context often changes upon being unstaged, so if we tried removing the patch in two steps: first unstaging it, then removing it, it may simply not work.

jesseduffield avatar Apr 08 '24 22:04 jesseduffield

Hi @jesseduffield first of all, thanks alot for this amazing tool! Its been a productivity boost, I don't have to tmux switch panes and run most of my git commands anymore.

Secondly, I've seen a tendency of declaring certain issues as "technically impossible". Maybe thats a coincidence I've seen a similar thing here, and in both cases issues are technically possible: https://github.com/jesseduffield/lazygit/issues/3297#issuecomment-1923372937 this issue is actually ongoing on https://github.com/jesseduffield/lazygit/issues/2117

I know that these issues are technically possible because I've been using these features on Vimagit for the last 6 years: https://github.com/jreybert/vimagit . They have been working really well and helped me achieve certain levels of productivity that now Im at a great extent missing in lazygit.

I'll have a look on how they implement it and describe it here if needed. I don't write in go language usually so I'm not very comfortable with it although understand most/all of it almost. I might also able to take a stab at it, probably in the next couple of months due to my current workload.

izelnakri avatar Apr 09 '24 15:04 izelnakri