Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
724 views
in Technique[技术] by (71.8m points)

github - What does it mean to squash commits in git?

What does Squashing commits in git mean. How do I squash commits in Github?

I'm new to Git and I requested to be assigned to a newcomer bug in coala-analyzer. I fixed the bug, and now I was asked to squash my commits. How do I do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can think of Git as an advanced database of snapshots of your working directory(ies).

One very nice feature of Git is the ability to rewrite the history of commits.
The principal reason for doing this is that a lot of such history is relevant only for the developer who generated it, so it must be simplified, or made more nice, before submitting it to a shared repository.

Squashing a commit means, from an idiomatic point of view, to move the changes introduced in said commit into its parent so that you end up with one commit instead of two (or more).
If you repeat this process multiple times, you can reduce n commit to a single one.

Visually, if you started your work at the commit tagged Start, you want this

Git commits squashing

You may notice that the new commit has a slightly darker shade of blue. This is intentional.

In Git squashing is achieved with a Rebase, of a special form called Interactive Rebase.
Simplifying when you rebase a set of commits into a branch B, you apply all the changes introduced by those commits as they were done, starting from B instead of their original ancestor.

A visual clue

enter image description here

Note again the different shades of blue.

An interactive rebase let you choose how commits should be rebased. If you run this command:

 git rebase -i branch

You would end up with a file that lists the commits that will be rebased

 pick ae3...
 pick ef6...
 pick 1e0...
 pick 341...

I didn't name the commits, but these four ones are intended to be the commits from Start to Head

The nice thing about this list is that it is editable.
You can omit commits, or you can squash them.
All you have to do is to change the first word to squash.

 pick ae3...
 squash ef6...
 squash 1e0...
 squash 341...

If you close the editor and no merge conflicts are found, you end up with this history:

enter image description here

In your case, you don't want to rebase into another branch, but rather into a previous commit.
In order to transform the history as shown in the very first example, you have to run something like

git rebase -i HEAD~4

change the "commands" to squash for all the commits apart from the first one, and then close your editor.


Note about altering history

In Git, commits are never edited. They can be pruned, made not reachable, cloned but not changed.
When you rebase, you are actually creating new commits.
The old ones are not longer reachable by any refs, so are not shown in the history but they are still there!

This is what you actually get for a rebase:

enter image description here

If you have already pushed them somewhere, rewriting the history will actually make a branch!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...