Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to checkout a specific commit from git

Tags:

git

bitbucket

My code isn't working and it seems I have merged a wrong piece of code in my master branch. I am unable to pin point the issue and thus I want to pull specific commits one by one to find out which version was working just before the wrong commit.

How could I pull a specific commit? Eg, On Bitbucket, I see a merge with id 9ce920d on branch test but I don't know how to pull this merge.

like image 823
Manu Chadha Avatar asked Jun 20 '18 07:06

Manu Chadha


2 Answers

The question has wrong terminology. You cannot pull a commit, you can only checkout a commit. Pulling a commit would defy the whole branch/commit structure saving memory. You pull all commits in a branch or repository, and if you want to check out a specific one, then well, check it out:

git checkout 9ce920d

You will be in headless mode (no pointer to the commit, even if you have branches pointing to them - you have to check them out explicitly!), so you may want to create a branch if you want to mess around:

git checkout -B mess_around_branch

For posterity, OP actually wanted to discard all commits from the latest commit on their branch to a specific commit before. To do this from your branch hit:

git reset 9ce920d

which will discard all commits, but leave the files as they were, allowing you to decide if you wish to retain some of the changes. If you really want to lose everything:

git reset --hard 9ce920d
like image 192
kabanus Avatar answered Oct 19 '22 08:10

kabanus


git checkout <id>

should do the trick. Where 'id' is the specific commit you want to get.

like image 23
Neli Avatar answered Oct 19 '22 07:10

Neli