Git Squash: Keep Clean of Your Git Commit History
Imagine, you are assigned to write a new feature of the main product. How do you start? You copy the source code, start coding for feature, write, test, write, fix bugs selfly explored, optimize code, fix typos or such minor task,s and time to time you have to commit your changes so that you don’t lose the valuable work you have done for the feature.
So, we can guess, you may have commits of test code, some type fixing commit, commit of missing comments etc. At last, you have completed your feature!
Wow! Great work! But wait! You want to have a look at your commit history and applied:
git log - -oneline
feature just modified //second one
feature modification 2 //..
feature final-done //final commit
You can see that we have some minor commits of the feature branch. Now, our goal is to make all the four feature commits to one commit.
We will use git-interactive way which is simple yet powerful.
Command format:
git rebase -i HEAD~N
i-> interactive
N-> number of commits starting from the current head
In your case, we got 4 commits to be merged. So apply this:
git rebase -i HEAD-4
you will see like this editor:
Notice that, the oldest commit is in the first position and then the newer ones.
So, when you are ready to squash commits, you have to replace the pick with s or squash except the oldest one from which you want to squash your commits. In this case, “feature created” is the oldest commit and we want to squash from this commit[including] to “feature final-done” commit, so our editor will be like this:
To edit: press “i” //vim instruction to insert
git merge feature
git log — oneline
Comments
Post a Comment