Git - Difference between amend and squash commands Git - Difference between amend and squash commands git git

Git - Difference between amend and squash commands


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.


amend only changes the last commit.

squash is like merging multiple commits into one single commit.

For only one commit, their effects may look alike.

https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History


let me explain it with some use-case:

suppose you just committed some changes, and now you realize you left some functionality incomplete. You start writing the code,now you are gonna create a new commit, better amend the changes to the last one.if you want to keep the message the same, pick the message using git log.else write git commit -m <your message> --amend.now your last commit went, and the new commit with the new message took the place.

Now Squash, If you have been working on some feature for a few days and keep committing the changes every day. On Completing the feature you need to push the feature. Then you should keep all the changes in one commit so that if there are some issues found with your committed code, It will be easy to identify and remove.

Go to git log take the count of your commits, then
git reset HEAD~<Number of commits>, Now just create a Commit and you are good to go.

[Tip: if you already pushed your last commit(s), use -f flag to forcefully pushing.]