Rebasing
4. When and How to Use Rebase
Rebasing is another way to integrate changes from one branch into another, but it works differently from merging. Instead of creating a merge commit, rebasing rewrites your branch's history as if you had branched off the target branch more recently. This results in a cleaner, linear history, which can be easier to follow. However, rebasing also has its risks.
The key difference between merging and rebasing is that rebasing modifies the commit history. Therefore, it can cause problems if others are working on the same branch. If you rebase a branch that others have already based their work on, you'll essentially be rewriting their history as well, leading to confusion and potentially lost work. For this reason, rebasing is generally recommended only for local branches or branches where you're the sole developer.
To rebase, make sure you're on the branch you want to rebase, and then run `git fetch` to get the latest changes from the remote repository. Next, run `git rebase origin/main` (replace 'main' with the target branch). Git will then replay your commits on top of the latest commits from the target branch. If you encounter conflicts during rebasing, you'll need to resolve them in the same way as with merging.
In simple terms, rebase means taking the changes you have made on your branch and applying them on top of the latest main branch. For example, if your main branch has changes A, B, and C, and your branch has changes D and E, rebase will take the changes D and E, and apply them after the changes C. Making it the changes are now A, B, C, D, and E. Merge will include these changes but keep the history as it is with two branches. In the merge result is two branches. In Rebase the result is one branch with a straight line.