Thursday, August 13, 2020

Git: Reverting git commit

Every developer making use of git from the command-line has mistakenly invoked "git commit". The "git commit" command commits staged changes to the local repository. In a previous post it was demonstrated how to rollback a git add (Git: Reverting git add).

A typical git commit takes the following form of a git add (with some command-line) followed by a git commit -m:
git add .
git commit -m "some message related to the commit" 

The following undoes the last commit and leaves the state of your files on disk unchanged (meaning your working tree is unchanged). The changes previously staged by the commit are reverted to unstaged:
git reset HEAD~ 

After invoking "git reset HEAD~" the files on disk can be modified as needed and the git add and git commit commands can be invoked where "git commit -c" will commit using the previous commit message text (as in "some message related to the commit" from the example message above):
git add .
git commit -c ORIG_HEAD

To edit the original commit message use the following sequence of commands:
git add .
git commit -c ORIG_HEAD

When "git commit -c ORIG_HEAD" is invoked an editor will display the log message from the previous commit and the message associated with the previous commit can be edited.

No comments :

Post a Comment