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.
Asked
Active
Viewed 4.7k times
28
-
4Does 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
-
1This 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 Answers
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
-
-
seem git reset --soft HEAD^ twice return me to a previous state and I reset needed file to a previous commit state. – Matrosov Oleksandr Mar 24 '16 at 14:23
-
5
I have few commits on github that I want to change.
I need to revert changes only for some of them
Few options:
Checkout the desired files from the desired commit
git checkout <commit> path/to/file
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.

CodeWizard
- 128,036
- 21
- 144
- 167