How do I interactively unstage a particular hunk in git?
GitStagingGit 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:
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)