125

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?

3 Answers 3

172

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.

4
  • I don't think my version has it (it's 1.6.3.3), but this looks like the correct answer. Mar 4, 2011 at 0:50
  • 2
    In that case (and assuming that you cannot upgrade for some reason), I suggest that you use git stash save --keep-index to save and reset your current working copy changes. Then, you can reset your file and undo the changes you don't want. If you copy the file to some temporary location first, you can use diff to save the changes you undo. Then, you can add the file back again (no need for an interactive add since you stashed away the other changes you weren't interested in). Use git stash pop to get back the old changes, and diff to apply the changes you undid. Quite cumbersome... :-( Mar 4, 2011 at 1:02
  • The short form -p also works for both commands... You mean git reset -p filename?
    – Nawaz
    Apr 9, 2020 at 7:08
  • 1
    @Nawaz: That's correct - git add -p filename selectively stages changes from that file, and git reset -p filename selectively unstages changes. There's also git checkout -p -- filename, which lets you selectively discard changes from a file. Warning: each of add and reset can be used to undo the other of the two, but if you use this form of checkout to discard a change, you can't get it back. Apr 9, 2020 at 21:57
4

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.

1
  • git gui does this in an excellent way. I wonder if there is a non-GUI way, to use in a terminal when e.g. being connected to a Linux server. Oct 13 at 14:58
-1

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)

4
  • 1
    For the Windows users out there, Git Extensions has a similarly nice UI. Mar 4, 2011 at 1:04
  • 4
    So does the built in git gui, except I'm not sure I'd use the word "nice" ;)
    – Tyler
    Mar 4, 2011 at 2:24
  • 1
    SourceTree (Windows + Mac) also has a nice UI for this.
    – chrnola
    Sep 24, 2014 at 20:56
  • I should mention the more recent graphical Git clients now -- GitKraken is beautiful, and supports this (though it now comes with a hefty fee for commercial use). GitHub's GitHub Desktop is free, beautiful, and supports easily staging and unstaging hunks. GitKraken and GitHub Desktop are both cross-platform, but neither are open-source.
    – jdgregson
    Jan 23, 2018 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.