In this tutorial we will merge several commits into one single commit
Navigate to your project directory:
cd path/to/project
View the commit history in a concise format:
git log --oneline
This command will display output like:
d5eb363 (HEAD) wip
323b361 wip
066d50c wip
f056912 feat(strapi-bridge): integrated openapi-fetch :rocket:
defad49 (upstream/main, origin/main, origin/HEAD, main) Merge pull request #13 from kilip/strapi
Identify how many commits you want to merge starting from (HEAD)
. For instance, if you wish to merge from feat(strapi-bridge)
to (HEAD) wip
, count the commits, which in this example would be 4.
Initiate an interactive rebase with the following command:
git rebase -i HEAD~4
This opens your Git editor, displaying something like:
pick f056912 feat(strapi-bridge): integrated openapi-fetch :rocket:
pick 24a1c6b wip
pick 8816256 wip
pick b0aa58b wip
# Rebase defad49..d5eb363 onto defad49 (4 commands)
Change pick
to s
(squash) for the commits you want to combine:
pick f056912 feat(strapi-bridge): integrated openapi-fetch :rocket:
s 24a1c6b wip
s 8816256 wip
s b0aa58b wip
# Rebase defad49..d5eb363 onto defad49 (4 commands)
After saving, your editor will show a message like this:
# This is a combination of 4 commits.
# This is the 1st commit message:
feat(strapi-bridge): integrated openapi-fetch :rocket:
# This is the commit message #2:
wip
# This is the commit message #3:
wip
# This is the commit message #4:
wip
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
Decide whether to keep or remove the commit messages. For example, to remove all wip
messages, you would edit it to:
# This is a combination of 4 commits.
# This is the 1st commit message:
feat(strapi-bridge): integrated openapi-fetch :rocket:
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
Save your changes and force push your commits to the remote repository:
git push origin main --force