In Git GUI I can select parts of a diff and stage just those lines or chunks. How would I do the opposite, as in roll back changed lines in a file. Usually these are accidental white space changes I just want to revert out but still stage/commit other parts of the same file.
-
git interactive add I believe or http://stackoverflow.com/questions/1085162/how-can-i-commit-only-part-of-a-file-in-git – Luke Hutton May 31 '12 at 01:19
-
That is the opposite. It stages the changes but then you have to revert the file. – Kenoyer130 May 31 '12 at 01:26
-
http://stackoverflow.com/questions/1981830/undo-part-of-unstaged-changes-in-git – Josh Lee May 31 '12 at 03:05
-
The way the question is worded does not match the title of the question. Leif's answer actually answers the question in the title. – mattgately Nov 07 '14 at 15:20
5 Answers
Stage the parts you want with git add -p
, then discard (git checkout -- filename
) the unstaged changes.
Update for Git 1.6.5+
In version 1.6.5, Git learned to checkout with a -p/--patch
flag. You can discard chunks in one step with git checkout -p -- filename
.
From the docs:
Interactively select hunks in the difference between the <tree-ish> (or the index, if unspecified) and the working tree. The chosen hunks are then applied in reverse to the working tree (and if a <tree-ish> was specified, the index).
This means that you can use git checkout -p to selectively discard edits from your current working tree.

- 4,333
- 2
- 17
- 29
-
e.g. Use `git add -p HEAD~1` to interactively revert hunks from two commits back. – Sandip Bhattacharya Jul 26 '23 at 15:39
In Git Gui,
- Stage the entire file that contains the unwanted change(s)
- Unstage the lines/hunks you want reverted
- Select the file in the Unstaged Changes pane and Commit->Revert Changes to undo all (unstaged) changes to that file

- 19,451
- 13
- 99
- 197

- 13,561
- 5
- 60
- 64
For Git Gui: First, click Rescan to scan for the modified file. Next, click the icon to the left of the filename to stage all modifications for commit. Then, right click on the debug line and chose Unstage Line From Commit.
The above information from: http://nathanj.github.com/gitguide/tour.html

- 3,899
- 1
- 20
- 28
I use git stash -p
for this purpose. It has a side effect of creating a stash with the changes which were removed from the working tree, which is occasionally useful to quickly restore them.

- 529
- 5
- 10
Since Git 2.24, Git GUI features the "Revert Hunk" and "Revert Line" options in the context menu, similar to staging. You can even "Undo Last Revert" which is a great addition.
Here's the commit that added this functionality: https://github.com/git/git/commit/62bd99934bb2b1a07f786fdf9b94d7b692768e30

- 131
- 2