📅  最后修改于: 2023-12-03 15:15:18.619000             🧑  作者: Mango
As a programmer, you may have come across the need to squash your last two commits in Git. Squashing is the process of combining multiple commits into a single commit. This can be useful when you want to clean up your Git history or when you want to make your changes more organized.
In this article, we will show you how to squash your last two commits in Git using the Shell-Bash command line.
Before squashing your last two commits, it is important to check your Git log to ensure that you are working with the correct commits. To do this, enter the following command in your terminal:
git log --oneline
This command will show you a list of your previous commits, along with their commit message and commit hash.
Once you have identified the commits that you want to squash, you can open Git Interactive Rebase by entering the following command:
git rebase -i HEAD~2
In this command, HEAD~2
means that you want to interactively rebase the last two commits.
After you have opened Git Interactive Rebase, you will see a list of your last two commits along with the word "pick" next to them. To squash these commits, change the word "pick" to "squash" on the second commit line, like so:
pick 1234567 commit message for first commit
squash abcdefg commit message for second commit
Save and close the file.
After you have squashed your commits, Git will prompt you to edit your commit message. You can do this by entering the following command:
git commit --amend
This will open a text editor where you can edit your commit message. After you have made your changes, save and close the file.
Once you have completed the previous steps, you can push your changes to the remote repository by entering the following command:
git push --force
This command will overwrite the previous commits with your newly squashed commit.
Congratulations! You have successfully squashed your last two commits in Git using the Shell-Bash command line.
Squashing your commits can help you keep your Git history organized and clean. Using the steps outlined in this article, you can easily squash your last two commits in Git using Shell-Bash. Remember to always check your Git log before making any changes to ensure that you are working with the correct commits.