The git rebase command has a reputation for being magical Git voodoo that beginners should stay away from, but it can actually make life much easier for a development team when used with care. In this article, well compare git rebase with the related git merge command and identify all of the potential opportunities to incorporate rebasing into the typical Git workflow.
The first thing to understand about git rebase is that it solves the same problem as git merge. Both of these commands are designed to integrate changes from one branch into another branchthey just do it in very different ways.
Consider what happens when you start working on a new feature in a dedicated branch, then another team member updates the main branch with new commits. This results in a forked history, which should be familiar to anyone who has used Git as a collaboration tool.
Now, lets say that the new commits in main are relevant to the feature that youre working on. To incorporate the new commits into your feature branch, you have two options: merging or rebasing.
The easiest option is to merge the main branch into the feature branch using something like the following:
Or, you can condense this to a one-liner:
This creates a new merge commit in the feature branch that ties together the histories of both branches, giving you a branch structure that looks like this:
Merging is nice because its a non-destructive operation. The existing branches are not changed in any way. This avoids all of the potential pitfalls of rebasing (discussed below).
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. If main is very active, this can pollute your feature branchs history quite a bit. While its possible to mitigate this issue with advanced git log options, it can make it hard for other developers to understand the history of the project.
As an alternative to merging, you can rebase the feature branch onto main branch using the following commands:
This moves the entire feature branch to begin on the tip of the main branch, effectively incorporating all of the new commits in main. 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.
The major benefit of rebasing is that you get a much cleaner project history. First, it eliminates the unnecessary merge commits required by git merge. Second, as you can see in the above diagram, rebasing also results in a perfectly linear project historyyou can follow the tip of feature all the way to the beginning of the project without any forks. This makes it easier to navigate your project with commands like git log, git bisect, and gitk.
But, there are two trade-offs for this pristine commit history: safety and traceability. If you dont follow the Golden Rule of Rebasing, re-writing project history can be potentially catastrophic for your collaboration workflow. And, less importantly, rebasing loses the context provided by a merge commityou cant see when upstream changes were incorporated into the feature.
Interactive rebasing gives you the opportunity to alter commits as they are moved to the new branch. This is even more powerful than an automated rebase, since it offers complete control over the branchs commit history. Typically, this is used to clean up a messy history before merging a feature branch into main.
To begin an interactive rebasing session, pass the i option to the git rebase command:
This will open a text editor listing all of the commits that are about to be moved:
This listing defines exactly what the branch will look like after the rebase is performed. By changing the pick command and/or re-ordering the entries, you can make the branchs history look like whatever you want. For example, if the 2nd commit fixes a small problem in the 1st commit, you can condense them into a single commit with the fixup command:
When you save and close the file, Git will perform the rebase according to your instructions, resulting in project history that looks like the following:
Eliminating insignificant commits like this makes your features history much easier to understand. This is something that git merge simply cannot do.
Once you understand what rebasing is, the most important thing to learn is when not to do it. The golden rule of git rebase is to never use it on public branches.
For example, think about what would happen if you rebased main onto your feature branch:
The rebase moves all of the commits in main onto the tip of feature. The problem is that this only happened in your repository. All of the other developers are still working with the original main. Since rebasing results in brand new commits, Git will think that your main branchs history has diverged from everybody elses.
The only way to synchronize the two main 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). Needless to say, this is a very confusing situation.
So, before you run git rebase, always ask yourself, Is anyone else looking at this branch? If the answer is yes, take your hands off the keyboard and start thinking about a non-destructive way to make your changes (e.g., the git revert command). Otherwise, youre safe to re-write history as much as you like.
If you try to push the rebased main branch back to a remote repository, Git will prevent you from doing so because it conflicts with the remote main branch. But, you can force the push to go through by passing the --force flag, like so:
This overwrites the remote main branch to match the rebased one from your repository and makes things very confusing for the rest of your team. So, be very careful to use this command only when you know exactly what youre doing.
One of the only times you should be force-pushing is when youve performed a local cleanup after youve pushed a private feature branch to a remote repository (e.g., for backup purposes). This is like saying, Oops, I didnt really want to push that original version of the feature branch. Take the current one instead. Again, its important that nobody is working off of the commits from the original version of the feature branch.
Rebasing can be incorporated into your existing Git workflow as much or as little as your team is comfortable with. In this section, well take a look at the benefits that rebasing can offer at the various stages of a features development.
The first step in any workflow that leverages git rebase is to create a dedicated branch for each feature. This gives you the necessary branch structure to safely utilize rebasing:
One of the best ways to incorporate rebasing into your workflow is to clean up local, in-progress features. By periodically performing an interactive rebase, you can make sure each commit in your feature is focused and meaningful. This lets you write your code without worrying about breaking it up into isolated commitsyou can fix it up after the fact.
When calling git rebase, you have two options for the new base: The features parent branch (e.g., main), or an earlier commit in your feature. We saw an example of the first option in the Interactive Rebasing section. The latter option is nice when you only need to fix up the last few commits. For example, the following command begins an interactive rebase of only the last 3 commits.
By specifying HEAD~3 as the new base, youre not actually moving the branchyoure just interactively re-writing the 3 commits that follow it. Note that this will not incorporate upstream changes into the feature branch.
If you want to re-write the entire feature using this method, the git merge-base command can be useful to find the original base of the feature branch. The following returns the commit ID of the original base, which you can then pass to git rebase:
This use of interactive rebasing is a great way to introduce git rebase into your workflow, as it only affects local branches. The only thing other developers will see is your finished product, which should be a clean, easy-to-follow feature branch history.
But again, this only works for private feature branches. If youre collaborating with other developers via the same feature branch, that branch is public, and youre not allowed to re-write its history.
There is no git merge alternative for cleaning up local commits with an interactive rebase.
In the Conceptual Overview section, we saw how a feature branch can incorporate upstream changes from main using either git merge or git rebase. 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 main.
This use of git rebase is similar to a local cleanup (and can be performed simultaneously), but in the process it incorporates those upstream commits from main.
Keep in mind that its perfectly legal to rebase onto a remote branch instead of main. This can happen when collaborating on the same feature with another developer and you need to incorporate their changes into your repository.
For example, if you and another developer named John added commits to the feature branch, your repository might look like the following after fetching the remote feature branch from Johns repository:
You can resolve this fork the exact same way as you integrate upstream changes from main: either merge your local feature with john/feature, or rebase your local feature onto the tip of john/feature.
Note that this rebase doesnt violate the Golden Rule of Rebasing because only your local feature commits are being movedeverything before that is untouched. This is like saying, add my changes to what John has already done. In most circumstances, this is more intuitive than synchronizing with the remote branch via a merge commit.
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.
If you use pull requests as part of your code review process, you need to avoid using git rebase after creating the pull request. As soon as you make the pull request, other developers will be looking at your commits, which means that its a public branch. Re-writing its history will make it impossible for Git and your teammates to track any follow-up commits added to the feature.
Any changes from other developers need to be incorporated with git merge instead of git rebase.
For this reason, its usually a good idea to clean up your code with an interactive rebase before submitting your pull request.
After a feature has been approved by your team, you have the option of rebasing the feature onto the tip of the main branch before using git merge to integrate the feature into the main code base.
This is a similar situation to incorporating upstream changes into a feature branch, but since youre not allowed to re-write commits in the main branch, you have to eventually use git merge to integrate the feature. However, by performing a rebase before the merge, youre assured that the merge will be fast-forwarded, resulting in a perfectly linear history. This also gives you the chance to squash any follow-up commits added during a pull request.
If youre not entirely comfortable with git rebase, you can always perform the rebase in a temporary branch. That way, if you accidentally mess up your features history, you can check out the original branch and try again. For example:
And thats all you really need to know to start rebasing your branches. 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.
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. Either option is perfectly valid, but at least now you have the option of leveraging the benefits of git rebase.
Visit link:
Merging vs. Rebasing | Atlassian Git Tutorial
- Versions of the Golden Rule in dozens of religions and other ... [Last Updated On: June 10th, 2016] [Originally Added On: June 10th, 2016]
- Rockwell's "Golden Rule" - Norman Rockwell Museum [Last Updated On: June 12th, 2016] [Originally Added On: June 12th, 2016]
- Versions of the Golden Rule in dozens of religions and ... [Last Updated On: June 12th, 2016] [Originally Added On: June 12th, 2016]
- Golden Rule - Wikipedia, the free encyclopedia [Last Updated On: June 12th, 2016] [Originally Added On: June 12th, 2016]
- Golden Rule (fiscal policy) - Wikipedia, the free encyclopedia [Last Updated On: June 12th, 2016] [Originally Added On: June 12th, 2016]
- Rockwell's "Golden Rule" - Norman Rockwell Museum [Last Updated On: June 16th, 2016] [Originally Added On: June 16th, 2016]
- Health insurance made simple | UnitedHealthOne [Last Updated On: June 19th, 2016] [Originally Added On: June 19th, 2016]
- Health insurance made simple | UnitedHealthOne [Last Updated On: June 19th, 2016] [Originally Added On: June 19th, 2016]
- The Golden Rule - harryhiker.com [Last Updated On: June 19th, 2016] [Originally Added On: June 19th, 2016]
- What is the Golden Rule? - GotQuestions.org [Last Updated On: June 21st, 2016] [Originally Added On: June 21st, 2016]
- Golden Rule Plumbing Heating & Cooling [Last Updated On: June 21st, 2016] [Originally Added On: June 21st, 2016]
- Golden Rule: Treat People as You'd Like to Be Treated [Last Updated On: June 25th, 2016] [Originally Added On: June 25th, 2016]
- What is the Golden Rule? (with pictures) - wiseGEEK [Last Updated On: June 25th, 2016] [Originally Added On: June 25th, 2016]
- World Scripture - The Golden Rule - Unification [Last Updated On: June 28th, 2016] [Originally Added On: June 28th, 2016]
- Golden Rule: Treat People as You'd Like to Be Treated [Last Updated On: June 28th, 2016] [Originally Added On: June 28th, 2016]
- Golden Rule - New World Encyclopedia [Last Updated On: June 28th, 2016] [Originally Added On: June 28th, 2016]
- Golden rule (law) - Wikipedia, the free encyclopedia [Last Updated On: June 28th, 2016] [Originally Added On: June 28th, 2016]
- Golden Rule - Robinsons Brewery [Last Updated On: June 29th, 2016] [Originally Added On: June 29th, 2016]
- Live by the Golden Rule (pledge/petition/proclamation ... [Last Updated On: June 29th, 2016] [Originally Added On: June 29th, 2016]
- The Golden Rule in World Religions - TeachingValues.com [Last Updated On: June 29th, 2016] [Originally Added On: June 29th, 2016]
- 18 Practical Tips for Living the Golden Rule : zen habits [Last Updated On: June 29th, 2016] [Originally Added On: June 29th, 2016]
- Golden Rule Plumbing Heating & Cooling [Last Updated On: June 29th, 2016] [Originally Added On: June 29th, 2016]
- Health insurance helps you manage your health care costs ... [Last Updated On: June 29th, 2016] [Originally Added On: June 29th, 2016]
- Upcoming Events Welcome the Golden Rule to Poulsbo ... [Last Updated On: August 16th, 2016] [Originally Added On: August 16th, 2016]
- UCF Golden Rule UCF [Last Updated On: August 23rd, 2016] [Originally Added On: August 23rd, 2016]
- VFP Golden Rule Project | Advancing VFP Opposition to ... [Last Updated On: September 2nd, 2016] [Originally Added On: September 2nd, 2016]
- Bible Verses About Golden Rule - King James Bible [Last Updated On: September 29th, 2016] [Originally Added On: September 29th, 2016]
- Why Choose UnitedHealthcare? - uhone.com [Last Updated On: October 13th, 2016] [Originally Added On: October 13th, 2016]
- Golden Rule Charter School [Last Updated On: November 6th, 2016] [Originally Added On: November 6th, 2016]
- Golden Rule Chronology - Gensler's Home Page [Last Updated On: November 6th, 2016] [Originally Added On: November 6th, 2016]
- The Golden Rule | Our Daily Bread [Last Updated On: November 6th, 2016] [Originally Added On: November 6th, 2016]
- Golden Rule Community CU [Last Updated On: November 21st, 2016] [Originally Added On: November 21st, 2016]
- Golden Rule - Wikipedia [Last Updated On: November 23rd, 2016] [Originally Added On: November 23rd, 2016]
- Golden LivingCenter Golden Rule [Last Updated On: December 9th, 2016] [Originally Added On: December 9th, 2016]
- The Golden Rule - Life, Hope & Truth [Last Updated On: December 9th, 2016] [Originally Added On: December 9th, 2016]
- Transition Probabilities and Fermi's Golden Rule [Last Updated On: January 11th, 2017] [Originally Added On: January 11th, 2017]
- Rockwell's "Golden Rule" - Norman Rockwell Museum - The Home ... [Last Updated On: January 13th, 2017] [Originally Added On: January 13th, 2017]
- Golden calf - Wikipedia [Last Updated On: January 15th, 2017] [Originally Added On: January 15th, 2017]
- Matriarchy - Wikipedia [Last Updated On: January 21st, 2017] [Originally Added On: January 21st, 2017]
- Opinion: No room for walls in Gospel's Golden Rule - The Catholic Register [Last Updated On: February 6th, 2017] [Originally Added On: February 6th, 2017]
- Rev. Jeff Bobin: The Golden Rule - GoErie.com [Last Updated On: February 6th, 2017] [Originally Added On: February 6th, 2017]
- No Gods, No Masters: Live the Golden Rule - Dissident Voice [Last Updated On: February 6th, 2017] [Originally Added On: February 6th, 2017]
- Let the Golden Rule be our response locally and globally - Berkeley Independent [Last Updated On: February 6th, 2017] [Originally Added On: February 6th, 2017]
- Good neighbors and the Golden Rule - WiscNews [Last Updated On: February 11th, 2017] [Originally Added On: February 11th, 2017]
- Shock on The Voice UK as coaches break a golden rule of the show and meet rejected Blind Audition singer Ciara Harvie - Radio Times [Last Updated On: February 12th, 2017] [Originally Added On: February 12th, 2017]
- Following the golden rule in times of tumult - College Heights Herald [Last Updated On: February 14th, 2017] [Originally Added On: February 14th, 2017]
- The Golden Rule of Business - Times-Citizen Communications [Last Updated On: February 15th, 2017] [Originally Added On: February 15th, 2017]
- A Fool's Golden Rule - Word and Way - Word and Way [Last Updated On: February 15th, 2017] [Originally Added On: February 15th, 2017]
- LETTER: Living by 'Golden Rule' would defeat ISIS - Daily Record - Daily Record [Last Updated On: February 17th, 2017] [Originally Added On: February 17th, 2017]
- h3cz: The Golden Rule of Twitter - Dexerto [Last Updated On: February 17th, 2017] [Originally Added On: February 17th, 2017]
- Motivating Your Team: Why The Golden Rule Doesn't Always Apply - Forbes [Last Updated On: February 17th, 2017] [Originally Added On: February 17th, 2017]
- Would golden rule survive if we were under siege? - Port Huron Times Herald [Last Updated On: February 20th, 2017] [Originally Added On: February 20th, 2017]
- Apply the Golden Rule to lift results - Business Management Daily [Last Updated On: February 22nd, 2017] [Originally Added On: February 22nd, 2017]
- United Way to present Golden Rule-Lightkeepers, Fabric of our Community Awards - Jacksonville Daily News [Last Updated On: February 23rd, 2017] [Originally Added On: February 23rd, 2017]
- 57th District It's the Golden rule: J. Central moves into 7th straight finals - The Independent [Last Updated On: February 23rd, 2017] [Originally Added On: February 23rd, 2017]
- From mushroom picker to deputy sheriff: Herc Avello relishes golden rule - Daily Local News [Last Updated On: February 24th, 2017] [Originally Added On: February 24th, 2017]
- The Golden Rule of Social Selling: Solve Your Customer's Problem - Business 2 Community [Last Updated On: February 24th, 2017] [Originally Added On: February 24th, 2017]
- Ron Forthofer: To live by the Golden Rule, we must recognize the 'others' - Longmont Times-Call [Last Updated On: February 24th, 2017] [Originally Added On: February 24th, 2017]
- Measuring life by the Golden Rule ... not a cellphone app - Bristol Herald Courier (press release) (blog) [Last Updated On: February 25th, 2017] [Originally Added On: February 25th, 2017]
- Apply the Golden Rule to immigrants - Statesman Journal [Last Updated On: February 28th, 2017] [Originally Added On: February 28th, 2017]
- Is the Golden Rule Still Alive and Well in America? - Huffington Post [Last Updated On: February 28th, 2017] [Originally Added On: February 28th, 2017]
- Five golden rules to always be in profit when you invest in equities - Economic Times [Last Updated On: March 1st, 2017] [Originally Added On: March 1st, 2017]
- Engineering's Golden Rule - Sourceable [Last Updated On: March 1st, 2017] [Originally Added On: March 1st, 2017]
- Email and The Golden Rule - Memphis Daily News - Memphis Daily News [Last Updated On: March 1st, 2017] [Originally Added On: March 1st, 2017]
- Golden Rule of Chaplaincy: Thy Shoes Matter - Boston.com [Last Updated On: March 4th, 2017] [Originally Added On: March 4th, 2017]
- Career Corner: The Golden Rule - Journal Record (subscription) [Last Updated On: March 4th, 2017] [Originally Added On: March 4th, 2017]
- Much to be gained from Golden Rule - Jackson Clarion Ledger [Last Updated On: March 5th, 2017] [Originally Added On: March 5th, 2017]
- OPINION: If only our institutions practiced the Golden Rule - Cape Cod Times (subscription) [Last Updated On: March 6th, 2017] [Originally Added On: March 6th, 2017]
- OPINION: If only our institutions practiced the Golden Rule - Nantucket Island Inquirer [Last Updated On: March 7th, 2017] [Originally Added On: March 7th, 2017]
- In the Neighborhood: A Meditation on the Golden Rule, Cheaters, and Prophets - Patheos (blog) [Last Updated On: March 11th, 2017] [Originally Added On: March 11th, 2017]
- The Golden Rule: Not So Golden Anymore | Issue 74 ... [Last Updated On: March 12th, 2017] [Originally Added On: March 12th, 2017]
- The Golden Rule | Our Daily Bread - odb.org [Last Updated On: March 12th, 2017] [Originally Added On: March 12th, 2017]
- Prairie Talk: Picture books teach us all - Herald & Review [Last Updated On: March 17th, 2017] [Originally Added On: March 17th, 2017]
- #3 Golden Rules for Entrepreneurs Starting Out Today - Entrepreneur [Last Updated On: March 17th, 2017] [Originally Added On: March 17th, 2017]
- A bowl of water and the golden rule - West Plains Daily Quill [Last Updated On: March 17th, 2017] [Originally Added On: March 17th, 2017]
- Hawaii judge upholds America's 'Golden Rule' - CNN [Last Updated On: March 17th, 2017] [Originally Added On: March 17th, 2017]
- Letter: If all could only follow the Golden Rule - Olean Times Herald [Last Updated On: March 19th, 2017] [Originally Added On: March 19th, 2017]
- 377A: Remember the golden rule in policymaking | TODAYonline - TODAYonline [Last Updated On: March 21st, 2017] [Originally Added On: March 21st, 2017]
- The Candlery keeps working partners busy - Muskogee Daily Phoenix [Last Updated On: March 23rd, 2017] [Originally Added On: March 23rd, 2017]
- Five golden rules to help solve your recycling dilemmas - The Conversation AU [Last Updated On: March 27th, 2017] [Originally Added On: March 27th, 2017]