SlideShare a Scribd company logo
AshokKumarSatuluri
1 | P a g e
GIT Merging Vs Rebasing
Overview:
The first thing to understand about gitrebaseis that it solves the same problem as gitmerge.
Both of these commands are designed to integrate changes from one branch into another
branch—they 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 masterbranch with new commits. This results in a
forked history, which should be familiar to anyone who has used Git as a collaboration tool.
Now, let’s say that the new commits in master are relevant to the feature that you’re
working on. To incorporate the new commits into your featurebranch, you have two
options: merging or rebasing.
The Merge Option
The easiest option is to merge the master branch into the feature branch using something
like the following:
git checkout feature
git merge master
Or, you can condense this to a one-liner:
git merge master feature
This creates a new “merge commit” in the featurebranch that ties together the histories of
both branches, giving you a branch structure that looks like this:
AshokKumarSatuluri
2 | P a g e
Merging is nice because it’s 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 featurebranch will have an extraneous merge
commit every time you need to incorporate upstream changes. If master is very active, this
can pollute your feature branch’s history quite a bit. While it’s possible to mitigate this
issue with advanced gitlogoptions, it can make it hard for other developers to understand
the history of the project.
The Rebase Option
As an alternative to merging, you can rebase the featurebranch onto master branch using the
following commands:
git checkout feature
git rebase master
This moves the entire featurebranch to begin on the tip of the master branch, effectively
incorporating all of the new commits in master. 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.
AshokKumarSatuluri
3 | P a g e
The major benefit of rebasing is that you get a much cleaner project history. First, it
eliminates the unnecessary merge commits required by gitmerge. Second, as you can see in
the above diagram, rebasing also results in a perfectly linear project history—you can
follow the tip of featureall the way to the beginning of the project without any forks. This
makes it easier to navigate your project with commands like git log, gitbisect, and gitk.
But, there are two trade-offs for this pristine commit history: safety and traceability. If you
don’t 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 commit—you can’t see when upstream changes were
incorporated into the feature.
Interactive Rebasing
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 branch’s commit history. Typically, this is used to clean up a messy history
before merging a feature branch into master.
To begin an interactive rebasing session, pass the i option to the gitrebasecommand:
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. By
changing the pick command and/or re-ordering the entries, you can make the branch’s
AshokKumarSatuluri
4 | P a g e
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:
pick 33d5b7a Message for commit #1
fixup 9480b3d Message for commit #2
pick 5c67e61 Message for commit #3
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 feature’s history much easier to
understand. This is something that git mergesimply cannot do.
The Golden Rule of Rebasing
Once you understand what rebasing is, the most important thing to learn is when not to do
it. The golden rule of git rebaseis to never use it on public branches.
For example, think about what would happen if you rebased masteronto
your featurebranch:
AshokKumarSatuluri
5 | P a g e
The rebase moves all of the commits in master 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 master. Since rebasing results in brand new commits, Git will think that
your master branch’s history has diverged from everybody else’s.
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). Needless to say, this is a very
confusing situation.
So, before you run gitrebase, 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 gitrevertcommand). Otherwise, you’re safe
to re-write history as much as you like.
Force-Pushing
If you try to push the rebased master branch back to a remote repository, Git will prevent
you from doing so because it conflicts with the remote master branch. But, you can force the
push to go through by passing the --forceflag, like so:
# Be very careful with this command!
git push --force
This overwrites the remote master 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 you’re doing.
One of the only times you should be force-pushing is when you’ve performed a local
cleanup after you’ve pushed a private feature branch to a remote repository (e.g., for
backup purposes). This is like saying, “Oops, I didn’t really want to push that original
AshokKumarSatuluri
6 | P a g e
version of the feature branch. Take the current one instead.” Again, it’s important that
nobody is working off of the commits from the original version of the feature branch.
Workflow Walkthrough
Rebasing can be incorporated into your existing Git workflow as much or as little as your
team is comfortable with. In this section, we’ll take a look at the benefits that rebasing can
offer at the various stages of a feature’s development.
The first step in any workflow that leverages gitrebaseis to create a dedicated branch for
each feature. 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. 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 commits—you can fix it up after the
fact.
When calling git rebase, you have two options for the new base: The feature’s parent branch
(e.g., master), 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.
git checkout feature
git rebase -i HEAD~3
AshokKumarSatuluri
7 | P a g e
By specifying HEAD~3 as the new base, you’re not actually moving the branch—you’re just
interactively re-writing the 3 commits that follow it. Note that this will not incorporate
upstream changes into the featurebranch.
If you want to re-write the entire feature using this method, the gitmerge-basecommand can
be useful to find the original base of the featurebranch. The following returns the commit ID
of the original base, which you can then pass to git rebase:
git merge-base feature master
This use of interactive rebasing is a great way to introduce gitrebaseinto 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 you’re collaborating with other
developers via the same feature branch, that branch is public, and you’re not allowed to re-
write its history.
There is no gitmergealternative for cleaning up local commits with an interactive rebase.
IncorporatingUpstream Changes Into a Feature
In the Conceptual Overview section, we saw how a feature branch can incorporate upstream
changes from master using either gitmergeor gitrebase. 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.
This use of gitrebaseis similar to a local cleanup (and can be performed simultaneously),
but in the process it incorporates those upstream commits from master.
AshokKumarSatuluri
8 | P a g e
Keep in mind that it’s perfectly legal to rebase onto a remote branch instead of master. 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 featurebranch, your repository might look like the following after fetching the
remote featurebranch from John’s repository:
You can resolve this fork the exact same way as you integrate upstream changes
from master: either merge your local featurewithjohn/feature, or rebase your local featureonto
the tip of john/feature.
AshokKumarSatuluri
9 | P a g e
Note that this rebase doesn’t violate the Golden Rule of Rebasingbecause only your
local featurecommits are being moved—everything 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 gitpull command performs a merge, but you can force it to integrate the
remote branch with a rebase by passing it the --rebaseoption.
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
rebaseafter creating the pull request. As soon as you make the pull request, other
developers will be looking at your commits, which means that it’s 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 gitmergeinstead of git
rebase.
For this reason, it’s usually a good idea to clean up your code with an interactive
rebase before submitting your pull request.
AshokKumarSatuluri
10 | P a g e
Integratingan 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 usinggitmergeto integrate the feature into
the main code base.
This is a similar situation to incorporating upstream changes into a feature branch, but
since you’re not allowed to re-write commits in themaster branch, you have to eventually
use gitmergeto integrate the feature. However, by performing a rebase before the merge,
you’re 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.
AshokKumarSatuluri
11 | P a g e
If you’re not entirely comfortable with gitrebase, you can always perform the rebase in a
temporary branch. That way, if you accidentally mess up your feature’s history, you can
check out the original branch and try again. For example:
git checkout feature
git checkout -b temporary-branch
git rebase -i master
# [Clean up the history]
git checkout master
git merge temporary-branch
Summary
And that’s 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
rebaseinstead of gitmergewhen 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 gitmerge. Either option is perfectly
valid, but at least now you have the option of leveraging the benefits of gitrebase.

More Related Content

PPTX
Mastering GIT
PDF
Introduction to Git (part 2)
PPTX
Git hub abduallah abu nada
PDF
18 Git #burningkeyboards
PPTX
Techoalien git
PPTX
Git tutorial git branches 20131206-Bryan
PDF
Github git-cheat-sheet
PDF
Git Tricks
Mastering GIT
Introduction to Git (part 2)
Git hub abduallah abu nada
18 Git #burningkeyboards
Techoalien git
Git tutorial git branches 20131206-Bryan
Github git-cheat-sheet
Git Tricks

What's hot (20)

PDF
Git Developer Cheatsheet
PDF
Git workflows
PPTX
Git presentation
PDF
01 git interview questions & answers
PDF
Atlassian git cheatsheet
PDF
Git for developers
PDF
Git Series. Episode 2. Merge, Upstream Commands and Tags
PPTX
Git Branch
DOCX
Git github
PDF
Advanced Git
PDF
Git Tricks — git utilities that make life git easier
PPTX
Git undo
PPTX
Demystifying Git
PDF
Subversion to Git Migration
PPTX
Common Git Commands
PDF
Getting Started with Git
PDF
Git with the flow
PDF
Git flow for daily use
PPTX
Git tips
Git Developer Cheatsheet
Git workflows
Git presentation
01 git interview questions & answers
Atlassian git cheatsheet
Git for developers
Git Series. Episode 2. Merge, Upstream Commands and Tags
Git Branch
Git github
Advanced Git
Git Tricks — git utilities that make life git easier
Git undo
Demystifying Git
Subversion to Git Migration
Common Git Commands
Getting Started with Git
Git with the flow
Git flow for daily use
Git tips
Ad

Viewers also liked (20)

PPTX
Business Intellenge (BI),BI Presentation
PDF
Flipping the Aviation Classroom Learning Environment
PPTX
Expansindelislam 121110172607-phpapp02
DOCX
setting up a repository using GIT
PPT
Business plan
PDF
GonzalezCarlos CV-2016
PPTX
Presentación carlos alberto sandoval fdn - cci
PDF
El Diario de Girona - La inversió per cada català en medicines és de 183 euro...
PPTX
Are Rushed Privatizations Substandard? Analyzing Privatization under Fiscal P...
PDF
Prepara tu Escuela para la Sociedad Digital Claves para sumarse al cambio
PDF
EL ORDENADOR
PDF
Mintzberg managing the myths_of_health_care
DOC
And now these
PDF
Manual termopanel
PPT
Lysistrata
PPTX
Business Intellenge (BI)
PDF
[PlayPlanet] Muslim Friendly Korea Tour 2016
PDF
Economic consequences of changing fertility. Insights from an OLG model
PPTX
Motivación y personalidad
PPTX
E governance, Electronic Governance, Digital Governance, Digital Connection, ...
Business Intellenge (BI),BI Presentation
Flipping the Aviation Classroom Learning Environment
Expansindelislam 121110172607-phpapp02
setting up a repository using GIT
Business plan
GonzalezCarlos CV-2016
Presentación carlos alberto sandoval fdn - cci
El Diario de Girona - La inversió per cada català en medicines és de 183 euro...
Are Rushed Privatizations Substandard? Analyzing Privatization under Fiscal P...
Prepara tu Escuela para la Sociedad Digital Claves para sumarse al cambio
EL ORDENADOR
Mintzberg managing the myths_of_health_care
And now these
Manual termopanel
Lysistrata
Business Intellenge (BI)
[PlayPlanet] Muslim Friendly Korea Tour 2016
Economic consequences of changing fertility. Insights from an OLG model
Motivación y personalidad
E governance, Electronic Governance, Digital Governance, Digital Connection, ...
Ad

Similar to GIT Rebasing and Merging (20)

PDF
Git tutorial
PPTX
Git rewriting git history
PDF
Collaborative development with Git | Workshop
PDF
Git and Github slides.pdf
PDF
Git and Github workshop ppt slide by slide
PDF
Advanced Git - Functionality and Features
PPTX
Git Best Practices.pptx
PPTX
Git Branching and Merging.pptx
PDF
Git slides
PDF
Git Commands Every Developer Should Know?
PDF
devops-complete-notes-2.pdf
PDF
git-commands-cheat-sheet-infopediya-com.pdf
PDF
Git basics
PDF
Git basics a starter on git and its ecosystem
PDF
Git_tutorial.pdf
PDF
Mastering Git Worktree - Enhancing Your Version Control Workflow
PPTX
Git from the trenches
PPTX
Understanding Github and Version Control System.pptx
PPTX
Git 101 - An introduction to Version Control using Git
PDF
Introducing Git and git flow
Git tutorial
Git rewriting git history
Collaborative development with Git | Workshop
Git and Github slides.pdf
Git and Github workshop ppt slide by slide
Advanced Git - Functionality and Features
Git Best Practices.pptx
Git Branching and Merging.pptx
Git slides
Git Commands Every Developer Should Know?
devops-complete-notes-2.pdf
git-commands-cheat-sheet-infopediya-com.pdf
Git basics
Git basics a starter on git and its ecosystem
Git_tutorial.pdf
Mastering Git Worktree - Enhancing Your Version Control Workflow
Git from the trenches
Understanding Github and Version Control System.pptx
Git 101 - An introduction to Version Control using Git
Introducing Git and git flow

GIT Rebasing and Merging

  • 1. AshokKumarSatuluri 1 | P a g e GIT Merging Vs Rebasing Overview: The first thing to understand about gitrebaseis that it solves the same problem as gitmerge. Both of these commands are designed to integrate changes from one branch into another branch—they 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 masterbranch with new commits. This results in a forked history, which should be familiar to anyone who has used Git as a collaboration tool. Now, let’s say that the new commits in master are relevant to the feature that you’re working on. To incorporate the new commits into your featurebranch, you have two options: merging or rebasing. The Merge Option The easiest option is to merge the master branch into the feature branch using something like the following: git checkout feature git merge master Or, you can condense this to a one-liner: git merge master feature This creates a new “merge commit” in the featurebranch that ties together the histories of both branches, giving you a branch structure that looks like this:
  • 2. AshokKumarSatuluri 2 | P a g e Merging is nice because it’s 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 featurebranch will have an extraneous merge commit every time you need to incorporate upstream changes. If master is very active, this can pollute your feature branch’s history quite a bit. While it’s possible to mitigate this issue with advanced gitlogoptions, it can make it hard for other developers to understand the history of the project. The Rebase Option As an alternative to merging, you can rebase the featurebranch onto master branch using the following commands: git checkout feature git rebase master This moves the entire featurebranch to begin on the tip of the master branch, effectively incorporating all of the new commits in master. 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.
  • 3. AshokKumarSatuluri 3 | P a g e The major benefit of rebasing is that you get a much cleaner project history. First, it eliminates the unnecessary merge commits required by gitmerge. Second, as you can see in the above diagram, rebasing also results in a perfectly linear project history—you can follow the tip of featureall the way to the beginning of the project without any forks. This makes it easier to navigate your project with commands like git log, gitbisect, and gitk. But, there are two trade-offs for this pristine commit history: safety and traceability. If you don’t 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 commit—you can’t see when upstream changes were incorporated into the feature. Interactive Rebasing 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 branch’s commit history. Typically, this is used to clean up a messy history before merging a feature branch into master. To begin an interactive rebasing session, pass the i option to the gitrebasecommand: 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. By changing the pick command and/or re-ordering the entries, you can make the branch’s
  • 4. AshokKumarSatuluri 4 | P a g e 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: pick 33d5b7a Message for commit #1 fixup 9480b3d Message for commit #2 pick 5c67e61 Message for commit #3 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 feature’s history much easier to understand. This is something that git mergesimply cannot do. The Golden Rule of Rebasing Once you understand what rebasing is, the most important thing to learn is when not to do it. The golden rule of git rebaseis to never use it on public branches. For example, think about what would happen if you rebased masteronto your featurebranch:
  • 5. AshokKumarSatuluri 5 | P a g e The rebase moves all of the commits in master 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 master. Since rebasing results in brand new commits, Git will think that your master branch’s history has diverged from everybody else’s. 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). Needless to say, this is a very confusing situation. So, before you run gitrebase, 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 gitrevertcommand). Otherwise, you’re safe to re-write history as much as you like. Force-Pushing If you try to push the rebased master branch back to a remote repository, Git will prevent you from doing so because it conflicts with the remote master branch. But, you can force the push to go through by passing the --forceflag, like so: # Be very careful with this command! git push --force This overwrites the remote master 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 you’re doing. One of the only times you should be force-pushing is when you’ve performed a local cleanup after you’ve pushed a private feature branch to a remote repository (e.g., for backup purposes). This is like saying, “Oops, I didn’t really want to push that original
  • 6. AshokKumarSatuluri 6 | P a g e version of the feature branch. Take the current one instead.” Again, it’s important that nobody is working off of the commits from the original version of the feature branch. Workflow Walkthrough Rebasing can be incorporated into your existing Git workflow as much or as little as your team is comfortable with. In this section, we’ll take a look at the benefits that rebasing can offer at the various stages of a feature’s development. The first step in any workflow that leverages gitrebaseis to create a dedicated branch for each feature. 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. 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 commits—you can fix it up after the fact. When calling git rebase, you have two options for the new base: The feature’s parent branch (e.g., master), 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. git checkout feature git rebase -i HEAD~3
  • 7. AshokKumarSatuluri 7 | P a g e By specifying HEAD~3 as the new base, you’re not actually moving the branch—you’re just interactively re-writing the 3 commits that follow it. Note that this will not incorporate upstream changes into the featurebranch. If you want to re-write the entire feature using this method, the gitmerge-basecommand can be useful to find the original base of the featurebranch. The following returns the commit ID of the original base, which you can then pass to git rebase: git merge-base feature master This use of interactive rebasing is a great way to introduce gitrebaseinto 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 you’re collaborating with other developers via the same feature branch, that branch is public, and you’re not allowed to re- write its history. There is no gitmergealternative for cleaning up local commits with an interactive rebase. IncorporatingUpstream Changes Into a Feature In the Conceptual Overview section, we saw how a feature branch can incorporate upstream changes from master using either gitmergeor gitrebase. 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. This use of gitrebaseis similar to a local cleanup (and can be performed simultaneously), but in the process it incorporates those upstream commits from master.
  • 8. AshokKumarSatuluri 8 | P a g e Keep in mind that it’s perfectly legal to rebase onto a remote branch instead of master. 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 featurebranch, your repository might look like the following after fetching the remote featurebranch from John’s repository: You can resolve this fork the exact same way as you integrate upstream changes from master: either merge your local featurewithjohn/feature, or rebase your local featureonto the tip of john/feature.
  • 9. AshokKumarSatuluri 9 | P a g e Note that this rebase doesn’t violate the Golden Rule of Rebasingbecause only your local featurecommits are being moved—everything 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 gitpull command performs a merge, but you can force it to integrate the remote branch with a rebase by passing it the --rebaseoption. 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 rebaseafter creating the pull request. As soon as you make the pull request, other developers will be looking at your commits, which means that it’s 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 gitmergeinstead of git rebase. For this reason, it’s usually a good idea to clean up your code with an interactive rebase before submitting your pull request.
  • 10. AshokKumarSatuluri 10 | P a g e Integratingan 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 usinggitmergeto integrate the feature into the main code base. This is a similar situation to incorporating upstream changes into a feature branch, but since you’re not allowed to re-write commits in themaster branch, you have to eventually use gitmergeto integrate the feature. However, by performing a rebase before the merge, you’re 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.
  • 11. AshokKumarSatuluri 11 | P a g e If you’re not entirely comfortable with gitrebase, you can always perform the rebase in a temporary branch. That way, if you accidentally mess up your feature’s history, you can check out the original branch and try again. For example: git checkout feature git checkout -b temporary-branch git rebase -i master # [Clean up the history] git checkout master git merge temporary-branch Summary And that’s 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 rebaseinstead of gitmergewhen 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 gitmerge. Either option is perfectly valid, but at least now you have the option of leveraging the benefits of gitrebase.