How can I squash my last X commits together into one commit using Git?
How can I squash my last X commits together into one commit using Git?
git rebase -i HEAD~X
The following content will be shown:
pick 1bffc15c My earlier commit
pick 474bf0c2 My recent commit
# ...
For the commits that you want to squash, replace pick with fixup, so it becomes:
pick 1bffc15c My earlier commit
fixup 474bf0c2 My recent commit
# ...
If it's open in vim (default interface within terminal), then press Esc
on your keyboard, type :wq
and Enter
to save the file.
Verify: Check git log
Simple one-liner that always works, given that you are currently on the branch you want to squash, master is the branch it originated from, and the latest commit contains the commit message and author you wish to use:
git reset --soft $(git merge-base HEAD master) && git commit --reuse-message=HEAD@{1}
What about an answer for the question related to a workflow like this?
merge --squash
after the PR, but the team thought that would slow down the process.)I haven't seen a workflow like that on this page. (That may be my eyes.) If I understand rebase
correctly, multiple merges would require multiple conflict resolutions. I do NOT want even to think about that!
So, this seems to work for us.
git pull master
git checkout -b new-branch
git checkout -b new-branch-temp
git checkout new-branch
git merge --squash new-branch-temp
// puts all changes in stagegit commit 'one message to rule them all'
git push
What can be really convenient:
Find the commit hash you want to squash on top of, say d43e15
.
Now use
git reset d43e15
git commit -am 'new commit name'
If you use TortoiseGit, you can the function Combine to one commit
:
Show Log
Combine to one commit
from the context menuThis function automatically executes all necessary single git steps. Unfortunatly only available for Windows.
git rebase -i HEAD^^
where the number of ^'s is X
(in this case, squash the two last commits)