History

Git Reset

Resetting Commits

Git reset reverts commits with soft or hard options.

What is Git Reset?

Git reset is a powerful command used in Git to undo changes in your working directory and staging area. It allows you to move the HEAD pointer to a specific commit, effectively changing the state of your project.

The Three Modes of Git Reset

Git reset can operate in three different modes: --soft, --mixed, and --hard. Each mode affects your working directory and staging area differently:

  • --soft: Moves the HEAD pointer to the specified commit and leaves the index and working directory unchanged.
  • --mixed (default): Moves the HEAD pointer and updates the index to match the specified commit, but leaves the working directory unchanged.
  • --hard: Moves the HEAD pointer and updates both the index and the working directory to match the specified commit.

Using Git Reset with Soft Option

The --soft option is useful when you want to undo a commit but keep your changes in the staging area for further adjustments or commits. Here is how you use it:

The above command moves the HEAD to the previous commit (HEAD~1) but keeps the changes in the staging area, allowing you to amend the commit or make additional changes.

Using Git Reset with Hard Option

The --hard option is used when you want to discard all changes in your working directory and staging area. This is how you can apply it:

Executing this command will move the HEAD to the previous commit and erase all changes in the working directory and staging area, effectively reverting your project to the specified commit.

When to Use Git Reset

Use git reset when you need to:

  • Undo a commit without losing changes (--soft).
  • Unstage changes without modifying the working directory (--mixed).
  • Completely discard changes (--hard).

Be cautious with --hard as it can result in data loss.

Previous
Reflog