Git

How to Rebase to a Specific Commit?

Git is an open-source tracking tool often used for managing large development project source code files. It has different functions and multiple commands that make the user’s work easier. The Git rebasing function is specifically designed to move or combine a sequence of Git repository commits from one working branch into another. It also changes the base of the current working branch. Moreover, developers can rebase the specific commit using the β€œgit rebase” command.

This article will illustrate the procedure to rebase a specific commit.

How to Rebase to a Specific Commit?

To rebase to a specific commit, try out the below-listed steps:

Step 1: Navigate to Git Repository

Use the below-stated command and switch to the specified repository:

$ cd "C:\Users\nazma\Git\testing-repo"

Step 2: Display Branches List

Next, view the list of all local branches by executing the β€œgit branch” command:

$ git branch

Step 3: Switch Local Branch

Now, execute the β€œgit checkout” command with the desired local branch name and navigate to it:

$ git checkout alpha

Step 4: Generate File

To create a file in the Git working area, run the given β€œtouch” command:

$ touch file1.txt

Step 5: Track All Changes

After that, run the git add .β€œ command and track all added changes to the staging index:

$ git add .

Step 6: Update Local Repository

Now, push all the tracked changes into the current working local repository through the β€œgit commit” command along with the particular commit message:

$ git commit -m "initial commit"

Step 7: Checkout to Local Branch

Next, use the β€œgit checkout” command and switch back to the main working branch:

$ git checkout master

Step 8: Create New Branch

To create a new branch from the current working branch, run the β€œgit branch <new-branch-name>” with the β€œ<current-working-branch^>”:

$ git branch beta master^

Step 9: Ensure Created Branch

Execute the β€œgit branch” command to view the list of all the local branches:

$ git branch

It can be seen that the newly created β€œbeta” local branch now exists in the list:

Step 10: Switch to New Branch

After that, switch to the newly created branch by running the β€œgit checkout” command:

$ git checkout beta

Step 11: Git Rebase

Finally, perform the β€œgit rebase” into the desired local branch:

$ git rebase alpha

According to the below-listed output, the rebase action has been performed successfully:

Step 12: View Git Log History

Run the β€œgit log” command to display the Git repository log history:

$ git log .

Step 13: Delete Rebased Branch

Next, delete the rebased branch by executing the β€œgit branch” with the β€œ-d” option and local branch name:

$ git branch -d alpha

Here, the β€œ-d” option assists in deleting the β€œalpha” local branch:

Step 14: View Git Reference Log History

Use the below-stated command to check the reference log history:

$ git log .

It can be observed that HEAD only points to the β€œbeta” local branch, and the rebased branch commits exist in the new branch history:

That’s all! We have compiled the most straightforward procedure to rebase to a specific commit.

Conclusion

To rebase to a specific commit, first, move to the required Git repository and check the list of local branches. After that, switch to the required Git local branch. Generate a file and track it to the Git staging index. Next, update the repository by pushing the added changes and switching back to the main working branch. Then, create and move to the new local branch. Finally, execute the β€œgit rebase <local-branch-name>” command. Lastly, delete the rebased branch from the local repository. This article demonstrated the procedure to rebase a specific commit.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.