To squash means combine. With the help of squash you can combine last X commits in your git repo.
Lets understand this through one simple example
Create 4 files fileA, fileB, fileC, fileD in any useless git directory using
touch fileA fileB fileC fileD
If you check git status
you can see these files newly created.
Now add files as follow
git add fileA
git commit -m "fileA added"
git add fileB
git commit -m "fileB added"
git add fileC
git commit -m "fileC added"
git add fileD
git commit -m "fileD added"
If you do git log
, you can see commit messages similar to below :
last commit - fileD added
2nd last commit - fileC added
3rd last commit - fileB added
4th last commit - fileA added
Now To squash 2nd , 3rd and 4th last commit. You can do as follow:
git rebase -i HEAD~4
You can see something like this (observe that order reverse compare to git log output):
pick d16e7b5 fileA added
pick 221b175 fileB added
pick 8006a22 fileC added
pick 4fb6454 fileD added
4th last commit is shown at top, then 3rd last and so on.
Our goal is to squash 2nd , 3rd and 4th last commit. For that you can to word pick with squash as follow:
pick d16e7b5 fileA added
squash 221b175 fileB added
squash 8006a22 fileC added
pick 4fb6454 fileD added
Once you save this you can find 2nd, 3rd and 4th last commit will be combined(squashed). In above case you will never need to use squash for d16e7b5 because that is first commit in rebase there is no previous commit you have provided.
git log
will show you :
last commit - fileD added
2ns last commit - "
FileA added
FileB added
FileC added"
I suspect that you must be using squash at first commit that's why it's not working
Note : Following case always give you error if you try in this example.
squash d16e7b5 fileA added
pick 221b175 fileB added
pick 8006a22 fileC added
pick 4fb6454 fileD added