Return a repository to a specific commit

22

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 e
  • commit f
  • commit g
  • commit h "Current version"

Is there a way to go back to "commit c"?

    
asked by jasilva 21.12.2015 в 17:56
source

2 answers

19

You should only do git checkout commitC where commitC is the hash of that commit.

Example:

You look in the git log for the commit you want to return to:

git log

You'll have something like this:

commit eb9b03c2e22305c965b610aa84a7d316e5cb208d
Author: oscar <oscar@oscar.(none)>
Date:   Thu Sep 19 16:05:51 2013 +0200

    commit3

commit 96046152e2515d651d777bac66602e4a50654f49
Author: oscar <oscar@oscar.(none)>
Date:   Thu Sep 19 00:13:37 2013 +0200

    commit2

commit 23abd7d49c00541fcac17acb1babd7a60a3e7c6e
Author: oscar <oscar@oscar.(none)>
Date:   Wed Sep 18 23:53:25 2013 +0200

    commit1 
  

You can see more information about git log here.

Then you do this:

git checkout eb9b03c

  

For more information on git checkout you can see here.

Then your HEAD will be pointing to the commit3 , being the result you need.

    
answered by 21.12.2015 / 17:57
source
11

Solution

Just execute the following command (replace <hash> ):

$ git checkout <hash>

Explanation

In order to make the jump from one confirmation (commit) to another, you must know the number (hash) by which it was registered. Taking into account that the official book of Git in Spanish, section 2.3 Fundamentals of Git - Viewing the confirmation history , tells us:

  

After having made several confirmations, or if you have cloned a repository that already had a history of confirmations, you probably want to look back to see what modifications have been made. The most basic and powerful tool to do this is the git log command.

When you execute the git log command on your project, you will have something like this:

$ git log
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <[email protected]>
Date:   Mon Mar 17 21:52:11 2008 -0700

    Cambiado el número de versión

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon <[email protected]>
Date:   Sat Mar 15 16:40:33 2008 -0700

    Removido código de prueba innecesario

commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon <[email protected]>
Date:   Sat Mar 15 10:31:28 2008 -0700

    Primera confirmación

The hash (or confirmation 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 take care of moving the pointer HEAD of your project to the indicated confirmation. Visual example (taking into account that you are the project is the branch master ):

In this way we can perceive that although your pointer now points to a previous capture, your subsequent changes to it still exist.

You can get more official information and in Spanish related to this topic in the following link: 6.1 Git tools - Selection of confirmations of specific changes

  

Short SHA : Simply by giving it the first characters of the SHA-1 code, Git is smart enough to figure out what the desired commit (commit) is. It is necessary to type at least 4 characters and these must be unambiguous --ie, there must be a single object in the repository whose code starts with that initial piece of SHA -.

    
answered by 21.12.2015 в 20:35