How do I interactively unstage a particular hunk in git?
Asked Answered
C

3

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?

Ciliata answered 4/3, 2011 at 0:20 Comment(0)
H
174

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.

Hubble answered 4/3, 2011 at 0:34 Comment(4)
I don't think my version has it (it's 1.6.3.3), but this looks like the correct answer.Ciliata
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... :-(Hubble
The short form -p also works for both commands... You mean git reset -p filename?Guesswork
@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.Hubble
P
5

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.

Pentosan answered 1/5, 2018 at 22:36 Comment(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.Extraterritorial
D
-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)

Dirndl answered 4/3, 2011 at 0:24 Comment(4)
For the Windows users out there, Git Extensions has a similarly nice UI.Hubble
So does the built in git gui, except I'm not sure I'd use the word "nice" ;)Lunar
SourceTree (Windows + Mac) also has a nice UI for this.Kori
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.Industry

© 2022 - 2024 — McMap. All rights reserved.