How Can I Merge Multiple Commits onto Another Branch as a Single Squashed Commit?
To merge multiple commits from one branch onto another branch as a single squashed commit, you can use the following steps. This process typically involves creating a new branch, performing an interactive rebase to squash the commits, and then merging the squashed commit into the target branch.
1. Check Out a New Branch from the Source Branch
First, check out a new branch from the branch that contains the commits you want to squash. This step ensures you don't accidentally alter the original branch.
git checkout -b temp-branch source-branch
Replace source-branch
with the name of the branch containing the commits you want to squash.
2. Perform an Interactive Rebase to Squash Commits
Start an interactive rebase to squash the commits. You'll specify the base commit before the commits you want to squash. For example, if you want to squash the last 3 commits, you would use HEAD~3
.
git rebase -i HEAD~3
An editor will open with a list of commits. Change the pick
commands to squash
(or s
) for all commits you want to squash except the first one.
pick 1234567 Commit message 1
squash 2345678 Commit message 2
squash 3456789 Commit message 3
Save and close the editor. Another editor window will open, allowing you to edit the commit message for the squashed commit. Save and close the editor to complete the rebase.
3. Check Out the Target Branch
Check out the branch you want to merge the squashed commit into.
git checkout target-branch
Replace target-branch
with the name of the branch you want to merge into.
4. Merge the Squashed Commit
Merge the squashed commit from your temporary branch into the target branch.
git merge --squash temp-branch
This command prepares the merge, but doesn't create a commit yet. To complete the merge, you'll need to commit the changes.
git commit -m "Merged commits from source-branch as a single squashed commit"
5. Clean Up
Finally, you can delete the temporary branch if you no longer need it.
git branch -d temp-branch
Summary
Create a temporary branch:
git checkout -b temp-branch source-branch
Perform an interactive rebase to squash commits:
git rebase -i HEAD~3
Check out the target branch:
git checkout target-branch
Merge the squashed commit:
git merge --squash temp-branch git commit -m "Merged commits from source-branch as a single squashed commit"
Delete the temporary branch:
git branch -d temp-branch
By following these steps, you can merge multiple commits from one branch onto another branch as a single squashed commit, keeping your commit history clean and concise.
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github