Based on How can I undo the last commit in Git? .
Is there a way to return a Git repository to a specific commit?
For example:
- commit to "First commit"
- commit b
- commit c
- commit d
- commit and
- commit f
- commit g
- commit h "Current version"
Is there a way to return to "commit c"?
You just have to do
git checkout commitC
wherecommitC
is the hash of said commit.Example:
You look in the git log for the commit you want to return to:
git log
You will have something like this:
Then you do this:
git checkout eb9b03c
Then you
HEAD
will be pointing to thecommit3
, being the result you need.Solution
Just run the following command (replace
<hash>
):Explanation
To be able to jump from one confirmation (commit) to another, you must know the number (hash) by which it was registered. Taking into account that the official Git book in Spanish, section 2.3 Git Basics - Viewing the commit history , tells us:
When executing the command
git log
on your project, you will have something similar to this:The hash (or commit number) will be the alphanumeric set that is followed by the word commit . You will copy this hash to use it with the command
git checkout <hash>
that will be in charge of moving the HEAD pointer of your project to the indicated commit. Visual example (taking into account that you are the project is the branchmaster
):In this way we can perceive that although your pointer now points to a previous capture, your changes after it still exist.
You can obtain more official information and in Spanish related to this topic in the following link: 6.1 Git tools - Selection of specific change commits