In my git repo which is tracking a svn repo I have made a number of edits to a single file.
Now I want to revert those changes(like svn revert), but only portions of the
The command line options described in the answers here are handy when the file is on a server which I am accessing via a ssh terminal. However, when the file is on my local machine I prefer the following way:
Open the file in the netbeans editor (which comes with git support). Netbeans puts red/green/blue marks at line numbers to indicate where stuff was deleted/added/modified (respectively).
Right clicking any of these marks gives you an option to undo that change. In addition, you can right click on red and blue marks to find see the old version in a popup.
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.
Re-reading the question, it sounds like you want to revert changes that are in your working tree and not changes that have been previously committed but some of the other answers make it sound like my reading may be wrong. Can you clarify?
If the changes are just in your working copy then the easiest way to do this is to stage the changes you want to keep with:
git add -i <file>
Then throw away the changes that you don't want to keep by checking out the index version:
git checkout -- <file>
Then unstage the changes if you don't want them staged yet:
git reset -- <file>
This recipe only reverts selected changes to the file (or files that you specify) and doesn't create any temporary commit that then needs reverting.
If you want to selectively apply only some of the changes made in previous commits then you can reset a file to a previous committed state first:
git reset <commit_before_first_unwanted_change> -- <file>
Then you can follow the previous recipe of git add -i <file>
to stage those changes that you want to keep, git checkout -- <file>
to throw away the unwanted changes and git reset -- <file>
to 'unstage' the changes.
You could run git diff
on the file, save the resulting diff, edit it to remove the changes you do want to save, then run it through patch -R
to undo the remaining diffs.
git diff file.txt >patch.tmp # edit patch.tmp to remove the hunks you want to keep patch -R <patch.tmp
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.
Looks like you want
git revert --no-commit $REVSISON
You can then use
git diff --cached
to see what change will be made before commiting ( as reverting is just a commit in a forwards direction that replicates the inverse of a change in the past )
If you were with a pure Git repository, you could possibly, depending on your goals, utilise interactive rebase (git rebase -i
) to go back to the commit you didn't like and edit the commit retroactively so that the changes you don't like never happened, but thats generally only for if you KNOW you'll never want to see it again.