Search code examples
gitgit-commitgit-reset

Roll back both on local branch and master branch?


How can I roll back to the last commit both local branch and master branch?

$ git reset --hard HEAD^
HEAD is now at e861a3e Added push menu label.

$ git status
On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)
nothing to commit, working tree clean

It asks me to pull. But if I pull I wiil get the same commit back from the master branch.

I made a mistake in my code and I have committed and pushed. So I need to roll back the commit and the push.

How can this be done correctly?


Solution

  • Usually you should not remove commits from remote. In general you should not modify remote history. Someone could have started making changes on top of that commit.

    Usually it's much better to revert a commit. A revert is a new commit that undoes all the changes contained in the reverted commit. You revert a commit with

    git revert COMMIT_HASH
    

    Then you push this commit as every other commit, and the changes of COMMIT_HASH are gone.

    Before you do this, you have to undo your commit deletion on your local branch. You can do this with

    git reset --hard origin/master