AndyE
AndyE

Reputation: 77

Git checkout certain commit does't seem to work

I want to checkout to a certain commit and build at that commit, but when I do git checkout <sha> ., it doesn't update the files and the log stays at the last commit(HEAD of branch).

If I want to build at a certain commit, do I need to make a new branch at that commit?

Am I doing something wrong or this is the default behaviour?

Do I need to reset the files after the git checkout <sha> .?

Upvotes: 1

Views: 735

Answers (2)

bk2204
bk2204

Reputation: 76694

git checkout can do several different things.

When you write git checkout <branch>, Git checks out the files in the working tree corresponding to the commit at the tip of that branch, and sets the branch to the one you specified (that is, HEAD is set to that branch).

When you write git checkout <sha> or git checkout <tag>, you're creating what's called a detached HEAD. In other words, the working tree is checked out to that commit and HEAD is updated, but there is no branch; if you create a commit at that point, it won't be on any branch. This is still helpful when you want to go to an older revision and look at things but not make any changes.

When you write git checkout <revision> <paths>, which is what you wrote here, you're asking Git to change the working tree and the index (the staging area) so that the files you've specified are the versions at that particular revision. In other words, by writing git checkout <sha> ., you've asked Git to change the working tree and index to completely match that commit, but you haven't changed where HEAD (the actual branch pointer) points to. Specifying certain paths can be a good way to pull specific old versions of files if you need to, but this probably isn't what you intended.

If you made a commit now, you'd be on whatever branch you're on, but you'd have all the files exactly as that were in the revision you specified, which means that you'd probably undo a lot of work. To undo this and get back to a sane state, do a git reset HEAD . and then a git checkout .. That will undo the changes to the working tree and leave you back with a clean working tree at the commit you were on.

To create and check out a new branch at a given revision, write git checkout -b <new-branch> <revision>. If you just want to create one at a given revision without checking it out, use git branch <new-branch> <revision>.

Upvotes: 1

rasengan__
rasengan__

Reputation: 977

I'm doing something wrong or this is the default behaviour?

I think that git checkout <sha1 of commit> . does not work. You have to use git checkout <sha1 of commit>, hence avoid the . at the end.

If I want to build at a certain commit I need to make a new branch at that commit

No, checking out works fine. In case you want to add a new feature, then creating a new branch is advised.

Best.

Upvotes: 1

Related Questions