Junior — Middle
How can you undo the last commit in a version control system?
sobes.tech AI
Answer from AI
You can undo the last commit in Git version control system in several ways, depending on whether the commit has already been pushed to a remote repository or not.
- If the commit has not been pushed:
git reset --soft HEAD~1
This will undo the last commit but keep the changes in the index (staging area), so you can modify or recommit them.
If you want to completely remove the commit and changes:
git reset --hard HEAD~1
- If the commit has already been pushed to a remote repository, it is better to use
git revert:
git revert HEAD
This creates a new commit that undoes the changes of the last commit, which is safe for collaborative work.
It is important to choose the method of undoing depending on the situation to avoid disrupting the repository history and colleagues' work.