There may come a time when mistakes are made and something needs to be removed or undone. This section will cover some of the ways these errors can be fixed.
This is one of the few areas in Git where data can be lost forever. If an undo is performed and then discovered it should not have been, it is highly likely that it is now impossible to recover the lost data. Proceed with caution.
This occurs most often when a commit is pushed too early, committing something that is not yet ready, or making a mistake in the commit message. This can be fixed by committing over the top of the latest commit using the --amend option.
$ git commit --amend
If the files on the staged status are different from those in the latest commit, the commit will run normally except it will override the original. If the files are the same, then the option will be provided to change the commit message, again, overriding the previous commit.
It is also possible to unstage a staged file. This can sometimes be required if git add * was used when the intention was to have two (or more) separate commits. The git status command provides a hint on how to do this as well:
# (use "git reset HEAD <file>..." to unstage)
So to follow its advice, use the command git reset HEAD filename and the file is now reverted to the modified status rather than the staged status.
To revert a file back to what it looked like at the last commit, the git status command comes to the rescue again in the unstaged status:
# (use "git checkout -- <file>..." to discard changes in working directory)
Following these instructions with the git checkout -- filename reverts the file. To reiterate the above warning however, this will cause data loss; only use it when it is certain this version of the file is no longer wanted.