To squash commits using
git reset --soft, you need to follow a few steps. Please note that squashing commits can rewrite the commit history, so be cautious and ensure you have a backup of your repository before proceeding.
Here's the process:
Identify the number of commits you want to squash. Suppose you have three commits with the following commit messages: Commit A, Commit B, and Commit C.
Open your terminal or command prompt and navigate to the repository's directory.
Execute the following command, replacing
<commit>with the commit hash of the commit preceding the commits you want to squash (in this example, it's Commit A):
git reset --soft <commit>
git reset --soft abcdef12345
This command moves the branch pointer to the specified commit while keeping your changes staged.
- Now, run the command:
This will open your default text editor to edit the commit message. By default, Git provides a message that includes the commit messages of all the squashed commits. You can modify this message to represent the squashed changes. Remove the lines corresponding to the commits you want to squash, save, and exit the editor.
Git will create a new commit with the updated message, combining the changes from the squashed commits.
Optionally, you can push the changes to the remote repository using:
git push origin <branch-name>
<branch-name> with the name of your branch.
That's it! You have successfully squashed the desired commits using
git reset --soft. Remember to be cautious while modifying commit history, especially if you are collaborating with others on the repository.