How do I interactively unstage a particular hunk in git?

GitStaging

Git Problem Overview


In git, if I have a couple of hunks from the same file staged in my index, how can I interactively unstage one of them?

Is there any alternative to unstaging the entire file, and then re-staging the hunks I want to keep, or manually undoing the changes to the working copy, and then interactively adding those undone changes?

Git Solutions


Solution 1 - Git

Try git reset --patch filename; this should do the opposite of git add --patch, according to the documentation. The short form -p also works for both commands.

Solution 2 - Git

git gui has a decent GUI to interactively stage or unstage hunks or lines. There are prettier/better GUI clients, but git gui is lightweight, built-in, and cross platform (lin, win, mac).

https://git-scm.com/docs/git-gui

Simply right click on a hunk to stage/unstage. For lines, highlight the lines first, then right click.

Solution 3 - Git

GitX has a nice UI for unstaging chunks of a file: enter image description here

The official client hasn't been maintained in a while, but a fork over at GitHub with more features is popular in some circles. (blog post about it)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrew GrimmView Question on Stackoverflow
Solution 1 - GitAasmund EldhusetView Answer on Stackoverflow
Solution 2 - GitwisbuckyView Answer on Stackoverflow
Solution 3 - GitJohn DouthatView Answer on Stackoverflow