Sobes.tech
Junior — Middle

How can you undo changes and revert to a previous commit state in git?

sobes.tech AI

Answer from AI

To undo changes and revert to a previous commit state in Git, you can use several commands depending on the situation:

  • git checkout -- <file> — cancels changes in the working directory for a specific file, returning it to the last commit state.
  • git reset --hard <commit> — resets the current branch to the specified commit, deleting all subsequent commits and changes in the working directory and index.
  • git revert <commit> — creates a new commit that undoes the changes of the specified commit, preserving history.

Example of reverting to the previous commit and deleting all changes:

git reset --hard HEAD~1

This example will move the branch back by one commit, deleting all changes after it.

How can you undo changes and revert to a previous… - sobes.tech