Sarasti
Sarasti

Reputation: 75

git - revert range of commits (already pushed) with merge commits in between

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 commitsays 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

Upvotes: 1

Views: 1718

Answers (1)

ElpieKay
ElpieKay

Reputation: 30956

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

Upvotes: 2

Related Questions