git create branch from commit id

we can create a branch form commit id or sha code or head number

create branch from commit id or sha code

           git  branch  <branch _name>  <commit id>

by using above command you can create a new branch from commit id or sha code

create branch from head number

        git  branch  <branch_name>  <HEAD@{n}>

 

git branch devops  HEAD@{0}

it will create a branch name called ‘devops’ from latest commit since we mentioned HEAD@{0}

HEAD@{0} represnts the latest commit

HEAD@{1} represnts 1 commit before

HEAD@{2} represnts 2 commits before

…………….

create branch from commit id and checkit out

        git  checkout   -b  <branch_name>  <commit-id>

If you want to check out the new branch as you create it you can use this command

git checkout -b devops <commit-id>

This will create a new branch called ‘devops’ and check it out.

create branch from HEAD and checkit out

        git checkout -b <branch_name> HEAD@{n}

this will also create branch and check it out. So you can use anyone of above commands

push branch to remote repository or git-hub or bit-bucket

above commands will only create a branch in local repository not in remote repository. so you need to push the newly created branch to remote git-hub or bit-bucket. for that we use below command.

   git   push   --set-upstream   origin   <newly_created_branch>

 

 

  • git create branch from commit id
  • git create branch from head number

Leave a Reply

Your email address will not be published. Required fields are marked *