What's the difference between “squash” and “fi

2019-01-30 21:15发布

问题:

I've been using Git Extensions for a while now (it's awesome!) but I haven't found a simple answer to the following:

Sometimes, when typing a commit message, a make a typo. My friend showed me how to fix it the following way (in Git Extentions):

Right-Click on the commit > Advanced > Fixup commit

Then I simply check the box "Amend" and rewrite my message and voila! My commit message is fixed.

However this other option "Squash commit"... I have always wondered what it does?!

My question is:

Would someone simply explain me what is the exact difference between Squash commit and Fixup commit in Git/Git Extentions? They look kind of... "similar" to me:

回答1:

I do not know what Git Extensions does with it specifically, but git rebase has an option to automatically squash or fixup commits with squash! or fixup! prefixes, respectively:

   --autosquash, --no-autosquash
       When the commit log message begins with "squash! ..." (or "fixup!
       ..."), and there is a commit whose title begins with the same ...,
       automatically modify the todo list of rebase -i so that the commit
       marked for squashing comes right after the commit to be modified,
       and change the action of the moved commit from pick to squash (or
       fixup).

The difference between squash and fixup is that during the rebase, the squash operation will prompt you to combine the messages of the original and the squash commit, whereas the fixup operation will keep the original message and discard the message from the fixup commit.



回答2:

Simply put, when rebasing a series of commits, each commit marked as a squash, gives you the opportunity to use its message as part of a pick or reword commit message.

When you use fixup the message from that commit is discarded.



回答3:

From git-rebase doc:

If you want to fold two or more commits into one, replace the command "pick" for the second and subsequent commits with "squash" or "fixup". If the commits had different authors, the folded commit will be attributed to the author of the first commit. The suggested commit message for the folded commit is the concatenation of the commit messages of the first commit and of those with the "squash" command, but omits the commit messages of commits with the "fixup" command.



回答4:

I tinkered with git extensions and couldn't get it to squash many commits into one. To do that, I had to resort to the command line and found this post helpful

git rebase -i Head~2

This is interactive rebase, and note the following:

  • ~2 here refers to how many commits you want to involve in this operation, including the current head
  • You have to edit the subsquent interactive edit window, leave the first item as "pick" and replace subsequent lines with "squash". The instructions in the link above are much clearer if this is opaque.