How can I undo 1 file change in my previous commit

I made 2 commits (and I did not press) in git, where "Commit 2" is the most recent ":

git log -2 commit 791962d776f66253d656586b097b2677eaa983d1 Author: michael <michael@michael-laptop.(none)> Date: Tue Jun 29 23:20:58 2010 -0700 Commit 2 commit b743075e81f6fe25fe48ddbef2b5e7cc06623533 Author: michael <michael@michael-laptop.(none)> Date: Tue Feb 16 23:09:53 2010 -0800 Commit 1 

And in my commit 1 b743075e81f6fe25fe48ddbef2b5e7cc06623533 I touched / changed a few files:

  dir1/file1.cpp dir1/file1.h dir1/file2.cpp dir1/file2.h 

My questions are: how can I revert my changes that I made to dir1 / file2.cpp, dir1 / file2.h from commit 1? And keep everything else the same?

Thanks.

+7
git
source share
3 answers

The simplest solution is from your last commit (HEAD) to:

  • checkout these two files in the old version,
  • add them
  • and then lock.
     git checkout b743075e81 - dir1 / file2.cpp
     git checkout b743075e81 - dir1 / file2.h
     git add dir1 / file2.cpp # only if you made additional changes
     git add dir1 / file2.h # only if you made additional changes
     git commit -m "revert dir1 / file2.cpp and dir1 / file2.h"

As Chris Johnsen mentions in the comments:

git checkout with pathspecs updates the index (for example, git reset with pathspecs) and the working tree, so git add not required if no additional changes are made after checking.

+5
source share

If you have already pushed your commits to the origin, then the other solutions described here (using git reset or git checkout and then git commit ) are your only reasonable option.

However , if you have not yet pushed your changes, and you want to delete all traces that have ever been committed, interactive permutation is a good option. Use the following command to restore the last two commits:

 git rebase --interactive HEAD~2 

An editor opens with your two commits. You want to edit the file to show the following, and then save; in particular, you want to change pick before the first commit to edit :

 edit b743075 Commit 1 pick 791962d Commit 2 

Git will then put us in a state in which we can edit the first commit (without touching the second). It says that you can change the commit with git commit --amend , but we really want to reset to commit before the voice and undo it completely:

 git reset HEAD^ 

This will push the changes from Commit 1 back to your working tree. Then git add only the files you want to save and overwrite them with git commit :

 git add dir1/file1.cpp git add dir1/file1.h git commit -m "Commit 1" 

Finally, do a hard reset to delete the files that you do not want to save from your working tree, and complete rebase:

 git reset --hard git rebase --continue 

When the overflow is complete, your repository will have both commits and traces of file2.* Files.

+3
source share

Use the following command to roll back a file with a previously committed file based on commit hashing and the file name you want to return

  git reset <commit hash> <filename> 

For your use, use

  git reset b743075e81f6fe25fe48ddbef2b5e7cc06623533 dir1/file2.cpp git reset b743075e81f6fe25fe48ddbef2b5e7cc06623533 dir1/file2.h 
0
source share

All Articles