The primary reason for rebasing is to maintain a linear project history. For example, consider a situation where the main branch has progressed since you started working on a feature branch. You want to get the latest updates to the main branch in your feature branch, but you want to keep your branch's history clean so it appears as if you've been working off the latest main branch. This gives the later benefit of a clean merge of your feature branch back into the main branch. Why do we want to maintain a "clean history"? The benefits of having a clean history become tangible when performing Git operations to investigate the introduction of a regression. A more real-world scenario would be:
1. A bug is identified in the main branch. A feature that was working successfully is now broken.
2. A developer examines the history of the main branch using
git log because of the "clean history" the developer is quickly able to reason about the history of the project.
3. The developer can not identify when the bug was introduced using
git log so the developer executes a
4. Because the git history is clean,
git bisect has a refined set of commits to compare when looking for the regression. The developer quickly finds the commit that introduced the bug and is able to act accordingly.
Learn more about git log and git bisect on their individual usage pages.
You have two options for integrating your feature into the main branch: merging directly or rebasing and then merging. The former option results in a 3-way merge and a merge commit, while the latter results in a fast-forward merge and a perfectly linear history. The following diagram demonstrates how rebasing onto the main branch facilitates a fast-forward merge.
Rebasing is a common way to integrate upstream changes into your local repository. Pulling in upstream changes with Git merge results in a superfluous merge commit every time you want to see how the project has progressed. On the other hand, rebasing is like saying, “I want to base my changes on what everybody has already done.”
Don't rebase public history
As we've discussed previously in rewriting history, you should never rebase commits once they've been pushed to a public repository. The rebase would replace the old commits with new ones and it would look like that part of your project history abruptly vanished.
Git rebase standard vs git rebase interactive
Git rebase interactive is when git rebase accepts an
-- i argument. This stands for "Interactive." Without any arguments, the command runs in standard mode. In both cases, let's assume we have created a separate feature branch.
# Create a feature branch based off of main
git checkout -b feature_branch main
# Edit files
git commit -a -m "Adds new feature"
Git rebase in standard mode will automatically take the commits in your current working branch and apply them to the head of the passed branch.
This automatically rebases the current branch onto
＜base＞, which can be any kind of commit reference (for example an ID, a branch name, a tag, or a relative reference to
git rebase with the
-i flag begins an interactive rebasing session. Instead of blindly moving all of the commits to the new base, interactive rebasing gives you the opportunity to alter individual commits in the process. This lets you clean up history by removing, splitting, and altering an existing series of commits. It's like
Git commit --amend on steroids.
git rebase --interactive <base>
This rebases the current branch onto
＜base＞ but uses an interactive rebasing session. This opens an editor where you can enter commands (described below) for each commit to be rebased. These commands determine how individual commits will be transferred to the new base. You can also reorder the commit listing to change the order of the commits themselves. Once you've specified commands for each commit in the rebase, Git will begin playing back commits applying the rebase commands. The rebasing edit commands are as follows:
pick 2231360 some old commit
pick ee2adc2 Adds new feature
# Rebase 2cf755d..ee2adc2 onto 2cf755d (9 commands)
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
Additional rebase commands
As detailed in the rewriting history page, rebasing can be used to change older and multiple commits, committed files, and multiple messages. While these are the most common applications,
git rebase also has additional command options that can be useful in more complex applications.
git rebase -- d means during playback the commit will be discarded from the final combined commit block.
git rebase -- p leaves the commit as is. It will not modify the commit's message or content and will still be an individual commit in the branches history.
git rebase -- x during playback executes a command line shell script on each marked commit. A useful example would be to run your codebase's test suite on specific commits, which may help identify regressions during a rebase.
Interactive rebasing gives you complete control over what your project history looks like. This affords a lot of freedom to developers, as it lets them commit a "messy" history while they're focused on writing code, then go back and clean it up after the fact.
Most developers like to use an interactive rebase to polish a feature branch before merging it into the main code base. This gives them the opportunity to squash insignificant commits, delete obsolete ones, and make sure everything else is in order before committing to the “official” project history. To everybody else, it will look like the entire feature was developed in a single series of well-planned commits.
The real power of interactive rebasing can be seen in the history of the resulting main branch. To everybody else, it looks like you're a brilliant developer who implemented the new feature with the perfect amount of commits the first time around. This is how interactive rebasing can keep a project's history clean and meaningful.
There are a few rebase properties that can be set using
git config. These options will alter the
git rebase output look and feel.
rebase.stat: A boolean that is set to false by default. The option toggles display of visual diffstat content that shows what changed since the last rebase.
rebase.autoSquash: A boolean value that toggles the
rebase.missingCommitsCheck: Can be set to multiple values which change rebase behavior around missing commits.
Prints warning output in interactive mode which warns of removed commits
Stops the rebase and prints removed commit warning messages
Set by default this ignores any missing commit warnings
git log format string that will be used for formatting interactive rebase display
Advanced rebase application
The command line argument
--onto can be passed to
git rebase. When in git rebase
--onto mode the command expands to:
git rebase --onto <newbase> <oldbase>
--onto command enables a more powerful form or rebase that allows passing specific refs to be the tips of a rebase. Let’s say we have an example repo with branches like:
featureB is based on featureA, however, we realize featureB is not dependent on any of the changes in featureA and could just be branched off main.
git rebase --onto main featureA featureB
featureA is the
main becomes the
＜newbase＞ and featureB is reference for what
HEAD of the
＜newbase＞ will point to. The results are then: