0

My experience with git is limited. I am stuck with a problem where I made wrong changes to a couple of files (e.g foo.c and bar.c) and committed. After another commit I realized that my changes were bad. So, the wrong changes with these two files are in HEAD~1. The correct version of these files were checked in to HEAD~5 and HEAD~7 respectively.

What is the recommended way to remove the wrong edits? I tried this :

`git checkout SHA path_to_foo/foo.c`

But it does not actually modify foo.c so that I can create another commit with this.

Santanu C
  • 1,362
  • 3
  • 20
  • 38
  • Did you already push the commit? – Tim Mar 23 '15 at 21:21
  • @Tim : Yes, the commits are already pushed to the remote branch. – Santanu C Mar 23 '15 at 21:22
  • 2
    This post seems to give a pretty complete answer: [reset-or-revert-a-specific-file-to-a-specific-revision-using-git][1] [1]: http://stackoverflow.com/questions/215718/reset-or-revert-a-specific-file-to-a-specific-revision-using-git – Ractoc Mar 23 '15 at 21:23
  • @Ractoc: Thanks for sharing the link. It's exactly what I needed. – Santanu C Mar 24 '15 at 05:25

1 Answers1

3
git revert <SHA>

where SHA points to the bad commit. As manual tells:

... revert the changes that the related patches introduce, and record some new commits that record them.

kofemann
  • 4,217
  • 1
  • 34
  • 39
  • But I don't want to revert everything. I only want to fix these two files. There were other files that were modified as part of these commits. – Santanu C Mar 23 '15 at 21:23
  • What if there are a 1000 other files changed in that commit? You don't want to undo all of that just to 'fix' one file – Tim Mar 23 '15 at 21:26
  • 1
    then you have to revert changes by hand. Create a patch as git diff ~1 foo.c > revert.patch, patch -p 1 -R < revert.patch, and commit – kofemann Mar 23 '15 at 21:27