How to Rebase One Branch onto Another: 1-Min Guide

Hello Developers!

Ever found yourself in a situation where your feature branch is behind the master branch, and your team leader says, "Raised a PR without creating a merge commit!"?

Don't worry we all have been there. Instead of creating messy merge commits, you can use rebase to catch up your branch with the master branch.

Rebasing helps keep your commit history clean and linear, making it easier for everyone to follow. Plus, it’s a handy way to resolve conflicts.

Step-by-Step Guide to Rebase your Branch like a Pro:

Steps to follow:

1. Before starting the rebase, make sure you pull the latest changes from the target branch:

git pull origin <branch_name>

2. After taking pull, navigate to the branch you want to rebase using the below command:

git checkout <branch_name>

3. Start rebasing your branch onto the target branch by running below command:

git rebase <target_branch>

After running this command, you’ll see a message successfully rebased and updated 'refs/heads/branch_name'.

4. If there are any merge conflicts during the rebase, Git will stop and notify you. Check the source control panel to identify which files have merge conflicts.

5. Open the conflicted files, resolve the issues, and stage them using below command:

git add <conflicted_file_name>

Then, continue the rebase using below command:

git rebase --continue

If the conflicts are unrelated, you can skip them using below command:

git rebase --skip

Repeat the process until the rebase is finished.

6. Once the rebase is complete, verify the commit history by running below command:

git log --oneline

7. Push the rebased branch to a remote repository using below command:

git push origin <branch_name> --force-with-lease

That’s it!

Your branch is now successfully rebased to the target branch. By rebasing you can keep your branches and commit history linear🚀.

Conclusion

Rebasing not only helps you to maintain clean and linear commit history also helps you to resolve conflicts, and ensure that code stays in sync with master branch without unnecessary merge commits.

Happy coding! 🚀

Frequently Asked Questions (FAQ’s) 

1. How does git rebase help in maintaining a clean commit history?

Git rebase process helps us by moving branch commits to the top of another branch, eliminating merge commits and creating a cleaner, more readable project history.

Key benefits:

  • Linear commit history
  • Eliminates unnecessary merge commits
  • Improves code readability.

2. Why do you need to use --force-with-lease when pushing a rebased branch?

'--force-with-lease' works as a safety check when force pushing, preventing accidental overwrites of remote changes.

It also ensures you don't unintentionally discard work added by other team members, protecting collaborative efforts and maintaining version control.

3. How do you resolve conflicts during a Git rebase?

When conflicts occur during a Git rebase, follow the below steps:

Open the conflicted files, in the source control panel to identify which files have merge conflicts.

Resolve the conflicts and stage the file:

git add file-name  

Continue the rebase process:

git rebase --continue  

If more conflicts arise, repeat the process. To abort the rebase and revert to the previous state:

git rebase --abort  

If the conflicts are unrelated, you can skip them using below command:

git rebase --skip

By this process you can resolve merge conflicts using rebase process.

Still have questions?

Send an email to archana@leadwalnut.com, OR

Book a FREE consultation with an expert developer here.