Git rebase allows you to integrate changes from one branch into another. You can apply commits from the source branch to the target branch.
But when actually you should use git rebase?
Use git rebase when you want to maintain a clean commit history, incorporate changes from a parent branch, resolve conflicts in a controlled manner, and collaborate on shared branches in a team setting. However, don’t make a habit of using
git rebase every time especially when you have other sophisticated ways to solve an issue.
It becomes very handy when you need to change a certain commit message. But this is not only the case. There are other cases where you’ll need this Git utility.
In the next sections, I will describe the situations in more detail where you may need to use
git rebase. Let’s get started.
When to use git rebase?
Below I explained the most common situations in more detail where you should use
Rebase allows you to modify the commit history of a branch. You can combine, rearrange, and modify commits using it.
Clean up commit history with git rebase
Git rebase can be used to squash, reword, or reorder commits in a branch. It can result in a cleaner and more organized commit history. This can be helpful before merging a branch into the main branch or creating a pull request, as it makes the commit history easier to understand and review.
Let’s say you have the following commit history on a feature branch:
93abcde - Commit C 7defghi - Commit B 2ijklmn - Commit A
Now, if you want to combine all three commits into one, and provide a new commit message. This is where git rebase can help you to clean up & combine the commit history.
Below are the commands at a glance:
git checkout feature-branch git rebase -i HEAD~3
This opens an interactive rebase window showing the last 3 commits on the feature branch.
In the interactive rebase window, change the “pick” keyword to “squash” for the commits you want to combine. After that, save and close the rebase window.
Next, another new window will open and it will allow you to edit the message for the combined commit. Update the commit message, save, and close the file.
Finally, you have to push the changes with the “–force” flag:
git push origin feature-branch --force
Be sure to replace the “feature-branch” with the actual name.
For more detail about the forceful push and other troubleshooting options, see how to push after rebasing.
Incorporating changes from a parent branch
Git rebase can be useful to integrate changes from a parent branch. For example, you created a feature branch based on a parent branch. And you want to update your feature branch with the latest changes from the parent branch.
In this case, you can use
git rebase to apply those changes on top of your feature branch. This helps in keeping your branch up-to-date with the latest changes in the parent branch.
Let’s say you have the following commit history on a feature branch (sidebar) that was branched off from a parent branch (front-page):
123abcd - Commit C (sidebar) 456efgh - Commit B (sidebar) 789ijkl - Commit A (sidebar)
Meanwhile, the parent branch (front-page) has received new commits:
789ijkl - Commit D (front-page) 012mnop - Commit E (front-page)
Now if you want to incorporate these changes from the parent branch (front-page) into your feature branch (sidebar) to ensure your branch is up-to-date. You can use the following steps:
Step 1: Ensure you are on the feature branch
git checkout sidebar
Step 2: Fetch the latest changes from the parent branch
git fetch origin front-page
Step 3: Rebase the feature branch onto the parent branch
git rebase origin/front-page
This will replay your feature branch commits on top of the updated parent branch commits.
Step 4: Resolve any conflicts.
If there are any conflicts between your feature branch and the updated parent branch, Git will prompt you to resolve them. Follow the prompts to resolve the conflicts, stage the changes (
git add -A), and continue the rebase.
Step 5: Complete the rebase
git rebase --continue
Once all conflicts are resolved, you can continue with the rebase using the above command.
Step 6: Push the changes
git push origin sidebar --force
Don’t forget to replace the “sidebar” with your actual feature branch, and “front-page” with the parent branch.
Resolving conflicts using git rebase
During a git rebase operation, if there are conflicts between the parent & feature branches, Git will prompt you to resolve those conflicts.
This allows you to address conflicts in a more controlled and organized manner, compared to resolving conflicts during a merge operation.
In the last section, you saw that Git prompts a new window to resolve conflicts if there are any. Let me give you another example below.
Let’s say you have a feature branch that was created from a main branch. You have been working on your feature branch and made several commits.
In the meantime, another team member has pushed changes to the main branch that conflict with your changes. When you try to merge your feature branch back into the main branch, Git will detect the conflicts and prevent a straightforward merge.
In this situation, you can use git rebase to resolve the conflicts and incorporate the changes from the main branch into your feature branch (just like we did in the last section).
Git rebase can be helpful when collaborating on shared branches with other team members. It allows you to update your local branch with changes made by others without creating extra merge commits.
It keeps your commit history clean, and organized, and makes team collaboration much easier.
Imagine you’re working on a team project with multiple colleagues. You each have your own branches for different features or bug fixes. The team has a main branch called “develop” where everyone merges their changes.
- Your colleague, Alice, makes some changes and pushes them to the “develop” branch.
- Meanwhile, you’ve been working on your own branch called “feature-branch” and want to incorporate Alice’s changes into your work.
- Instead of using
git merge, you decide to use
git rebaseto keep your commit history cleaner.
- You run
git pull origin developto fetch Alice’s changes from the remote “develop” branch.
- You then run
git rebase developto apply Alice’s changes on top of your “feature-branch.”
- Git automatically incorporates Alice’s changes into your branch, resolving any conflicts.
- You review and test the changes on your “feature-branch” to ensure everything is working as expected.
- Once you’re satisfied, you push your updated “feature-branch” to the remote repository.
- Your team can then review, approve, and merge your changes into the “develop” branch.
Learn more about Git
- What is Git and why it is used?
- How to install Git on Windows & Mac?
- How to use Git with Visual Studio Code?
- How to use Git and GitHub?
- What is the difference between Git and GitHub?
- How to connect local Git to a remote server?
- How to change Git remote origin URL?
- How to create a Git repository on GitHub?
- Git add all (stage all changes)
- How to undo the git add -A command?
- How to undo the last Git commit?
- How to change the git commit message?
- How to remove or unstage a file from Git commit?
- How to git push after rebase?
- Git switch branches: How to checkout the git branch?
- How to merge a git branch to master?
- Git branch: create, checkout, merge, list, command, delete & push
- How to clone a Git repository?
- How to git pull to override the local project?
- How to remove file from Git?
- When should you use git rebase?
- Git commands & explanation (downloadable cheatsheet included)
- How to create GitHub Pages?
It’s important to know that git rebase rewrites commit history. So you should use it with caution, especially in shared repositories or when collaborating with others.
I would recommend you communicate with your team members before using it. And follow established Git workflows and best practices when using git rebase. I suggest you read an article on Atlassian to better understand the workflow.
In this post, I tried to give you a clear concept to understand the situations when you should use git rebase. Along the way, I provided you with a couple of examples based on real-world cases. Also, I gave you step-by-step commands to solve those cases. Therefore, if you still have questions, please let me know.