Search code examples
gitversion-controlrollback

Git revert remote branch to previous commit


I made multiple commits to a branch to a branch and pushed them to a remote repo. There have been some merge commits in the middle as well. I wish to reset the branch to a known commit before all of these. And I do not wish to rewrite history on the remote.

Basically, I have the commits:

A -> B -> C -> D -> E
F -> G -> H /

and I want to make a commit E -> I that just directly puts the branch in the same state as A. I do not care about reverting individual commits, not least because some of them are merge commits as well.

If it helps, think of this like rolling back a flawed deployment. I do not know and I do not care which individual commits are an issue. I just want to go back to the last known good version.

If it matters, A might be a merge commit as well.

I saw this question but it was about rewriting history. The branch has already been pulled by others so I do not want to mess it all up for everyone.


Solution

  • you need to use git reset with soft

    Start from A to get the state you want

    git reset --hard <Commit A>
    

    move to E without changing files

    git reset --soft <Commit E>
    

    then commit it

    git add . && git commit -m "Commit I"