Squashing commits
Squashing commits in Git allows you to create a clean and concise commit history by combining multiple commits into a single, logical commit. This technique helps simplify your commit history, remove extraneous commits, and provide a clearer representation of the changes made during development.
In this part of our tutorial, we will explore the process of squashing commits in Git.
Go to the git-tutorial/tutorial7
directory you previously downloaded.
When you examine the history of this repository, it will look like the following:
We will squash commits from the issue1
branch into a single commit and then merge it into the main branch.
Switch to the main branch, and execute a merge with the --squash
option like below.
$ git checkout main
Switched to branch 'main'
$ git merge --squash issue1
Auto-merging sample.txt
CONFLICT (content): Merge conflict in sample.txt
Squash commit -- not updating HEAD
Automatic merge failed; fix conflicts and then commit the result.
Git will detect a conflict. We must manually resolve it in the sample.txt
file and commit the change.
$ git add sample.txt
$ git commit
[main 0d744a7] Conflicts: sample.txt
1 files changed, 4 insertions(+), 0 deletions(-)
We now have a new commit added to the main branch, which includes all of the commits in the issue1
branch.
You can verify the new change in the revision history using the git log command.