124

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?

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338

3 Answers3

171

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.

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
  • I don't think my version has it (it's 1.6.3.3), but this looks like the correct answer. – Andrew Grimm Mar 04 '11 at 00: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... :-( – Aasmund Eldhuset Mar 04 '11 at 01:02
  • `The short form -p also works for both commands.`.. You mean `git reset -p filename`? – Nawaz Apr 09 '20 at 07: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. – Aasmund Eldhuset Apr 09 '20 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.

wisbucky
  • 33,218
  • 10
  • 150
  • 101
-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)

John Douthat
  • 40,711
  • 10
  • 69
  • 66
  • 1
    For the Windows users out there, [Git Extensions](http://code.google.com/p/gitextensions/) has a similarly nice UI. – Aasmund Eldhuset Mar 04 '11 at 01:04
  • 4
    So does the built in `git gui`, except I'm not sure I'd use the word "nice" ;) – Tyler Mar 04 '11 at 02:24
  • 1
    [SourceTree](http://www.sourcetreeapp.com/) (Windows + Mac) also has a nice UI for this. – chrnola Sep 24 '14 at 20:56
  • I should mention the more recent graphical Git clients now -- [GitKraken](https://www.gitkraken.com/) is beautiful, and supports this (though it now comes with a hefty fee for commercial use). GitHub's [GitHub Desktop](https://desktop.github.com/) 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 '18 at 21:05