Git ~ Rebase - rohit120582sharma/Documentation GitHub Wiki

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 branch—they just do it in very different ways.

From a content perspective, rebasing is changing the base of your branch from one commit to another making it appear as if you'd created your branch from a different commit.

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. 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 Golden Rule of Rebasing

Don't rebase public history

The golden rule of git rebase is to never use it on public branches.

For example, think about what would happen if you rebased master onto your feature branch:


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.

So, before you run git rebase, always ask yourself, “Is anyone else looking at this branch?” If the answer is yes, start thinking about a non-destructive way to make your changes.


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 --force flag, 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.



⚠️ **GitHub.com Fallback** ⚠️