Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a new Git branch from an old commit? [duplicate]

Tags:

git

branch

commit

Possible Duplicate / a more recent/less clear question
Branch from a previous commit using Git

I have a Git branch called jzbranch and have an old commit id: a9c146a09505837ec03b.

How do I create a new branch, justin, from the information listed above?

like image 683
JZ. Avatar asked Aug 23 '11 21:08

JZ.


People also ask

How do you create a new branch from an old commit?

In order to create a Git branch from a commit, use the “git checkout” command with the “-b” option and specify the branch name as well as the commit to create your branch from. Alternatively, you can use the “git branch” command with the branch name and the commit SHA for the new branch.

How do I recreate a remote branch?

If you are allowed to rewrite the remote branch, you can use git push --force my_remote my_branch . Show activity on this post. That's right, git push -f is appropriate in the example. The reason being there's no need to re-create the entire branch, since all the commits pre-rebase remain the same.


1 Answers

git checkout -b NEW_BRANCH_NAME COMMIT_ID

This will create a new branch called 'NEW_BRANCH_NAME' and check it out.

("check out" means "to switch to the branch")

git branch NEW_BRANCH_NAME COMMIT_ID

This just creates the new branch without checking it out.


in the comments many people seem to prefer doing this in two steps. here's how to do so in two steps:

git checkout COMMIT_ID
# you are now in the "detached head" state
git checkout -b NEW_BRANCH_NAME
like image 60
bdonlan Avatar answered Oct 28 '22 23:10

bdonlan