Open In App

Practical Uses Of 'git reset --soft'?

Last Updated : 03 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

With the git reset --soft command, you can roll back your branch to a particular commit while maintaining the changes made to the working directory and index (staging area), which sets it apart from other reset modes.

In this article, we will see some of the practical uses of git reset--soft.

Key Characteristics of git reset --soft:

  • Preserves Working Directory: The changes in your working directory remain untouched.
  • Preserves Staging Area: Changes that were staged remain staged after the reset.
  • Rewinds Commit History: The branch pointer is moved back to a specified commit, removing subsequent commits from the history.

Approach 1: Amend a Recent Commit:

One of the most common uses of git reset --soft is to amend recent commits without losing the changes. Suppose you have committed changes but later realized that you want to modify the commit message or add more changes to the commit. You can use git reset --soft to unstage the commit while keeping the changes intact.

Step 1: Run git reset --soft HEAD~1

This command will reset your branch to the previous commit (HEAD~1), but your changes will still be staged.

git reset --soft HEAD~1

Step 2: Amend the Commit Message

Use git commit --amend to create a new commit with the correct message.

git commit --amend

This opens an editor where you can modify the commit message.

first
Amend the Commit Message


Step 3: Push the Changes (if necessary)

If you've already pushed the incorrect commit, you'll need to force-push to overwrite it on the remote branch.

git push origin branch-name --force
Screenshot-2024-09-01-114108
Output

Approach 2: Remove Unnecessary Commits

If you've made some experimental commits and want to remove them without losing the changes:

Step 1: Run git reset --soft <commit_hash>

Replace <commit_hash> with the hash of the commit you want to reset to. This will remove all commits after the specified commit while retaining all changes.

git reset --soft abc1234

Step 2: Make a New Commit or Modify Changes:

You can choose to either make a new commit or adjust the changes before committing.


Next Article
Article Tags :

Similar Reads