0

I have a git repository and the scenario is like this.

Past: rev 102

I made changes and pushed it to the repository, so

Present: rev 103

Now, i want to undo all the changes and go back to rev 102 so that when users checkout/update the repository, it will be the one same as rev 102. I tried revert but the rev 103 still remains in the remote repository and thus upon checkout or update it is rev 103 that gets checked out. Thanks.

1 Answers1

0

There're several ways, and one I used sometimes is as follows.

First reset your header to the previous commit. Surely you can specify any commit when reset.

git reset --hard HEAD~1

Then force push to your remote repository with -f or --force-with-lease. The later option is used to avoid overwriting other users' new commits after rev 103 if any.

git push origin master --force-with-lease

But the precondition is that the remote repository allows force push, because force push is really dangerous which will overwrite histories.

Landys
  • 7,169
  • 3
  • 25
  • 34
  • This manipulation can be dangerous and i would suggest to use `revert` instead. If you proceed a hard reset and push it to the remote, you need to be sure that no one has pulled the last version because you're removing every trace of it! By using the `revert` action you avoid this kind of issues – Joël Salamin Sep 01 '14 at 09:34
  • Yes, `revert` is much safer, but `rev 103` is still there for the case. According to the question's description, it seems it just wants to remove `rev 103` without other users' aware. Hence, it depends on what is really wanted with the knowledge of the dangerous. Besides, we can use `--force-with-lease` instead of `-f` to avoid overwrite other users' new commits. – Landys Sep 01 '14 at 09:52
  • You're right, i just wanted to mention the risk of using this command when working with a remote repo. – Joël Salamin Sep 01 '14 at 10:01