Whenever we have two branches and they are being worked on by separate members and we want to integrate the changes from one branch to the other, we have two options, merging and rebasing. These two commands work in a very different way. Merge will take the changes from the other branch and create a new commit with those changes into the branch. In a way, merge happens as if we did those change ourselves and committed those changes. But merge preserves the reference from the main branch in its log.
/// checkout the branch your are working and merge the branch with latest changes into it. git checkout branch1 git merge branch2
Git rebase however performs this differently. When you rebase your branch onto another branch, git changes the whole base of the branch to the new commit. Its is as though you have started working from the latest changes of the other branch. It takes all the commits from your branch and starts applying them one by one on the new branch, thereby modifying the git branch history.
/// checkout the branch your are working and rebase the branch with latest changes into it. git checkout branch1 git rebase branch2
Rebase is an operation that is not used or not understood properly. If we can just merge the changes, then why would you need to think of rebasing? But rebase has its own uses. One of the most important advantages of git rebase is, it keeps your history in a linear fashion. There will not be any unnecessary branchings when you use rebase. Git rebase also allows us to rebase the commits in an interactive way. This allows us to perform lots of other operations
If we want to rebase with the interactive prompt, we need to use the following command
git rebase branch2 -i
Some of the functionalities we can achieve through interactive rebasing are
- we can squash multiple commits into a single commit
- normally git rebase will apply all the commits. We can choose to skip the commits we don't want to include.
- change the order in which commit occurs in your git history.
- change the commit messages of previously committed changes
If you want to rebase only a specific number of commits then you can do so by using the below command
git rebase branch2 -i HEAD~3
This command will now rebase only the last 3 commits.
These are some very powerful functionalities that will affect the whole branch. We should be careful and use rebase only when we know it's safe to do so. Once you have changes git history it is impossible to recover that.
Usually, it is advised to use rebase only on the branches in which only you are working. It is useful to use rebase in this case and it also helps to keep the local commit tree clean from unnecessary commits. But once you have raised a PR, we should not rebase any further as it will change the commit tree which will cause confusion for the reviewer.
Another thing to keep in mind is that we should never rebase a public branch. We should never rebase a feature branch, in which lots of people are working. This will cause lots of issues for those who are using the branch currently. So it's not a great idea to rebase on public facing branches.