Reputation: 716
I've been looking for an alternative solution for squashing a series of commits in a branch. What I've done in the past is to use git rebase -i HEAD~<#commits>
and then chose which commits to squash. Typically I pick
ed the latest commit, and squashed redundant commits in between.
This approach works reasonably well, but I was hoping for a faster way. I noticed that there is a --squash
option for git commit
. But I'm not sure how this works. What does it do exactly, and how do you use it? Would this be a solution for my problem?
Upvotes: 4
Views: 1804
Reputation: 95978
From the documentation:
--squash=<commit>
Construct a commit message for use with
rebase --autosquash
. The commit message subject line is taken from the specified commit with a prefix of "squash! ". Can be used with additional commit message options (-m
/-c
/-C
/-F
). See git-rebase[1] for details.
--squash
(also the --fixup
flags) takes a commit as an argument, and formats the message for use with --autosquash
.
It's possible to set rebase.autosquash = true
in your config to make the whole process shorter.
If you want to squash your last N
commits:
git reset --soft HEAD~N && git commit
This will put the head on HEAD~N
, so index and the working directory will not be altered at all, meaning that you can now use git commit
and create a single commit for all the changes.
Upvotes: 5
Reputation: 434
Consider you are working on feature branch where you have done say 15 commits. Now you are done with the task and want to merge it master. These 15 commits will make history of master branch long, better option is to squash them in single commit , give it meaningfull comment. Master history is clean and good for you also to review in future. Git merge master --sqash.
Now IDE also provide very good support for this option.
Upvotes: -2