0

I commited some code changes to a branch which was the wrong one. I did not push the code but did a 'revert commit' in Sourcetree. This created another commit on the branch. I have not pushed this new commit either.

Since I don't want any commits to be made at all, is there any way back from this state now that the revert commit is already done?

Thanks

Niclas M
  • 48
  • 4
  • Possible duplicate of [How do I "un-revert" a reverted Git commit?](https://stackoverflow.com/questions/8728093/how-do-i-un-revert-a-reverted-git-commit) – phd May 08 '18 at 15:14

1 Answers1

3

You can always erase commits by using the git reset command.

For example, if you have two new commits that you want to reset, run the following.

git reset head~2

This resets recent two commits from the head but keeps the changes in your workspace. If you don't want the changes entirely, you can run the following.

git reset head~2 --hard
Raja Anbazhagan
  • 4,092
  • 1
  • 44
  • 64
  • 1
    Note that since the last commit reverts the commit before it, it shouldn't really make a difference in this case to use `--hard` or not. Also worth noting that this solution works without negative consequence specifically *because* nothing has been pushed; any history rewrite (i.e. any answer that could work) gets more complicated if the affected commits had been pushed... – Mark Adelsberger May 08 '18 at 13:55
  • Kudos, the second one "git reset head~2 --hard" was what I needed. – Niclas M May 11 '18 at 08:27
  • @NiclasM please do note that `--hard` is like playing with fire. Also look at `mark`'s comments – Raja Anbazhagan May 11 '18 at 09:35