Sobes.tech
Junior — Middle

What happens to the commit history in Git after executing the command git reset --hard?

sobes.tech AI

Answer from AI

The git reset --hard command changes the current branch and the state of the working directory to the specified commit, while:

  • Commit history: the branch pointer (HEAD) moves to the selected commit.
  • Working directory and index: are fully updated to match this commit.

If there were commits after this point (i.e., newer commits), they become inaccessible from the current branch and are considered "dangling." They are not immediately deleted but may be removed over time by Git's garbage collector.

Important: all unsaved changes in the working directory and index will be lost without recovery.

Example:

git reset --hard HEAD~1  # rollback one commit back, deleting changes

Thus, git reset --hard is a powerful tool for reverting the repository state but requires caution to avoid losing important data.