How to merge commits in Git?
Sometimes, you commit something and realize it needs some more fixing, and then some more. Resulting in 3 or more commits for the same task.
You like to keep your
git log clean and want to merge your commits. Here is how to do just that:
Suppose we have this history:
$ git log --oneline 2750b94 Finally fix bug #84. da52aaa Fix bug #84. 31f5476 Fixing bug #84.
We enter rebasing with:
$ git rebase --interactive HEAD~3 # HEAD~3 says from HEAD include 3 commits
$EDITOR will open with:
pick 31f5476 Fixing bug #84. pick da52aaa Fix bug #84. pick 2750b94 Finally fix bug #84.
Update the file as follow:
pick 31f5476 Fixing bug #84. squash da52aaa Fix bug #84. squash 2750b94 Finally fix bug #84.
Then quit and save, you will be prompted to enter a new commit message.
Your new log history will be:
$ git log --oneline 38c0f13 Your new commit message about bug #84.