19

How can I revert some remote repository to an old commit without force pushing and losing history? I know I could do

git reset --hard <commit-hash>
git push -f origin master

but I don't have permissions to force push and I also don't want to lose the history.

I want to do this because I pushed some **** into my master branch and the automatically deployment deploys to production environment from this branch. I worked around it by creating a new branch and changed the deployment settings to use this one, but is there any option to just set the current state of the remote repository to an old commit in case this happens?

bpoiss
  • 13,673
  • 3
  • 35
  • 49
  • Read this out http://stackoverflow.com/questions/34519665/how-to-move-head-checkout-revert-reflog-reset/34519716#34519716 – CodeWizard Mar 09 '16 at 09:18

2 Answers2

26

You can use git revert <commit>… for all the n commits, and then push as usual, keeping history unchanged.

https://git-scm.com/docs/git-revert

Enrique Quero
  • 582
  • 4
  • 12
  • 1
    OK, but git revert would create a new commit that I don't really need, and to remove this one after some time I would also have to force push, or is there any other possibility to get back the reverted commit when I need it again? – bpoiss Mar 09 '16 at 07:56
  • 2
    @bpoiss, the solution without having to force is to use `git revert`, which adds a new commit. Any solution where you _remove_ a commit, e.g. through `git reset --hard` or similar, will require force pushing. – ChrisGPT was on strike Mar 09 '16 at 12:21
  • Yes but this gives me an additional commit, isn't it possible to just set the HEAD to an old commit without doing a new one with `git revert`? – bpoiss Mar 09 '16 at 12:45
  • @bpoiss, you can accomplish that with a force push. Depending on the server you're using you might be able to do it directly on the server using `git reset --hard` or similar, but the effect will be _exactly the same_ as with a force push. (And you are right to be wary of force pushing. It can cause complications, especially with shared repositories.) – ChrisGPT was on strike Mar 09 '16 at 13:06
  • I know, but I don't have permissions to force push or access the server directly. – bpoiss Mar 10 '16 at 07:33
  • 1
    @bpoiss Please realise that every *change* in the history of a branch will require a force push. In order to prevent changes in the history (and thus make sure no force push is needed), you need to add changes as a new commit. – Robin Bastiaan Jun 10 '21 at 13:11
8

As added in the comment above.

Check this out to get the full details:

How to move HEAD back to a previous location? (Detached head)


There is also the git revert which is not described in the above post.

git revert <commit>

git revert simply undoes the desired commit and undoes the changes made to this repo by adding a new "revert" commit.

enter image description here

Ahmad
  • 69,608
  • 17
  • 111
  • 137
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • 1
    hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart – fdrv Aug 17 '21 at 03:07