Git Reset
Reset is the command used to move the repository back to a previous commit, discarding any changes made after that commit.
Step 1: Identify the previous commit.
Step 2: Revert the repository to that commit.
After the previous chapter, we have a point in our commit history that we can return to. Let’s try to do that using the reset command.
Git Reset Find Commit in Log
First, we need to identify the point we want to return to in the commit history. To do this, we’ll look through the log.
To make the log easier to read, we’ll use the --oneline
option, which displays each commit on a single line, showing:
- The first seven characters of the commit hash, which we’ll use to reference in our reset command.
- The commit message.
Now, let’s find the commit we want to reset to.
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 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 21ec6e First release of Hello World!
|
We want to revert to the commit 9a9add8 (origin/master) Added .gitignore, which is the last one before we started making changes.
Git Reset
We reset our repository to the specific commit using git reset commithash (where commithash is the first 7 characters of the commit hash found in the log).
Example
[user@localhost] $
|
git reset 9a9add8
|
Now, let’s 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 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!
|
Warning: Altering the commit history of a repository can be risky. It’s generally safe to make these changes in your own local repository, but you should avoid rewriting history in remote repositories, especially if others are collaborating on them. |
Git Undo Reset
Although the commits no longer appear in the log, they have not been removed from Git.
If you know the commit hash, you can reset the repository to that commit. |
Example
[user@localhost] $
|
git reset e56ba1f
|
Now, let’s review 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 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!
|