git selective revert local changes from a file
I believe you can do it most simply with:
git checkout -p <optional filename(s)>
From the manpage:
−p, −−patch 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.
You can do that directly with git checkout -p
. See Daniel Stutzbach's answer below.
Old answer (before checkout -p
was introduced):
You can do it like this:
git add -i
(select the hunks you want to keep)
git commit -m "tmp"
Now you have a commit with only the changes you want to keep, and the rest is unstaged.
git reset --hard HEAD
At this point, uncommitted changes have been discarded, so you have a clean working directory, with the changes you want to keep committed on top.
git reset --mixed HEAD^
This removes the last commit ('tmp'), but keeps the modifications in your working directory, unstaged.
EDIT: replaced --soft
with --mixed
, to clean up the staging area.