Introduction
As a developer, one wants to keep the commits cleaner. At the same time, while developing, I also want to keep the detailed record of changes I made and commits I make. From the start to the end, depending on scope, I might have made many trivial commits and after I am happy with the changes, I would like a few (sometimes one while at other times multiple) commits to indicate what changed, rather than to have several commit messages.
Squash in GIT can be used to combine multiple individual commits into one. This makes commits in the repository cleaner. For illustration, let’s say that we have about 25 commits from the beginning through the end of development on an issue. During those 25 commits, several code changes were made which may not be of high importance to remain in the repository. Those 25 commits can be wrapped to a single commit before it can be merged into a master branch.
The rebase
command in the interactive mode (i.e., -i
or –interactive
) can be used to combine those comments into one.
I am using git bash
in this article and I have 25 commits on a branch which I want to modify to have just one commit reflecting a feature has been completed.
Git commit history
Rebasing
Run the following command to ensure that I am picking 25 commits in the branch I am in.
git rebase -i Head~25
Git Bash Window
The above command is instructed to get 25 commits from the head. Upon execution, the git opens a text editor showing the commit details (messages) of the last 25 commits.
Editor to mark commits as squash or pick
The editor will depend on what is set in the config. Vim, Nano or Notepad are the common choices. You can select an editor of your choice by modifying the setting in .gitconfig. Depending on the editor the screen-shot may appear different than what I have depicted here. Please see https://www.codeproject.com/Tips/5163724/Update-Editor-for-Git for tip to update the editor choice in git.
I can edit the commits by putting ‘Squash
’ on all the commits by changing ‘pick
’ to ‘squash
’ and leaving one commit with ‘pick
’, probably the first one in the list, i.e., the latest. We can change that message later on. In the example, I am picking the first one as shown in the figure:
Editor after marking the commits as squash or pick
Save the changes in the editor by clicking the save button on the top left and close by the window by clicking ‘x’ on top right.
After rebasing is completed, the branch will change to show the commit message that had ‘pick
’ in the editor.
Updating Commit Message
The commit message may not be relevant at this point and thus one may want to change the commit message. This can be achieved by:
git commit –amend
The above command will open an editor just like before showing all the previous commit messages that can be changed to relevant message and save and close the editor. After which git would have updated the message.
Handling Remote
If the branch was previously pushed to remote, we need to synchronize the commits to remote as well.
Since the branch appears different in remote, it might find conflicts. Thus, we must force the local branch which will replace the remote with the latest local changes by squash
.
Git push –force
With the rebase
, amend
and force
, I was able to combine the twenty-five smaller commits into one with one clear message, both in remote and local.
CodeProject