Rebasing is an alternative way to merge two branches together. However, unlike merging, it is destructive. It rewrites the git history, with the history becoming linear. Rebasing should never be used on a master or production branch.
The advantages of rebasing include all commits have one parent, assuming you only rebase from your target branch and then merge. The drawbacks include that it doesn’t keep the entire history of all commits.
When merging a branch into another where changes have been made, a three-way merge is performed and a new merge commit is created with it’s parents being the latest commit in both branches. The entire history is preserved.
Performing rebasing is a four step process. First checkout the feature branch, then rebase on top of the target branch. Checkout the base branch, and merge the feature branch into the base branch. Fast-forward merge will be used.
git checkout feature
git rebase master
git checkout master
git merge feature
When we rebase on top of the base branch (master in this case), new commits are created for all of the commits we made in the feature branch. This is because the parent information changes and that changes the sha1 hash used as the folder and file name. The pre-existing commits will be garbage collected by git.