Modify or Revert Commit

Modfiying A Git Commit

Git places restrictions on what type of modification we can make to commits already in the repository. Only the last commit in the respository can be altered. This is because every commit is linked to the previous commit via the SHA1 commit 1. When you alter a commit the SHA1 ID changes. This would cause a ripple in the history where altering one commit would wire altering any commits above it. The last commit can be alter because no other commits point to it yet. To modify the commit we can use the --amend argument.

Alter A Commit Message

If we just made a commit and would like to alter that commit's message. We can use the command below. This will change the commit 'message' of the last commit made to the repo. If we haven't staged any files since the last commit, only the the message will be altered.

git commit --amend -m "Altering commit message"

Alter Last Commit

If we've made a commit. And realized there was an error in the previous commit. We could fix the error, and re-stage the file. This is the same command used above.

#the last commit in the repo will be alter with the changes and message.
git commit --amend -m "Fix error in index html"

Undo The Last Commit

We can use the revert command to undo the last commit.
In this example, "second commit" was the last commit made. We would like to undo all the changes that commit made to the repo. Once we know it's commit ID the command  git revert can be use to undo the changes.

This reverts our working copy to the state of "first commit" and then commit it back to the repo automatically. After this command we will have a third commit in the repo.

git revert 929ad8a4169816f419

When the -n flag is used. The reverted changes are "staged" rather than being commit right away. Therefore this gives us a chance to do other things before committing.

git revert -n 929ad8a4169816f419

Undo multiple Commits

Cookbook Category: 

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.