| by Arround The Web | No comments

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.

Share Button

Source: linuxhint.com

Leave a Reply