Sobes.tech
Junior — Middle

Can you list and explain the main types of git reset commands?

sobes.tech AI

Answer from AI

The git reset command is used to undo changes and move the HEAD pointer in Git. The main types of git reset differ in the level of impact on the index and working directory:

  1. git reset --soft <commit>

    • Moves HEAD to the specified commit.
    • Changes remain in the index and working directory.
    • Used when you want to undo commits but keep changes for recommitting.
  2. git reset --mixed <commit> (default)

    • Moves HEAD to the specified commit.
    • Changes are unstaged (removed from the index) but remain in the working directory.
    • Allows you to unstage changes without deleting them from files.
  3. git reset --hard <commit>

    • Moves HEAD to the specified commit.
    • Resets the index and working directory to the state of this commit.
    • All unsaved changes will be lost.

Example:

# Undo the last commit, keep changes in the index
git reset --soft HEAD~1

# Undo the last commit and unstage changes, but keep in files
git reset --mixed HEAD~1

# Completely revert the last commit and changes
git reset --hard HEAD~1

Be cautious when using --hard to avoid losing important data.