问题
First, to be clear, I didn't find the right answer to my issue!!
Description I've pushed (very accidentally) a branch into master and some files were lost :-S (they weren't in the pushed branch). I'd like to retrieve them by rolling back to a nth-previous commit.
In How to revert Git repository to a previous commit? it is said to use revert, which I agree. But when I do git revert <nth-previous_right_commit>
the missing files are still missing and, if according to revert
definition it undoes changes, in this case that doesn't seem to happen.
I get the missing files back if I do git checkout <nth-previous_right_commit>
, but I can't do an effective commit (I mean, git commit
says nothing to commit :-S).
Edit:
Previously I didn't notice the snapshot I want to retrieve is the 4th-5th previous commit but there are merge commits in between, so doing a git revert -m 1 <nth-previous-commit>..HEAD
complaints about no merge commits.
So, how can accomplish the revert with merge commits in between?
Thanks
回答1:
To make master
of the remote repository where it should be:
git push origin -f <nth-previous_right_commit>:master
You may need the force-push right if your remote repository has setup access control.
And in the local,
git checkout master
git reset <nth-previous_right_commit> --hard
You can do both,
git checkout master
git reset <nth-previous_right_commit> --hard
git push origin -f master:master
来源:https://stackoverflow.com/questions/47811491/git-revert-range-of-commits-already-pushed-with-merge-commits-in-between