Git Undo Rebase- How To Rollback A Rebase
In Git, how do undo git rebase if not happy with it?
Git doesn't have dry-run for rebasing. If I did a rebase and have not pushed it yet, how do I rollback to before, as if it never happened?
Suppose you did a git rebase in your local branch but mistakenly rebased to an older branch and pushed changes to remote, then here is the solution to undo rebase and go back to the previous state. Back up all your changes. Use git reflog to see all your previous operations. git log will show rebased and squashed changes only. Find out the commit where you want to go back to. Most probably this will be the commit before your rebase operation. You will see commit ids like HEAD@{16} Now reset your local branch to this commit.
git reset --hard HEAD@{16}
This only moves the local branch to HEAD@{16}
Now check the status of your local branch.
git status
On branch my_branch
Your branch and 'origin/my_branch' have diverged
Now this is frustrating.
This means your local branch and remote branch have diverged as your local branch has gone back to a previous commit while remote branch is still ahead.
Force your remote branch to go back to where your local branch is.
git push --force
NOTE: At any stage, if you think you have made a mistake and want to reset your local branch with the current remote branch HEAD,
git reset --hard origin/HEAD