Rebasing
Rebasing, just like merging, is a way to integrate changes from one branch into another branch. It's a way to maintain a linear commit history, which can make it easier to understand the project history.

If a project with a main branch and a feature branch had changes made to the main branch while the feature branch was being worked on, rebasing the feature branch onto the main branch would make it look like the feature branch was created after the changes to the main branch were made, keeping the commit history clean by not adding merge commits to the timeline. It is then easier to understand the steps taken during development of a project.

The downside to rebasing is that it will rewrite the commit history of the feature branch, which may not be the desired outcome. Never rebase a shared branch (like the main branch) into a personal branch. That will cause your main branch to diverge from the remote main branch, creating copies of the same commits, which will cause conflicts when trying to push your changes to the remote repository.

Rebasing Example

Rebasing Image from Atlassian: Merging vs Rebasing

Do NOT do this!

Rebasing Image from Atlassian: Merging vs Rebasing
Use Cases:
Examples:
Rebasing a feature branch on the tip of main:
git checkout feature
git rebase main

Interactive Rebasing (starts a vim session):
git rebase -i [branch_name]

Output will include something like this:
pick 33d5b7a Message for commit #1
pick 9480b3d Message for commit #2
pick 5c67e61 Message for commit #3

Replace pick with another command to execute an action on the commit. The rebase will execute on file save.

reword 33d5b7a Message for commit #1
fixup 9480b3d Message for commit #2
fixup 5c67e61 Message for commit #3

This will rewrite a new commit #1 with a new message with the changes from commit #2 and #3

Save the file to execute the rebase.
:wq

List of commands for interactive rebase:
  • squash: This will combine the selected commit's changes with the previous commit, including the commit message
  • fixup: This will combine the selected commit's changes into the previous commit, keeping the previous commit's message intact
  • reword: This will change a commit's message
Note that the order of the commits in the list is the order of execution. It is possible to modify the order since the commits are being rewritten.