Skip to content

Instantly share code, notes, and snippets.

@kilip
Last active September 26, 2024 01:42
Show Gist options
  • Save kilip/e333952c30a73f077a868da7c3859c4e to your computer and use it in GitHub Desktop.
Save kilip/e333952c30a73f077a868da7c3859c4e to your computer and use it in GitHub Desktop.
Join several commits into one commit

Git Merge Commits Tutorial

In this tutorial we will merge several commits into one single commit

1. Change to Your Working Directory

Navigate to your project directory:

cd path/to/project

2. List Your Commits

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

3. Count Commits to Merge

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.

4. Start the Rebase

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)

5. Modify the Commit 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)

6. Save Changes

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.

7. Edit the Commit Message

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.

8. Finalize and Push Changes

Save your changes and force push your commits to the remote repository:

git push origin main --force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment