0

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?

Run
  • 54,938
  • 169
  • 450
  • 748
  • 2
    You could force push local: `git push --force origin master` after you did the reset on the local branch. – j-i-l May 01 '17 at 15:18
  • 2
    You can read this answer http://stackoverflow.com/questions/4114095/how-to-revert-git-repository-to-a-previous-commit – Ziyaddin Sadigov May 01 '17 at 15:20

1 Answers1

2

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
Manuel Schmidt
  • 2,178
  • 2
  • 15
  • 19