Curriculum
Course: Git
Login
Text lesson

Git Revert

Git Revert

Revert is the command used to create a new commit that undoes the changes from a previous commit, preserving the commit history.

Step 1: Locate the previous commit:

img_revert_part11

Step 2: Use it to create a new commit.

img_revert_part22

Let’s create a new commit where we have “accidentally” deleted a file:

Example

[user@localhost] $

git commit -m “Just a regular update, definitely no accidents here…”
[master 16a6f19] Just a regular update, definitely no accidents here…
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 img_hello_git.jpg

Now we have a point in our commit history we want to return to. Let’s use revert to accomplish that.

Git Revert Find Commit in Log

First, we need to locate the commit we want to return to by examining the log.

To simplify this, use the –oneline option to display a concise list with one line per commit, showing:

  • The first seven characters of the commit hash
  • The commit message

Let’s find the commit we want to revert to:

Example

[user@localhost] $

git log –oneline
52418f7 (HEAD -> master) Just a regular update, definitely no accidents here…
9a9add8 (origin/master) Added .gitignore
81912ba Corrected spelling error
3fdaa5b Merge pull request #1 from code7school-test/update-readme
836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches
daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta
facaeae (gh-page/master) Merge branch ‘master’ of https://github.com/code7school-test/hello-world
e7de78f Updated index.html. Resized image
5a04b6f Updated README.md with a line about focus
d29d69f Updated README.md with a line about GitHub
e0b6038 merged with hello-world-images after fixing conflicts
1f1584e added new image
dfa79db updated index.html with emergency fix
0312c55 Added image to Hello World
09f4acd Updated index.html with a new line
221ec6e First release of Hello World!

We want to revert to the previous commit: 52418f7 (HEAD -> master) Just a regular update, definitely no accidents here…. This commit is the latest one in the history.

Git Revert HEAD

We revert the latest commit using git revert HEAD to undo the most recent change and create a new commit. Add the –no-edit option to skip the commit message editor and use the default revert message.

Example

[user@localhost] $

git revert HEAD –no-edit
[master e56ba1f] Revert “Just a regular update, definitely no accidents here…”
 Date: Thu Apr 22 10:50:13 2021 +0200
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 img_hello_git.jpg

Let’s check the log once more:

Example

[user@localhost] $

git log –oneline
e56ba1f (HEAD -> master) Revert “Just a regular update, definitely no accidents here…”
52418f7 Just a regular update, definitely no accidents here…
9a9add8 (origin/master) Added .gitignore
81912ba Corrected spelling error
3fdaa5b Merge pull request #1 from w3schools-test/update-readme
836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches
daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta
facaeae (gh-page/master) Merge branch ‘master’ of https://github.com/code7school-test/hello-world
e7de78f Updated index.html. Resized image
5a04b6f Updated README.md with a line about focus
d29d69f Updated README.md with a line about GitHub
e0b6038 merged with hello-world-images after fixing conflicts
1f1584e added new image
dfa79db updated index.html with emergency fix
0312c55 Added image to Hello World
09f4acd Updated index.html with a new line
221ec6e First release of Hello World!

Note: To revert to earlier commits, use git revert HEAD~x, where x is the number of commits to go back (e.g., 1 for one commit earlier, 2 for two commits earlier, etc.).

On the next page, we’ll cover git reset, which restores the repository to an earlier state in the commit history without creating a new commit.