28

I have few commits on github that I want to change. So in my previous commit I've changed some file/folders and I need to revert changes only for some of them. What is the best solution to make it.

Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277
  • 4
    Does this answer your question or hav I misunderstood? [Reset or revert a specific file to a specific revision using Git?](http://stackoverflow.com/q/215718/5358968) – Steve Mar 24 '16 at 13:43
  • so I have a commit that contains changes for one folder for a submodule. and I need to fix it, by reverting it to state before commit. – Matrosov Oleksandr Mar 24 '16 at 13:51
  • 1
    This is not a duplicate question. Because it was marked as a duplicate, it can no longer be answered. This question was about reversing changes in a commit (a true revert), not just making a new commit with copies of files from an older one. See my [proposed answer as a comment](https://stackoverflow.com/a/50231389/1307074). – Suncat2000 Mar 21 '19 at 15:36

2 Answers2

48

You can use git checkout:

git checkout HEAD~ -- file/to/revert

to stage a version of the file from the previous commit. Then just commit the changes and you're good to go! Of course, you can replace HEAD~, which references the previous commit, with a hash of a commit, a further-back ancestor, or any "tree-ish" object you wish.

JKillian
  • 18,061
  • 8
  • 41
  • 74
5

I have few commits on github that I want to change.
I need to revert changes only for some of them


Few options:

  1. Checkout the desired files from the desired commit

    git checkout <commit> path/to/file
    

  1. Interactive rebase

    // X is the number of commits you wish to edit
    git rebase -i HEAD~X
    

Once you squash your commits - choose the e for edit the commit.

enter image description here


CodeWizard
  • 128,036
  • 21
  • 144
  • 167