Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - Difference between amend and squash commands

Tags:

What is the difference between amend and squash commands? I tried both and found that both are doing the same for proper management.

like image 743
Raju Guduri Avatar asked Jan 27 '16 17:01

Raju Guduri


People also ask

What is amend command in Git?

The git commit --amend command is a convenient way to modify the most recent commit. It lets you combine staged changes with the previous commit instead of creating an entirely new commit. It can also be used to simply edit the previous commit message without changing its snapshot.

What is squash command in Git?

To "squash" in Git means to combine multiple commits into one. You can do this at any point in time (by using Git's "Interactive Rebase" feature), though it is most often done when merging branches. Please note that there is no such thing as a stand-alone git squash command.

When should I use Git squash?

You use Git Squash to make it happen and then you merge it back to your default branch. This way, you'll achieve a simple and very straightforward tree. And if you need to roll back a version, you can easily do so by just resetting that squashed commit.

What is the difference between squash and rebase?

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.


1 Answers

In Git, commits are rarely actual destroyed, they just become orphans, or detached, meaning that they are not pointed to or reachable by a reference like a branch or tag.

"amending" and "squashing" are similar concepts though.

Typically, amending is a single commit operation in which you want to combine work that you have staged with your HEAD commit. This can be very convenient if you have just created a commit and realize that you need to add some content to it. Simply recall your commit command and use the --amend option.

Squashing is the more abstract term. I would say that an amend is a type of squash. Whenever you combine commits you could say that you are squashing them. If you have been working on a branch for a little while and have made 5 commits that taken together should be 1 commit, you can interactively rebase to squash them together.

There are several ways in Git to amend/squash, but they all center around the concept of organizing your commit history (which means re-writing the history of a branch) in this spirit of making it easier to grok.

like image 74
Jonathan.Brink Avatar answered Nov 15 '22 12:11

Jonathan.Brink