How do I revert selected lines or chunks

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.


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


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.


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
  • Source: http://git.661346.n2.nabble.com/Revert-hunk-td4991128.html

    链接地址: http://www.djcxy.com/p/4176.html

    上一篇: 你可以在TortoiseGit中进行部分提交吗?

    下一篇: 如何恢复选定的行或块