Rewriting History with Git Rebase

http://code.tutsplus.com/tutorials/rewriting-history-with-git-rebase--cms-23191

1. Rebasing for a Linear History
The first use case we'll explore involves a divergent project history.

Consider a repository where your production branch has moved forward while you were developing a feature:

To rebase the feature branch onto the master branch, you would run the following commands:
git checkout feature
git rebase master

This transplants the feature branch from its current location to the tip of the master branch:

注意下图,rebase中的base指的是某一个commit


There are two scenarios where you would want to do this.
First, if the feature relied on the new commits in master, it would now have access to them.
Second, if the feature was complete, it would now be set up for a fast-forward merge into master.
In both cases, rebasing results in a linear history, whereas git merge would result in unnecessary merge commits.

有2种情况会使用到rebase

1.如果feature分支,依赖于master分支上的新提交一系列commit【将master合并到feature】

2.如果feature分支已经完成,那么就需要将feature通过fast-forward的方式合并到master【将feature合并到master】

======使用普通的合并,无法fast-forward,会生成多余的commit====

For example, consider what would happen if you integrated the upstream commits with a merge instead of a rebase:

git checkout feature
git merge master

This would have given us an extra merge commit in the feature branch.
What's more, this would happen every time you wanted to incorporate upstream commits into your feature.

Eventually, your project history would be littered with meaningless merge commits.

第一种情况【将master合并到feature】

This same benefit can be seen when merging in the other direction.
Without a rebase, integrating the finished feature branch into master requires a merge commit.
While this is actually a meaningful merge commit (in the sense that it represents a completed feature), the resulting history is full of forks:

第二种情况【将feature合并到master】

 ======使用普通的合并,无法fast-forward,会生成多余的commit====

When you rebase before merging, Git is able to fast-forward master to the tip of feature.
You'll find a linear story of how your project has progressed in the git log output—the commits in feature are neatly grouped together on top of the commits in master.
This is not necessarily the case when branches are tied together with a merge commit.

冲突的处理可以去原文看

原文地址:https://www.cnblogs.com/chucklu/p/4747833.html