📜  git squash last 2 commits - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:15:18.619000             🧑  作者: Mango

Git Squash Last 2 Commits - Shell-Bash

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.

Step 1: Check Your Git Log

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.

Step 2: Open Git Interactive Rebase

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.

Step 3: Squash Your 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.

Step 4: Edit Your Commit Message

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.

Step 5: Push Your Changes

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.

Conclusion

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.