-1

Up to now I know two ways to address a commit in git:

  • via commit-hash
  • via name of a branch

Is there a way to address a commit-hash inside an existing branch?

If I do checkout a commit-hash then I am not on a branch.

If I checkout a branch, then I always get to the latest commit of this branch.

How to address a commit inside a branch?

guettli
  • 25,042
  • 81
  • 346
  • 663
  • Please read [ask] and share your research. If you checkout a specific commit that's not the tip of a branch, then you're in detached head state. You can successively create a _new_ branch. Is that what you want? If not, then what do you want and what have you tried? – CodeCaster May 30 '18 at 08:36
  • See [Fix a Git detached head?](https://stackoverflow.com/questions/10228760/fix-a-git-detached-head), [Git how to checkout a commit of a branch](https://stackoverflow.com/questions/42159715/git-how-to-checkout-a-commit-of-a-branch), and so on. – CodeCaster May 30 '18 at 08:37
  • The question doesn't make sense. Rather than just dismiss the answer you have, can you clarify what it is you are trying to do (ie. why is ending up in a detached head state undesirable for what you want to achieve)? Maybe you want to reset a branch to a specific commit, or maybe you're hoping you could checkout a branch using a commit hash instead of a name, and we could explain why you can't do that? – SpoonMeiser May 31 '18 at 07:58

2 Answers2

0

Use git branch <new-branch-name> <commit-hash> to create a new branch named <new-branch-name> that starts on the commit identified by <commit-hash>. You can then git checkout <new-branch-name> to start working on it.

Or you can combine the two commands above into a single command that does both operations in one step:

git checkout -b <new-branch-name> <commit-hash>

Read more about git checkout and git branch. Also, somehow related to a part of your question is the documentation page that explains the various ways to specify a Git revision or a range of revisions.

axiac
  • 68,258
  • 9
  • 99
  • 134
  • I want to checkout an existing branch. – guettli May 30 '18 at 14:17
  • A branch is a pointer to a commit. If you checkout a commit directly you get a [detached HEAD](https://git-scm.com/docs/gitglossary#gitglossary-aiddefdetachedHEADadetachedHEAD). Do you need to modify a past commit? – axiac May 30 '18 at 14:22
0

If you checkout a specific commit, you are not on a branch, and it wouldn't mean anything for you to be on a branch. It is not clear what you are actually trying to achieve.

One thing you can do, is create a new branch at the commit, and switch to that branch:

$ git branch my-new-branch <commit-hash>
$ git checkout my-new-branch

That would make your commit the head of a new branch and have it checked out.

Commits in git form a tree, and a branch is nothing more than a label pointing to one of those commits. A commit could be part of any number of branches, and in many situations it doesn't make sense to think of commits as belonging to a branch.

SpoonMeiser
  • 19,918
  • 8
  • 50
  • 68
  • I want to checkout an existing branch. – guettli May 30 '18 at 14:18
  • @guettli But you said you want to check out a specific commit. You can either check out a commit or a branch - what you're asking doesn't have any meaning. Perhaps if you explained why, or what you were trying to do, we might be able to help you? – SpoonMeiser May 30 '18 at 14:21