从另一个分支在 Git 中创建一个分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4470523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:39:32  来源:igfitidea点击:

Create a branch in Git from another branch

gitgit-branchgit-workflowgit-flow

提问by revohsalf

I have two branches: masterand dev

我有两个分支:masterdev

I want to create a "feature branch" from the devbranch.

我想从开发分支创建一个“功能分支” 。

Currently on the branch dev, I do:

目前在分支 dev 上,我这样做:

$ git checkout -b myfeature dev

... (some work)

... (一些工作)

$ git commit -am "blablabla"
$ git push origin myfeature

But, after visualizing my branches, I got:

但是,在可视化我的分支之后,我得到了:

--**master**
------0-----0-----0-----0-----0
------------------------**dev**----**myfeature**

I mean that the branch seems ff merged, and I don't understand why...

我的意思是分支似乎合并了,我不明白为什么......

What I'm doing wrong?

我做错了什么?

Can you explain me please how you branch off from another branch and push back to the remote repository for the feature branch?

你能解释一下你如何从另一个分支分支并推回到特性分支的远程存储库吗?

All that in a branching model like the one described here.

所有这些都在一个分支模型中,就像这里描述的那样

回答by Abizern

If you like the method in the link you've posted, have a look at Git Flow.

如果您喜欢您发布的链接中的方法,请查看Git Flow

It's a set of scripts he created for that workflow.

这是他为该工作流程创建的一组脚本。

But to answer your question:

但要回答你的问题:

$ git checkout -b myFeature dev

Creates MyFeature branch off dev. Do your work and then

在 dev 之外创建 MyFeature 分支。做你的工作然后

$ git commit -am "Your message"

Now merge your changes to dev without a fast-forward

现在无需快进即可将您的更改合并到 dev

$ git checkout dev
$ git merge --no-ff myFeature

Now push changes to the server

现在将更改推送到服务器

$ git push origin dev
$ git push origin myFeature

And you'll see it how you want it.

你会看到它是你想要的。

回答by Praveen George

If you want create a new branch from any of the existing branches in Git, just follow the options.

如果您想从 Git 中的任何现有分支创建一个新分支,只需按照选项操作即可。

First change/checkout into the branch from where you want to create a new branch. For example, if you have the following branches like:

首先更改/签出到要创建新分支的分支。例如,如果您有以下分支,例如:

  • master
  • dev
  • branch1
  • 掌握
  • 开发
  • 分支 1

So if you want to create a new branch called "subbranch_of_b1"under the branch named "branch1"follow the steps:

所以,如果你想创建一个名为新分支“subbranch_of_b1”命名的分支下的“BRANCH1”遵循的步骤:

  1. Checkout or change into "branch1"

    git checkout branch1
    
  2. Now create your new branch called "subbranch_of_b1"under the "branch1"using the following command.

    git checkout -b subbranch_of_b1 branch1
    

    The above will create a new branch called subbranch_of_b1under the branch branch1(note that branch1in the above command isn't mandatory since the HEAD is currently pointing to it, you can precise it if you are on a different branch though).

  3. Now after working with the subbranch_of_b1you can commit and push or merge it locally or remotely.

  1. 结帐或更改为“branch1”

    git checkout branch1
    
  2. 现在,创建名为新分支“subbranch_of_b1”下的“BRANCH1”使用下面的命令。

    git checkout -b subbranch_of_b1 branch1
    

    上面的代码将创建一个名为新的分支subbranch_of_b1分支下BRANCH1(注意,branch1在上面的命令是不是强制性的,因为头是目前指向它,你可以精确的,如果你是在一个不同的分支,虽然)。

  3. 现在在使用subbranch_of_b1之后,您可以在本地或远程提交和推送或合并它。

A sample Graphical Illustration Of Creating Branches Under another Branch

在另一个分支下创建分支的示例图解说明

push the subbranch_of_b1 to remote

将 subbranch_of_b1 推送到远程

 git push origin subbranch_of_b1 

回答by Gnanasekar S

Create a Branch

创建一个分支

  • Create branch when master branch is checked out. Here commits in master will be synced to the branch you created.

    $ git branch branch1

  • Create branch when branch1 is checked out . Here commits in branch1 will be synced to branch2

    $ git branch branch2

  • 检出主分支时创建分支。这里 master 中的提交将同步到您创建的分支。

    $ git branch branch1

  • 检出 branch1 时创建分支。这里 branch1 中的提交将同步到 branch2

    $ git branch branch2



Checkout a Branch

结帐分行

git checkout command switch branches or restore working tree files

git checkout 命令切换分支或恢复工作树文件

  • $ git checkout branchname
  • $ git checkout branchname


Renaming a Branch

重命名分支

  • $ git branch -m branch1 newbranchname
  • $ git branch -m branch1 newbranchname


Delete a Branch

删除一个分支

  • $ git branch -d branch-to-delete
  • $ git branch -D branch-to-delete( force deletion without checking the merged status)
  • $ git branch -d branch-to-delete
  • $ git branch -D branch-to-delete强制删除而不检查合并状态


Create and Switch Branch

创建和切换分支

  • $ git checkout -b branchname
  • $ git checkout -b branchname


Branches that are completely included

完全包含的分支

  • $ git branch --merged
  • $ git branch --merged




************************** Branch Differences[ git diff branch1..branch2 ] ************************

**************************分支差异[git diff branch1..branch2] ************** **********

Multiline difference多行差异
  • $ git diff master..branch1
  • $ git diff master..branch1
Singleline difference单线差异
  • $ git diff --color-words branch1..branch2
  • $ git diff --color-words branch1..branch2

回答by ToothlessRebel

Do simultaneous work on the devbranch. What happens is that in your scenario the feature branch moves forward from the tip of the dev branch, but the dev branch does not change. It's easier to draw as a straight line, because it can be thought of as forward motion. You made it to point A on dev, and from there you simply continued on a parallel path. The two branches have not really diverged.

dev分支上同时工作。发生的情况是,在您的场景中,功能分支从 dev 分支的尖端向前移动,但 dev 分支没有改变。画一条直线更容易,因为它可以被认为是向前运动。你在 dev 上到达了 A 点,然后你只是继续沿着平行路径前进。这两个分支并没有真正分道扬镳。

Now, if you make a commit on dev, before merging, you will again begin at the same commit, A, but now features will go to C and dev to B. This will show the split you are trying to visualize, as the branches have now diverged.

现在,如果您在 dev 上进行提交,则在合并之前,您将再次从同一个提交 A 开始,但现在功能将转到 C,而 dev 转到 B。这将显示您尝试可视化的拆分,作为分支现在已经分道扬镳了。

*-----*Dev-------*Feature

Versus

相对

       /----*DevB
*-----*DevA
       \----*FeatureC

回答by Sumit Ghewade

To create a branch from another branch in your local directory you can use following command.

要从本地目录中的另一个分支创建分支,您可以使用以下命令。

git checkout -b <sub-branch> branch

For Example:

例如:

  • name of the new branch to be created 'XYZ'
  • name of the branch ABC under which XYZ has to be created
  • 要创建的新分支的名称 'XYZ'
  • 必须在其下创建 XYZ 的分支 ABC 的名称
git checkout -b XYZ ABC

回答by JSON C11

Git 2.23 introduces git switchand git restoreto split the responsibilities of git checkout

Git 2.23 引入git switchgit restore划分职责git checkout

Creating a new branch from an existing branch as of git 2.23:

从 git 2.23 的现有分支创建一个新分支:

git switch -c my-new-branch

git switch -c my-new-branch

Switched to a new branch 'my-new-branch'

切换到一个新的分支 'my-new-branch'

  • -cis short for --createand replaces the well-known git checkout -b
  • -c--create 的缩写,替换了著名的git checkout -b

Take a look at thisGithub blog post explaining the changes in greater detail:

看看这篇Github 博客文章,更详细地解释了这些变化:

Git 2.23 brings a new pair of experimental commands to the suite of existing ones: git switchand git restore. These two are meant to eventually provide a better interface for the well-known git checkout. The new commands intend to each have a clear separation, neatly divvying up what the many responsibilities of git checkout

Git 2.23 为现有命令套件带来了一对新的实验命令:git switchgit restore。这两个旨在最终为众所周知的 git checkout 提供更好的界面。新命令旨在将每个命令明确分开,巧妙地划分git checkout的许多职责

回答by Darshit

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. BranchExistingis the name of branch from which you need to make a new branch with name BranchMyNew.
  1. 您目前在 master 分支。
  2. 您没有要提交的更改。(如果您有任何更改要提交,请将其隐藏!)。
  3. BranchExisting是分支的名称,您需要从中创建一个名为 name 的新分支BranchMyNew

Steps:

步骤

  1. Fetch the branch to your local machine.

    $ git fetch origin BranchExisting : BranchExisting
    
  1. 将分支提取到您的本地机器上。

    $ 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
    
  1. 现在,从主分支结帐到新获取的分支

    $ git checkout BranchExisting
    
  2. 您现在位于 BranchExisting。现在从这个现有分支创建一个新分支。

    $ git checkout -b BranchMyNew
    

Here you go!

干得好!

回答by Alexander Samoylov

For creating a branch from another one can use this syntax as well:

要从另一个分支创建分支,也可以使用以下语法:

git push origin refs/heads/<sourceBranch>:refs/heads/<targetBranch>

It is a little shorter than "git checkout -b " + "git push origin "

它比“git checkout -b”+“git push origin”短一点