Many times, I found myself having to delete Git repositories as part of my daily duties. Following some of the documentation online, I tried many times getting frustrated ..
git reset is the command responsible for the undo. It will undo your last commit while leaving your working tree (the state of your files on disk) untouched. You'll need to add them again before you can commit them again.
# This will destroy any local modifications.# Don't do it if you have uncommitted work you want to keep.
git reset --hard HEAD~
# Alternatively, if there's work to keep (stash your changes before):
git stash -u
git reset --hard HEAD~
git stash pop
# This saves the modifications, then reapplies that patch after resetting.# You could get merge conflicts if you've modified things which were# changed since the commit you reset to.
git add anything that you want to include in your new commit.
Commit the changes, reusing the old commit message. reset copied the old head to .git/ORIG_HEAD; commit with -c ORIG_HEAD will open an editor, which initially contains the log message from the old commit and allows you to edit it. If you do not need to edit the message, you could use the -C option.
Alternatively, to edit the previous commit (or just its commit message), commit --amend will add changes within the current index to the previous commit.