99

Is there a command that allows you to partially undo the changes to a file (or files) in the working directory?

Suppose you have edited a file a lot but you realize that you want to undo some of the changes back to the committed state, but not the other changes.

I'm envisioning an option for git checkout that works a lot like git add -p, i.e. it goes through the file hunk by hunk and asks if you want to keep it or not.

4 Answers 4

125

With git version >= 1.7.1 I can

git checkout -p dir/filename 

or to iterate through "all changed hunks in all files"

git checkout -p 

I am not sure when this feature was introduced.

Sign up to request clarification or add additional context in comments.

3 Comments

It's worth mentioning that you can also do git reset -p to selectively unstage changes from the staging area/index. I also don't know off the top of my head in which version of Git that this was introduced.
This is the correct answer, why it's not been accepted.
@wukong because this answer has been posted 2 years after the question. At this moment (2009), Charles's answer was the best solution
89

You could use

git add -p <path> 

to stage the chunks that you want to keep in a particular file, then

git checkout -- <path> 

to discard the working tree changes that you didn't want to keep, by checking out the staged version of the file.

Finally, you can use

git reset -- <path> 

to revert the staged version of the file to the most recent committed version of the file to leave you with your changes unstaged (if you don't want to commit them).

5 Comments

For this simple use case, which every other version control system just calls "revert", why is the Git command so obscure?
@Jan Do other version control systems' revert command allow you to select which changes within a file are reverted? Genuinely asking, as I only have experience with CVS and Git. In Git, git checkout -- path/to/file is a single command that reverts all changes in that file, but this is not the same as above.
There's also git checkout --patch and git reset --patch which work like git add --patch in latest git.
@Rudie, the -- usually indicate the end of options parsing, and that any arguments that come after it are to be interpreted literally. This means that you wouldn't have to add ./ before any filename that starts with a minus sign, if the filename comes after --.
when i use git checkout --patch the diff appears to be backwards. The minus symbols are ADDING text to my working copy, and the plus symbols are REMOVING lines from my working copy.
11

git checkout $file reverts the state of the file $file to the last committed state. I think you can use git checkout SHA-1 -- $file to revert the file to the commit identified by SHA-1.

1 Comment

yeah, not exactly what I want, since I want to keep some of the changes I have made in the file and revert the others
2

How many commits do you need to go back and select from? If it's just one, maybe take a branch just before it, checkout the file you committed and then use git add -p to add it how you wanted it. Then you can go back to where you were and checkout the file from your temp branch.

that is:

git checkout -b temp troublesome-commit^ git checkout troublesome-commit -- path/to/file git add -p path/to/file git commit -c troublesome-commit git checkout @{-1} git checkout temp -- path/to/file git commit path/to/file git branch -D temp 

Other alternatives include going back and editing the commit with git rebase -i (marking the commit as edit, then doing a git reset HEAD^ and redoing the commit when dropped back into the shell).

If the changes you need to select from are spread over a series of commits, it may be better to extract them as patches (or a patch covering all of them) and hand-edit the patch, taking out the changes you want to keep, and feeding the residual into git apply --reverse.

1 Comment

I'm not actually wanting to revert anything that is committed, I want to undo changes that are only in my working copy

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.