Git Reset

Git Reset

reset command is used when moving the repository back to a previous commit, regardless of any changes made after that commit.

Step 1: Find the previous commit

git revert

Step 2: Move the repository back to that step:

git reset

Git Reset Find Commit in Log:

Let’s search for the point we want to return to. To complete this task, we need to go through the log.

Using the –oneline option will help us avoid going through the long list, which gives just one line per commit showing:

  • The first seven characters of the commit hash –are used to refer to in our reset command.
  • the commit message.

Let’s find the point that we want to reset to:

[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 webhostguru/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/webhostguru/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 return to the commit: 9a9add8 (origin/master) Added .gitignore, the last one before we started to mess with things.

Git Reset

The git reset commithash resets our repository back to the specific commit  (commithash being the first 7 characters of the commit hash we found in the log):

Example

[user@localhost] $ git reset 9a9add8

Check the log again:

[user@localhost] $  git log –oneline

9a9add8 (HEAD -> master, origin/master) Added .gitignore

81912ba Corrected spelling error

3fdaa5b Merge pull request #1 from webhostguru/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/webhostguru/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!

Git Undo Reset

In spite of the commits no longer showing up in the log, it is not removed from Git.

Example:

[user@localhost] $ git reset e56ba1f

Check the log again:

[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 webhostguru/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/webhostguru/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

[user@localhost] $ 09f4acd Updated index.html with a new line

[user@localhost] $   221ec6e First release of Hello World!