Rebase - biswajitsundara/gitdoc GitHub Wiki
Git Rebase allows us to change the base of our branch to be a different branch
- and thereby incorporate changes from that branch into your own branch.
- When we perform a Git rebase, Git first identifies the common ancestor commit of the two branches involved.
- It then applies each commit of the branch being rebased one by one on top of the target branch,
- effectively rewriting the history of the rebased branch.
- This results in a linear, chronological sequence of commits,
- which can make it easier to review and understand the development history of a project.
Git rebase should be used with caution, especially in a collaborative environment
- It can alter the history of a project and potentially cause conflicts for other team members.
- It is generally recommended to use Git rebase on local, feature branches before merging them into a shared branch (main or release branch)
- This is a common scenario that we cut a
feature
branch frommaster
branch - We are working on the
feature
branch for some time - During that time our team mates have merged their code to the
master
branch - So our branch is behind lot of commits as of
master
branch - If we want to merge our code to
master
, we will have so many conflicts - So before merging, we can
rebase
our branch withmaster
so all the latest changes come in - We resolve the conflicts if there are any and then finally merge to master
Let's replicate the scenario
1. Master branch
- Has a file
hello.html
- The content of the file is
<html>
<h1>Hello World</h1>
</html>
2. feature/contactus branch
- Create a new file
contact.html
- The content of the file is
<html>
<h1>Contact us</h1>
</html>
3. feature/about branch
- Create a new file
about.html
- The content of the file is
<html>
<h1>About us</h1>
</html>
- Update the
hello.html
file
<html>
<h1>Hello World</h1>
<p>About us</p>
</html>
- Create an PR and merge the PR
feature/contactus branch
- Update the
hello.html
file
<html>
<h1>Hello World</h1>
<p>Contact us</p>
</html>
- If we go to PR preview, it will say we need to resolve one conflict
- The conflict is on the
hello.html
file
- First, make sure we are on feature branch
git checkout feature/contactus
- Fetch the latest changes from the remote master branch
git fetch origin master
- Once latest changes are pulled, start the rebase
git rebase origin/master
- If any conflicts occur git will pause and ask to resolve the conflict
- Edit the file, resolve the conflict and save the changes
- Then stage the changes using
git add <file>
- Continue the rebase process after resolving all the conflicts
git rebase --continue
- Repeat steps 4-7 until all changes from the
master
branch have been applied to thefeature
branch. - Now the rebase process is complete
- Push the changes to the remote feature branch
git push --force-with-lease origin feature/contactus
- Use
--force-with-lease
option to avoid overwriting any changes that have been made to the feature branch since your last push. - That's it! The feature branch should now include all the changes from the
master
branch.
git checkout feature/contactus
git fetch origin master
git rebase origin/master
|
| if conflict occurs
| git add <file>
| git rebase --continue
|
git push --force-with-lease origin features/contactus
This is a git command that replays the changes made on the origin/master branch on top of your current branch.
- When you run this command, Git will first identify the common ancestor commit of your current branch and the origin/master branch.
- It will then apply each commit that exists in origin/main but not in your current branch, one by one, on top of your branch.
- This effectively integrates the changes made in
origin/master
into your branch's history. - Using git rebase helps keeping the
feature
branch up-to-date with changes made to the master branch - and helps to ensure a smoother merge when the feature is eventually merged back into the master branch.