I\'m new to git and I\'m trying to understand the difference between a squash and a rebase. As I understand it you perform a squash when doing a rebase.
问题:
回答1:
Both git merge --squash
and git rebase --interactive
can produce a \"squashed\" commit.
But they serve different purposes.
git merge --squash abranch
will produce a squashed commit on the destination branch, without marking any merge relationship.
(Note: it does not produce a commit right away: you need an additional git commit -m \"squash branch\"
)
This is useful if you want to throw away the source branch completely, going from (schema taken from SO question):
git checkout stable
X stable
/
a---b---c---d---e---f---g tmp
to:
git merge --squash tmp
git commit -m \"squash tmp\"
X-------------------G stable
/
a---b---c---d---e---f---g tmp
and then deleting tmp
branch.
git rebase --interactive
replays some or all of your commits on a new base, allowing you to squash (or more recently \"fix up\", see this SO question), going directly to:
git checkout tmp
git rebase -i stable
stable
X-------------------G tmp
/
a---b
If you choose to squash all commits of tmp
(but, contrary to merge --squash
, you can choose to replay some, and squashing others).
So the differences are:
merge
does not touch your source branch (tmp
here) and creates a single commit where you want.rebase
allows you to go on on the same source branch (stilltmp
) with:- a new base
- a cleaner history
回答2:
Merge commits: retains all of the commits in your branch and interleaves them with commits on the base branch
Merge Squash: retains the changes but omits the individual commits from history
Rebase: This moves the entire feature branch to begin on the tip of the master branch, effectively incorporating all of the new commits in master
More on here
回答3:
Merge squash merges a tree (a sequence of commits) into a single commit. That is, it squashes all changes made in n commits into a single commit.
Rebasing is re-basing, that is, choosing a new base (parent commit) for a tree. Maybe the mercurial term for this is more clear: they call it transplant because it\'s just that: picking a new ground (parent commit, root) for a tree.
When doing an interactive rebase, you\'re given the option to either squash, pick, edit or skip the commits you are going to rebase.
Hope that was clear!