git selective revert local changes from a file

GitVersion ControlGit Svn

Git Problem Overview


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 file.

I want to be able to view the diffs on the file, discard(revert) the changes that I don't want and retain the changes I want.

the

git add -i 

command seems to have an option to do that but I don't want to stage this yet.

Git Solutions


Solution 1 - Git

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 (or the index, if unspecified) and the working tree. The chosen hunks are then applied in reverse to the working tree (and if a was specified, the index).

       This means that you can use git checkout −p to selectively discard
       edits from your current working tree.

Solution 2 - Git

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.

Solution 3 - Git

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

Solution 4 - Git

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.

Solution 5 - Git

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.

Solution 6 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPradeepView Question on Stackoverflow
Solution 1 - GitDaniel StutzbachView Answer on Stackoverflow
Solution 2 - GitPaolo CapriottiView Answer on Stackoverflow
Solution 3 - GitGreg HewgillView Answer on Stackoverflow
Solution 4 - GitKent FredricView Answer on Stackoverflow
Solution 5 - GitCB BaileyView Answer on Stackoverflow
Solution 6 - GitAbhishek AnandView Answer on Stackoverflow