Create new branch based on current branch to work on a new feature

时间:2017-10-12 10:04:45

标签: git git-branch git-checkout

How do I create a new branch in git to begin work on a new feature?

I want the new branch to be a duplicate of the current branch (ie, the new branch's HEAD should be the same as the current HEAD).


Question differentiation:

2 个答案:

答案 0 :(得分:10)

TL;DR:

To create and start work on a new branch called FEATURE, you do:

git checkout -b FEATURE

Detailed explanation

To create a branch called FEATURE:

git branch FEATURE

However, this does not change your current branch.

You can then checkout the newly created branch (which means make to it the branch you're currently working on:

git checkout FEATURE

(You can see the current branch marked with a * in the output of git branch --list.)

Generally you want to start working in the branch you have just created, so the shortcut equivalent for both commands is git checkout -b FEATURE, which creates a new branch, then does checkout on it.

答案 1 :(得分:2)

如果你说

$ git checkout -b myFeatureBranch anotherBranch

它将根据myFeatureBranch创建anotherBranch。但是如果你说

$ git checkout -b myFeatureBranch

它将从当前分支中创建myFeatureBranch

相关问题