I have made some changes to a file which has been committed a few times as part of a group of files, but now want to reset/revert the changes on it back to a previous versio
Assuming the hash of the commit you want is c5f567
:
git checkout c5f567 -- file1/to/restore file2/to/restore
The git checkout man page gives more information.
If you want to revert to the commit before c5f567
, append ~1
(where 1 is the number of commits you want to go back, it can be anything):
git checkout c5f567~1 -- file1/to/restore file2/to/restore
As a side note, I've always been uncomfortable with this command because it's used for both ordinary things (changing between branches) and unusual, destructive things (discarding changes in the working directory).
And to revert to last committed version, which is most frequently needed, you can use this simpler command.
git checkout HEAD file/to/restore
git revert <hash>
Will revert a given commit. It sounds like you think git revert
only affects the most recent commit.
That doesn't solve your problem, if you want to revert a change in a specific file and that commit changed more than that file.
This worked for me:
git checkout <commit hash> file
Then commit the change:
git commit -a
Here's how rebase
works:
git checkout <my branch> git rebase master git checkout master git merge <my branch>
Assume you have
---o----o----o----o master \---A----B <my branch>
The first two commands ... commit git checkout git rebase master
... check out the branch of changes you want to apply to the master
branch. The rebase
command takes the commits from <my branch>
(that are not found in master
) and reapplies them to the head of master
. In other words, the parent of the first commit in <my branch>
is no longer a previous commit in the master
history, but the current head of master
. The two commands are the same as:
git rebase master <my branch>
It might be easier to remember this command as both the "base" and "modify" branches are explicit.
. The final history result is:
---o----o----o----o master \----A'----B' <my branch>
The final two commands ...
git checkout master
git merge <my branch>
... do a fast-forward merge to apply all <my branch>
changes onto master
. Without this step, the rebase commit does not get added to master
. The final result is:
---o----o----o----o----A'----B' master, <my branch>
master
and <my branch>
both reference B'
. Also, from this point it is safe to delete the <my branch>
reference.
git branch -d <my branch>
First Reset Head For Target File
git reset HEAD path_to_file
Second Checkout That File
git checkout -- path_to_file