The existing branches are not changed in any way. By periodically performing an interactive rebase, you can make sure each commit in your feature is focused and meaningful. If you would prefer a clean, linear history free of unnecessary merge commits, you should reach for git rebase instead of git merge when integrating changes from another branch. To incorporate the new commits into your feature branch, you have two options: merging or rebasing. Integrating an Approved Feature After a feature has been approved by your team, you have the option of rebasing the feature onto the tip of the master branch before using git merge to integrate the feature into the main code base. Note that this will not incorporate upstream changes into the feature branch.
By default, the git pull command performs a merge, but you can force it to integrate the remote branch with a rebase by passing it the --rebase option. This gives you the necessary branch structure to safely utilize rebasing: Local Cleanup One of the best ways to incorporate rebasing into your workflow is to clean up local, in-progress features. Re-writing its history will make it impossible for Git and your teammates to track any follow-up commits added to the feature. The only way to synchronize the two master branches is to merge them back together, resulting in an extra merge commit and two sets of commits that contain the same changes the original ones, and the ones from your rebased branch. This is something that git merge simply cannot do. On the other hand, this also means that the feature branch will have an extraneous merge commit every time you need to incorporate upstream changes. The first step in any workflow that leverages git rebase is to create a dedicated branch for each feature.
All of the other developers are still working with the original master. Take the current one instead. The only thing other developers will see is your finished product, which should be a clean, easy-to-follow feature branch history. This lets you write your code without worrying about breaking it up into isolated commits—you can fix it up after the fact. Reviewing a Feature With a Pull Request If you use pull requests as part of your code review process, you need to avoid using git rebase after creating the pull request. Interactive Rebasing Interactive rebasing gives you the opportunity to alter commits as they are moved to the new branch.
Second, as you can see in the above diagram, rebasing also results in a perfectly linear project history—you can follow the tip of feature all the way to the beginning of the project without any forks. Workflow Walkthrough Rebasing can be incorporated into your existing Git workflow as much or as little as your team is comfortable with. Merging is a safe option that preserves the entire history of your repository, while rebasing creates a linear history by moving your feature branch onto the tip of master. Incorporating Upstream Changes Into a Feature In the Conceptual Overview section, we saw how a feature branch can incorporate upstream changes from master using either git merge or git rebase. Needless to say, this is a very confusing situation. But, instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the original branch. This makes it easier to navigate your project with commands like git log, git bisect, and gitk.
Typically, this is used to clean up a messy history before merging a feature branch into master. For example, the following command begins an interactive rebase of only the last 3 commits. . Both of these commands are designed to integrate changes from one branch into another branch—they just do it in very different ways. On the other hand, if you want to preserve the complete history of your project and avoid the risk of re-writing public commits, you can stick with git merge.
We saw an example of the first option in the Interactive Rebasing section. To begin an interactive rebasing session, pass the i option to the git rebase command: git checkout feature git rebase -i master This will open a text editor listing all of the commits that are about to be moved: pick 33d5b7a Message for commit 1 pick 9480b3d Message for commit 2 pick 5c67e61 Message for commit 3 This listing defines exactly what the branch will look like after the rebase is performed. This avoids all of the potential pitfalls of rebasing discussed below. The Rebase Option As an alternative to merging, you can rebase the feature branch onto master branch using the following commands: git checkout feature git rebase master This moves the entire feature branch to begin on the tip of the master branch, effectively incorporating all of the new commits in master. First, it eliminates the unnecessary merge commits required by git merge.