undo git rebase

How to Undo Git Rebase

Git Rebase is a process of reapplying a sequence of commits from a branch to a specific commit as the new base for those commits. It is generally used to change the starting point of a branch in commit history to the final commit of your master of another branch. Basically, in this case, we move the starting point of a branch to a new point. Sometimes you may need to undo git rebase. In this article, we will learn how to undo git rebase.

How to Undo Git Rebase

Here are the steps to undo git rebase.

1. Find Head commit before rebase

The first step is to find the head commit of a branch before the rebase was started, using reflog command.

$ git reflog

2. Reset Current Branch

Next, you need to reset the current branch to it. For example, if your old commit was at HEAD@{2} in the ref log. In such cases, run the following command. But please be sure you absolutely want to do this, before you run the following command.

On Linux
$ git reset --hard HEAD@{2}

On Windows
> git reset --hard "HEAD@{2}"

You can check the history of old HEAD with the following command.

On Linux
$ git log HEAD@{2}

On Windows
> git log "HEAD@{2}"

3. Push to Remote

If you run the following command you will know if your current branch and remote one have diverged or not, by getting the following message.

$ git status
On branch <branch_name>
Your branch and 'origin/<branch_name>' have diverged

In such cases, you can simply push local changes to remote, to sync the two branches.

$ git push --force

In this short article, we have learnt how to undo git rebase.

Also read:

How to Stop Tracking Folder in Git Repository
Bash Loop Through Files in Directory & Subdirectories
How to Remove Git Ignore Files from Git Repository
How to Stop Python Code After Certain Time
How to Convert Bytes to String in Python

Leave a Reply

Your email address will not be published. Required fields are marked *