1

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

Sarasti
  • 75
  • 7
  • It may help to realize that while other systems use the verb *revert* to mean *revert **to** some snapshot*, Git's revert means *back out the change of some snapshot*. Mercurial calls this `hg backout` which is a much better verb. – torek Dec 14 '17 at 19:00
  • Since you have use revert the changes successful, you can add an answer for the way you used and accept it as answer. It will also benefit others who have similar questions. – Marina Liu Dec 18 '17 at 08:46

1 Answers1

2

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
ElpieKay
  • 27,194
  • 6
  • 32
  • 53
  • 1
    Thank you for the quick response, but I don't want to use reset as there are more people who already pulled the commits I want to remove. Also, your response was so quick (thanks for that ;-)) but I just realized the (additional) issue is a merge commit in between. – Sarasti Dec 14 '17 at 11:40
  • @Sarasti then you need to find out how many wrong commits have been pushed to `master` and revert them one by one reversely. If all of them were brought in by a merge, it would be simpler by `git revert -m ` where `n` is 1 or 2. Since I don't know exactly how you pushed, you may need to test if `n` should be 1 or 2. – ElpieKay Dec 14 '17 at 11:45
  • Yes, that is the solution. For those who come across this issue, this is the better answer. As @ElpiKay says, `-m` depends on the merge (in my case it was 2, but that is just this particular case). Also, http://www.hostingadvice.com/how-to/git-rollback-commit/ was helpful in order to get a good description, example on how `revert` works. – Sarasti Dec 14 '17 at 15:14