Checkout commit to new branch
A common Git workflow!
To checkout a commit to a new branch, you can use the following steps:
-
Checkout the commit: Use the
git checkout
command with the commit hash or reference to checkout the commit. For example:git checkout <commit-hash>
Replace
<commit-hash>
with the actual hash of the commit you want to checkout. -
Create a new branch: Use the
git branch
command to create a new branch based on the checked-out commit. For example:git branch new-branch
Replace
new-branch
with the name you want to give to your new branch. -
Checkout the new branch: Use the
git checkout
command again to switch to the new branch. For example:git checkout new-branch
Now you should be on the new branch, with the commit you checked out as the starting point.
Here's an example of the entire workflow:
$ git checkout <commit-hash>
$ git branch new-branch
$ git checkout new-branch
Note that if you want to create a new branch from the current branch (i.e., the branch you're currently on), you can use the -b
option with git checkout
. For example:
git checkout -b new-branch
This will create a new branch and switch to it in one step.