git checkout -b <branchname> <commit>
This command is useful for starting a new line of development from a particular commit, allowing you to create and immediately switch to a new branch that begins at the specified commit rather than the current HEAD.
It works by creating a new branch named <branchname>
at the commit hash or reference <commit>
, and then checking out that branch so you can start working from that point in the project history.
You can use any valid commit reference for <commit>
, such as a commit hash, a tag, or a branch name, and the new branch will point to that commit.
This is especially handy when you want to base your work on an earlier state of the repository or when branching off from a feature branch that isn't the current HEAD.
A closely related command is git checkout <commit>
(without -b
), which just checks out the commit in a detached HEAD state without creating a new branch. You can also use git switch -c <branchname> <commit>
as a more modern alternative, which achieves the same result with a slightly different syntax.
Example:
git checkout -b feature-1234 some-branch