Undoing Commits:
To undo commits in Git, you have several options depending on your specific needs and the state of your repository.
Undo the Last Commit (without changing files):
To undo the last commit but keep the changes in your working directory
git reset --soft HEAD~[number-of-commits]
is used.You can see that the commit is deleted but the unstaged file are there.
Undo the Last Commit and Discard Changes:
If you want to completely undo the last commit and discard all changes (both staged and unstaged)
git reset --hard HEAD~[number-of-commits]
is used.You can see that the commits are deleted and also the staged files are not there.
Reverting a Commit:
If you want to undo the changes introduced by a specific commit without altering the commit history,
git revert [commit-id]
is used.
Changing Commits:
If you want to make additional changes to the last commit (e.g., add forgotten files or modify the commit message), git commit --amend
is used.