[git] Create a branch in Git from another branch

If you want to make a branch from some another branch then follow bellow steps:

Assumptions:

  1. You are currently in master branch.
  2. You have no changes to commit. (If you have any changes to commit, stash it!).
  3. BranchExisting is the name of branch from which you need to make a new branch with name BranchMyNew.

Steps:

  1. Fetch the branch to your local machine.

    $ git fetch origin BranchExisting : BranchExisting
    

This command will create a new branch in your local with same branch name.

  1. Now, from master branch checkout to the newly fetched branch

    $ git checkout BranchExisting
    
  2. You are now in BranchExisting. Now create a new branch from this existing branch.

    $ git checkout -b BranchMyNew
    

Here you go!