Sobes.tech
Junior — Middle

What methods exist for undoing or rolling back changes after a commit in the Git version control system?

sobes.tech AI

Answer from AI

There are several ways to undo or revert changes after a commit in Git:

  • git revert <commit> — creates a new commit that undoes the changes of the specified commit. Safe for public branches.

  • git reset — moves the branch pointer to another commit:

    • git reset --soft <commit> — reverts commits, leaving changes in the staging area.
    • git reset --mixed <commit> — reverts commits, changes remain in the working directory but are removed from the index.
    • git reset --hard <commit> — reverts commits and deletes all changes from the index and working directory (dangerous, data may be lost).
  • git checkout or git restore — to undo changes in files before a commit.

The choice of method depends on whether you need to preserve history (use revert) or can rewrite history (reset), as well as whether the commit has been pushed to a shared repository.