Merging the last four commits into one is a common task in version control using Git. This process is known as "squashing" commits:
-
Start an Interactive Rebase: Begin an interactive rebase for the last four commits.
git rebase -i HEAD~4
-
Edit the Rebase List: An editor will open with a list of the last four commits. The commits will be listed in the order they were made, from top (oldest) to bottom (most recent). It will look something like this:
pick <commit-hash-1> Commit message 1 pick <commit-hash-2> Commit message 2 pick <commit-hash-3> Commit message 3 pick <commit-hash-4> Commit message 4
Change the
pick
tosquash
(or simplys
) for the second, third, and fourth commits, like this:pick <commit-hash-1> Commit message 1 squash <commit-hash-2> Commit message 2 squash <commit-hash-3> Commit message 3 squash <commit-hash-4> Commit message 4
-
Save and Close the Editor: Save the changes and close the editor. This will start the rebase process, and Git will squash the specified commits into a single commit.
-
Edit the Commit Message: Another editor window will open, allowing you to edit the commit message for the new squashed commit. You can combine all the commit messages or write a new comprehensive message for the squashed commit.
-
Save and Close the Editor: Save the new commit message and close the editor.
-
Force Push (if necessary): If you have already pushed the original commits to a remote repository, you will need to force push the new squashed commit.
git push origin <branch-name> --force
-
Conflict Resolution: During the rebase, you might encounter conflicts. Resolve them and then continue the rebase with:
git rebase --continue