DEV Community

Cover image for How to squash commit using #git reset --soft
ncutixavier
ncutixavier

Posted on

How to squash commit using #git reset --soft

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:

  1. 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.

  2. Open your terminal or command prompt and navigate to the repository's directory.

  3. 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>
Enter fullscreen mode Exit fullscreen mode

For example:

   git reset --soft abcdef12345
Enter fullscreen mode Exit fullscreen mode

This command moves the branch pointer to the specified commit while keeping your changes staged.

  1. Now, run the command:
   git commit
Enter fullscreen mode Exit fullscreen mode

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.

  1. Git will create a new commit with the updated message, combining the changes from the squashed commits.

  2. Optionally, you can push the changes to the remote repository using:

   git push origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

Replace <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.

Top comments (0)