Git

How to Completely Cancel a Rebase in Git

In Git, moving or combining a series of commits to a new base commit is known as rebasing. Git Rebase is a robust component of Git that allows Git developers to access and use the commits of another branch along with their commits. But occasionally, Git developers may want to completely undo the rebase due to some reasons, such as they may mistakenly rebase the incorrect branch.

This article will illustrate the procedure to completely cancel a rebase.

How to Completely Cancel a Rebase in Git?

To completely cancel a rebase, first, go to the Git repository. Then, the β€œgit reset <commit id>” command completely discards the rebase in the current branch.

Check out the mentioned steps to completely cancel a rebase in Git.

Step 1: Open Git Terminal
First, launch the β€œGit Bash” Git terminal from the Start menu:

Step 2: Go to Git Repository
Go to the Git repository via the β€œcd” command:

$ cd "C:\Git"

Step 3: Move to Branch to Commit Changes
Move to the branch in which you want to commit changes using the β€œgit checkout” command:

$ git checkout dev

Step 4: Generate New File
Generate the new file by utilizing the β€œtouch” command:

$ touch demo.txt

Step 5: Add Untracked File to Staging Index
Add the untracked changes to the tracking index through the provided command:

$ git add demo.txt

Check the repository status to verify if the changes are added to the staging index or not:

$ git status

It can be observed that the changes are successfully added to the staging index:

Step 6: Commit Tracked Changes
In the next step, track the committed changes using the β€œgit commit” command:

$ git commit -m "Demo.txt is committed"

Step 7: Switch Branch
Now, move to the branch in which you want to rebase the commits of the current branch using the β€œgit checkout” command:

$ git checkout master

After that, use the β€œgit reflog” command to check the Git logs along with references and commit id to verify if the changes are added to log references and note the current position of HEAD:

$ git reflog

Step 8: Rebase Branch
Next, rebase the other branch in the current branch to access the commit of the other branch using the β€œgit rebase <branch-name>” command:

$ git rebase Dev

After rebasing, again check out the Git log using β€œgit reflog” to verify whether the rebasing of the branch takes place or not:

$ git reflog

The output shows that we have successfully rebased the commits of another branch. Now, note the previous commit id to completely cancel the rebase:

Step 9: Completely Cancel a Rebase
Next, utilize the β€œgit reset” command along with the previous commit id to revert the changes and cancel the rebase completely:

$ git reset 7d90678

Again, check the log references to see the position of HEAD:

$ git reflog

It can be seen that we have successfully reverted a rebase:

As an alternative, you can confirm the cancellation of rebase by checking out the logs of the current branch:

$ git log

You can see that the current branch does not contain any commits from the other branch:

We have taught you the method to cancel a rebase in Git completely.

Conclusion

To completely cancel the Git rebase, open the Git repository, and commit the tracked changes using the β€œgit commit” command. After that, move to the branch in which you would like to completely cancel the rebase using the β€œgit checkout <branch-name>” command. Lastly, use the β€œgit reset <commit-id>” command to undo the rebase, where commit id is the id of the commit that came before the rebase. This write-up has demonstrated how to cancel a rebase completely.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.