Clone remote git repository until certain commit

Question:

I have a project stored in a repository and I need to clone it until a certain commit, I have tried to do it as follows:

git clone https://<usuario>@bitbucket.org/<repositorio> id_commit

But it has not worked. How can I clone a repository to a specific commit?

Answer:

Consider

We must bear in mind that when cloning a remote repository , the entire repository is downloaded , which may not necessarily limit the fact of returning to a specific commit .


How do we do it?

I am going to explain an example with a repository that I have created in .

First

We clone the repository:

git clone https://usuario@bitbucket.org/usuario/repositorio.git

Having done this, we will have a folder with the name of our repository.

cd repositorio/

Being inside said directory, we obtain the list of commits .

git log

Getting something like this:

commit a0f4ae240b8c786b85b90414ff89418d057f4528
Author: Fulano de tal <fulano.detal@gmail.com>
Date:   Mon Mar 27 11:50:27 2017 -0500

    Tercer Commit

commit 7a080414fff18f4a452c7885b9d08b5894e240b6
Author: Fulano de tal <fulano.detal@gmail.com>
Date:   Mon Mar 27 11:42:50 2017 -0500

    Segundo Commit

commit f18f4a4528b85b9d057a080414ff894e240bc786
Author: Fulano de tal <fulano.detal@gmail.com>
Date:   Mon Mar 27 11:40:53 2017 -0500

    Primer Commit

We must look at the commit value, the one that is a hash . This is the identifier of the commit .

Then

It is enough to obtain the identifier of the commit to which we want to return our code (eg: 7a080414fff18f4a452c7885b9d08b5894e240b6 ) , in this case, for our second commit. And we execute the following command:

git checkout 7a080414fff18f4a452c7885b9d08b5894e240b6

And voila!

Our repository is back to how it was when the commit was made.

Scroll to Top