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

发布于 2024-10-08 06:30:31 字数 715 浏览 0 评论 0原文

我有两个分支:ma​​sterdev

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

目前在分支dev上,我做:

git checkout -b myfeature dev

...(一些工作)

git commit -am "blablabla"
git push origin myfeature

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

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

我的意思是分支看起来快进合并,我不明白为什么......

我做错了什么?

如何从另一个分支分支并将功能分支推回到远程存储库?

所有这些都在分支模型中,例如此处描述的模型

I have two branches: master and dev

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

Currently on the branch dev, I do:

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 fast-forward merged, and I don't understand why...

What am I doing wrong?

How can 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

四叶草在未来唯美盛开 2024-10-15 06:30:32

从另一个分支在 Git 中创建分支的各种方法:

此答案添加了一些额外的见解,这些见解尚未出现在现有答案中,仅涉及问题本身的标题在来自另一个分支的 Git),但是没有解决问题的更狭窄的细节,这里已经有足够的答案。

我添加这个是因为我现在确实需要知道如何执行下面的#1(从我签出的分支创建一个新分支),而且目前还不清楚如何做这样做,谷歌搜索将这里列为热门搜索结果。所以,我将在这里分享我的发现。这里的任何其他答案都没有很好地触及这一点(如果有的话)。

在执行此操作时,我还将添加我在常规工作流程中使用的其他最常见的 gitbranch 命令,如下所示。

1. 要从您未签出的分支创建新分支:

branch1 创建 branch2 while你有任何分支签出(例如:假设你有 master 签出):

git branch branch2 branch1

一般格式是:

git branch <new_branch> [from_branch]

man gitbranch 显示如下。我所说的 就是他们所说的 ,我所说的 [from_branch] 就是他们所说的 [<起点>]

git 分支 [--track | --no-track] [-l] [-f] <分支名称> [<起点>]

2. 要从您所做已签出的分支创建新分支:

git branch new_branch

这非常适合在变基、压缩、硬重置等操作之前进行备份 - 在执行任何操作之前这可能会严重扰乱你的分支。

示例:我在 feature_branch1 上,我打算使用 git rebase -i master 将 20 个提交压缩为 1 个。如果我想“撤消”此操作,让我们先备份此分支!我所有......时间都这样做,并且发现它非常有帮助和令人欣慰,因为我知道我总是可以轻松返回到这个备份分支并重新分支重试,以防我在此过程中弄乱 feature_branch1

git branch feature_branch1_BAK_20200814-1320hrs_about_to_squash

20200814-1320hrs 部分是格式为 YYYYMMDD-HHMMhrs 的日期和时间>,所以这将是 2020 年 8 月 14 日 13:20(下午 1:20)。这样我就可以轻松找到我的备份分支,直到我确定我准备好删除它们。如果你不这样做并且搞砸了,你必须使用 git reflog 在搞砸之前找到你的分支,这会更困难,压力更大,也更容易出错。

感叹词:关于 git checkoutgit switch 的注释

  1. 经典、通用的“瑞士军刀”,可以做 1000 件事:git checkout
  2. git checkout 的新的实验性替代命令:git switch + git Restore

git switch 最近在 Git v2.23 中添加了。使用 git --version 查看您的 git 版本。它被设计为 git checkout 的一种极其有限的形式,设计用于切换分支,而不是像 git 那样具有签出或恢复文件的能力checkout 就可以了。在此处阅读更多信息:https://www.git-tower.com/learn/git/commands/git-switch。

另请参阅:引入 git switch 后 git checkout 还能做什么?. git Restore 提供了 git checkout 的一些其余功能,而 git switch 不包含这些功能。

man git switchman git Restore 注意:

此命令是实验性的。行为可能会改变。

因此,如果您愿意,请随意坚持使用 git checkout 。它的行为不太可能改变。

我自己几乎只使用 git checkout ,但欢迎您使用 git switch (和 git restore )来恢复或“签出”文件)如果你喜欢的话。

3. 要从您执行已检出的分支中创建并检出一个新分支:

# the standard, "classic" command most people still use; `-b` stands
# for "create 'b'ranch"
git checkout -b new_branch

# the newer, "experimental" command now available as of Git v2.23; `-c` stands
# for "'c'reate branch"
git switch -c new_branch

为了使那里发生的情况变得显而易见,请了解上面的这个命令相当于这两个单独的命令:

# classic
git branch new_branch
git checkout new_branch

# OR: new/experimental as of Git v2.23
git branch new_branch
git switch new_branch

4. 要创建并从一个分支中签出一个新分支,您已经检查过:

# classic
git checkout -b new_branch from_branch

# OR: new/experimental as of Git v2.23
git switch -c new_branch from_branch

为了清楚地表明那里发生了什么,请知道上面的这个命令相当于这三个单独的命令:

# classic
git checkout from_branch
git branch new_branch
git checkout new_branch

# OR: new/experimental as of Git v2.23
git switch from_branch
git branch new_branch
git switch new_branch

5. 重命名分支

就像重命名分支中的常规文件或文件夹一样在终端中,git 认为“重命名”更像是 'm'ove 命令,因此您可以使用 git Branch -m 来重命名分支。这是一般格式:

git branch -m <old_name> <new_name>

man gitbranch 显示如下:

git 分支 (-m | -M) [<旧分支>] <新分支>

示例:让我们将 branch_1 重命名为 branch_1.5

git branch -m branch_1 branch_1.5

或者,如果您已签出 branch_1,则可以重命名当前-签出分支branch_1.5,如下所示:

git branch -m branch_1.5

6. 根据 上最新的上游更改创建新的功能分支 (feature2) >main 当您当前已签出 feature1

让我们将上面学到的大量内容放在一起,以呈现一个非常常见的示例,每当我们完成一项功能并需要启动另一项功能时,我们都需要运行该示例一。

# The beginner/intermediate way

git checkout main           # check out main branch
git pull                    # pull latest upstream changes down
git checkout -b feature2    # create branch `feature2` from `main`, and check
                                # it out, all in one step`

# The advanced way

# [this is what I do--I'll explain why, below]
git fetch origin main:main      # pull latest upstream changes down from the
                                    # remote `main` branch, to the
                                    # locally-stored remote-tracking **hidden**
                                    # branch named `origin/main`, **and** to
                                    # the local `main` branch
git checkout -b feature2 main   # create branch `feature2` from `main`, and
                                    # check it out, all in one step`

# OR (nearly the same thing)
git fetch origin main           # pull latest upstream changes down from the
                                    # remote `main` branch, to the
                                    # locally-stored remote-tracking **hidden**
                                    # branch named `origin/main`. This does NOT
                                    # update the local `main` branch though!
git checkout -b feature2 origin/main # create branch `feature2` from
                                        # `origin/main`, and check it out, all
                                        # in one step`

因此,当我在分支 feature1 上时,刚刚完成它并准备开始基于最新 main 的新 feature2 > 分支,为什么我要这样做:

git fetch origin main:main
git checkout -b feature2 main

...而不是这个?:

git checkout main
git pull
git checkout -b feature2

令人惊讶的是,答案是 git checkout 可能是一个非常慢且繁重的操作!—使用git lfs<在大型单一存储库上花费最多3个多小时 /code>存储库的 .git/hooks/post-checkout 文件中的 post-checkout 挂钩。让我解释一下。 .git/hooks/post-checkout 文件是一个可执行文件,其中包含每次运行 git checkout 后 git 都会运行的脚本。如果它包含对 git lfs post-checkout "$@" 的调用,那么它可能会尝试下载 20+ GBgit lfs 数据(具体到我工作的存储库 - 您的情况可能会有所不同)在运行看似无辜的 git checkout 后。我不想那样做!因此,我跳过 git checkout 过程以避免这种麻烦,并立即开始我的 feature2 分支,无需下载所有我不下载的东西。不需要先!

另请参阅:

  1. 我经常引用并认为是“git 基础知识”的答案的快速链接:
    1. 从另一个分支在 git 中创建分支的各种方法
    2. 有关在 git 中检出文件或目录的所有信息
    3. 根据 Git,谁是“我们”,谁是“他们”?
    4. 如何通过 --diff-filter= 使用 git diff 过滤器
    5. 有关在 git 存储库中搜索(通过 grep 或类似方式)的所有内容
    6. 作为基本用户如何使用 git lfs:git lfs 和 git lfs 有什么区别fetchgit lfs fetch --allgit lfs pull

Various ways to create a branch in Git from another branch:

This answer adds some additional insight, not already present in the existing answers, regarding just the title of the question itself (Create a branch in Git from another branch), but does not address the more narrow specifics of the question which already have sufficient answers here.

I'm adding this because I really needed to know how to do #1 below just now (create a new branch from a branch I do not have checked out), and it wasn't obvious how to do it, and Google searches led to here as a top search result. So, I'll share my findings here. This isn't touched upon well, if at all, by any other answer here.

While I'm at it, I'll also add my other most-common git branch commands I use in my regular workflow, below.

1. To create a new branch from a branch you do not have checked out:

Create branch2 from branch1 while you have any branch whatsoever checked out (ex: let's say you have master checked out):

git branch branch2 branch1

The general format is:

git branch <new_branch> [from_branch]

man git branch shows it as follows. What I call <new_branch> is what they call <branchname>, and what I call [from_branch] is what they call [<start-point>]:

git branch [--track | --no-track] [-l] [-f] <branchname> [<start-point>]

2. To create a new branch from the branch you do have checked out:

git branch new_branch

This is great for making backups before rebasing, squashing, hard resetting, etc.—before doing anything which could mess up your branch badly.

Example: I'm on feature_branch1, and I'm about to squash 20 commits into 1 using git rebase -i master. In case I ever want to "undo" this, let's back up this branch first! I do this ALL...THE...TIME and find it super helpful and comforting to know I can always easily go back to this backup branch and re-branch off of it to try again in case I mess up feature_branch1 in the process:

git branch feature_branch1_BAK_20200814-1320hrs_about_to_squash

The 20200814-1320hrs part is the date and time in format YYYYMMDD-HHMMhrs, so that would be 13:20 hours (1:20 pm) on 14 Aug. 2020. This way I have an easy way to find my backup branches until I'm sure I'm ready to delete them. If you don't do this and you mess up badly, you have to use git reflog to go find your branch prior to messing it up, which is much harder, more stressful, and more error-prone.

Interjection: notes about git checkout vs git switch

  1. The classic, universal "Swiss army knife" which can do 1000 things: git checkout.
  2. The new and experimental alternative commands to git checkout: git switch + git restore.

git switch was added recently in Git v2.23. See your git version with git --version. It is designed to be an extremely limited form of git checkout, designed only to switch branches rather than also having the ability to check out or restore files, like git checkout can do. Read more here: https://www.git-tower.com/learn/git/commands/git-switch.

See also: What does git checkout still do after git switch got introduced?. git restore offers some of the rest of the functionality of git checkout which git switch does not contain.

Both man git switch and man git restore caution:

THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.

So, feel free to stick with git checkout if you like. Its behavior is not likely to change.

I pretty much just use git checkout myself, but you are welcome to use git switch (and git restore to restore or "check out" files) if you like.

3. To create and check out a new branch from the branch you do have checked out:

# the standard, "classic" command most people still use; `-b` stands
# for "create 'b'ranch"
git checkout -b new_branch

# the newer, "experimental" command now available as of Git v2.23; `-c` stands
# for "'c'reate branch"
git switch -c new_branch

To make it obvious what is happening there, know that this one command above is equivalent to these two separate commands:

# classic
git branch new_branch
git checkout new_branch

# OR: new/experimental as of Git v2.23
git branch new_branch
git switch new_branch

4. To create and check out a new branch from a branch you do not have checked out:

# classic
git checkout -b new_branch from_branch

# OR: new/experimental as of Git v2.23
git switch -c new_branch from_branch

To make it obvious what is happening there, know that this one command above is equivalent to these three separate commands:

# classic
git checkout from_branch
git branch new_branch
git checkout new_branch

# OR: new/experimental as of Git v2.23
git switch from_branch
git branch new_branch
git switch new_branch

5. To rename a branch

Just like renaming a regular file or folder in the terminal, git considered "renaming" to be more like a 'm'ove command, so you use git branch -m to rename a branch. Here's the general format:

git branch -m <old_name> <new_name>

man git branch shows it like this:

git branch (-m | -M) [<oldbranch>] <newbranch>

Example: let's rename branch_1 to branch_1.5:

git branch -m branch_1 branch_1.5

OR, if you already have branch_1 checked out, you can rename the currently-checked-out branch to branch_1.5 like this:

git branch -m branch_1.5

6. To create a new feature branch (feature2) based off of the latest upstream changes on main when you currently have feature1 checked out

Let's put a lot of what we learned above together to present a very common example we need to run whenever we finish one feature and need to start another one.

# The beginner/intermediate way

git checkout main           # check out main branch
git pull                    # pull latest upstream changes down
git checkout -b feature2    # create branch `feature2` from `main`, and check
                                # it out, all in one step`

# The advanced way

# [this is what I do--I'll explain why, below]
git fetch origin main:main      # pull latest upstream changes down from the
                                    # remote `main` branch, to the
                                    # locally-stored remote-tracking **hidden**
                                    # branch named `origin/main`, **and** to
                                    # the local `main` branch
git checkout -b feature2 main   # create branch `feature2` from `main`, and
                                    # check it out, all in one step`

# OR (nearly the same thing)
git fetch origin main           # pull latest upstream changes down from the
                                    # remote `main` branch, to the
                                    # locally-stored remote-tracking **hidden**
                                    # branch named `origin/main`. This does NOT
                                    # update the local `main` branch though!
git checkout -b feature2 origin/main # create branch `feature2` from
                                        # `origin/main`, and check it out, all
                                        # in one step`

So, when I'm on branch feature1, and have just finished it and am ready to start on a new feature2 based off of the latest main branch, why do I do this:

git fetch origin main:main
git checkout -b feature2 main

...instead of this?:

git checkout main
git pull
git checkout -b feature2

The answer, surprisingly, is that git checkout can be a horribly slow and heavy operation!—taking up to 3+ hours on a massive mono-repo utilizing git lfs post-checkout hooks in the repo's .git/hooks/post-checkout file. Let me explain. The .git/hooks/post-checkout file is an executable file containing a script that git will run after each time you run git checkout. If it contains a call to git lfs post-checkout "$@", then it may try to download 20+ GB of git lfs data (specific to the repo I work in—your scenario may vary) after running an innocent-looking git checkout. I don't want to do that! So, I skip the git checkout process to avoid that hassle and get started on my feature2 branch immediately without downloading all of that stuff I don't need first!

See also:

  1. Quick links to my answers I reference frequently and consider to be "git fundamentals":
    1. Various ways to create a branch in git from another branch
    2. All about checking out files or directories in git
    3. Who is "us" and who is "them" according to Git?
    4. How to use git diff filters via --diff-filter=
    5. All about searching (via grep or similar) in your git repositories
    6. How to use git lfs as a basic user: What is the difference between git lfs fetch, git lfs fetch --all, and git lfs pull?
毁我热情 2024-10-15 06:30:32

创建分支

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

    git 分支分支1

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


签出分支

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

  • git checkoutbranchname

重命名分支

  • gitbranch -mbranch1newbranchname

删除分支

  • gitbranch-d要删除的分支
  • gitbranch-Dbranch-to-删除
    ( 强制删除而不检查合并状态 )

创建并切换分支

  • git checkout -b 分支名称

完全包含的分支

  • gitbranch --merged


分支差异 [ git diffbranch1..branch2 ]

多行差异

  • git diff master..branch1

单行差异

  • git diff --color-wordsbranch1..branch2

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


Checkout a Branch

git checkout command switch branches or restore working tree files

  • git checkout branchname

Renaming a Branch

  • 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 )

Create and Switch Branch

  • git checkout -b branchname

Branches that are completely included

  • git branch --merged


Branch Differences [ git diff branch1..branch2 ]

Multiline difference

  • git diff master..branch1

Singleline difference

  • git diff --color-words branch1..branch2
无声静候 2024-10-15 06:30:32

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

git checkout -b <sub-branch> branch

例如:

  • 要创建的新分支的名称是“XYZ”
  • ,分支的名称是“ABC”,在该分支下必须创建“XYZ”
git checkout -b XYZ ABC

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

git checkout -b <sub-branch> branch

For example:

  • the name of the new branch to be created is 'XYZ'
  • the name of the branch, 'ABC', under which 'XYZ' has to be created
git checkout -b XYZ ABC
2024-10-15 06:30:32

Git 2.23 引入了 git switchgit Restore 来拆分 git checkout 的职责。

从 Git 2.23 开始,从现有分支创建新分支:

git switch -c my-new-branch

切换到新分支 'my-new-branch '

  • -c--create 的缩写,取代了著名的 git checkout -b

看看在 这篇 GitHub 博客文章中解释了以下更改更详细的信息:

Git 2.23 为现有套件带来了一对新的实验性命令
其中:git switchgit 恢复。这两个最终目的是
为众所周知的 git checkout 提供更好的界面。新的
命令旨在每个命令都有明确的分离,整齐地划分
git checkout

有哪些职责

Git 2.23 introduces git switch and git restore to split the responsibilities of git checkout.

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

git switch -c my-new-branch

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

  • -c is short for --create and replaces the well-known git checkout -b

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

Git 2.23 brings a new pair of experimental commands to the suite of existing
ones: git switch and 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

春花秋月 2024-10-15 06:30:32

dev 分支上同时进行工作。在您的场景中,功能分支从 dev 分支的尖端向前移动,但 dev 分支不会改变。绘制直线更容易,因为它可以被视为向前运动。您在 dev 上到达了 A 点,然后从那里继续沿着平行路径前进。这两个分支并没有真正分歧。

现在,如果您在合并之前对 dev 进行提交,您将再次从同一提交 A 开始,但现在功能将转到 C,dev 转到 B。这将显示您试图想象的分裂,因为分支现在已经分叉。

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

相对

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

Do simultaneous work on the dev branch. 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.

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-------*Feature

Versus

       /----*DevB
*-----*DevA
       \----*FeatureC
分開簡單 2024-10-15 06:30:32

如果您想从另一个分支创建分支,请按照以下步骤操作:

假设

  1. 您当前位于 master 分支中。
  2. 您无需提交任何更改。 (如果您有任何要提交的更改,请将其隐藏!)
  3. BranchExisting 是分支的名称,您需要从中创建一个名为 BranchMyNew 的新分支。

步骤

  1. 将分支提取到本地计算机。

    git fetch origin BranchExisting : BranchExisting
    

此命令将在本地创建一个具有相同分支名称的新分支。

  1. 现在,从master分支签出到新获取的分支

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

    git checkout -b BranchMyNew
    

干得好!

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

Assumptions:

  1. You are currently in the 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 the 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!

心的位置 2024-10-15 06:30:32

这将从当前分支创建新分支:

git checkout -b your_new_branch_name 

如果您想创建现有分支的 new_branch 那么:

git checkout -b your_new_branch_name existing_branch_name

This is going to create new branch from current branch:

git checkout -b your_new_branch_name 

If you want to create new_branch of an existing branch then:

git checkout -b your_new_branch_name existing_branch_name
宁愿没拥抱 2024-10-15 06:30:32

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

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

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

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 "

紫轩蝶泪 2024-10-15 06:30:32

切换到 develop 分支:

git checkout develop

创建 developfeature/foo 分支。

git checkout -b feature/foo develop

合并更改以进行开发,无需快进

git checkout develop
git merge --no-ff myFeature

现在将更改推送到服务器:

git push origin develop
git push origin feature/foo

Switch to the develop branch:

git checkout develop

Creates feature/foo branch of develop.

git checkout -b feature/foo develop

Merge the changes to develop without a fast-forward

git checkout develop
git merge --no-ff myFeature

Now push changes to the server:

git push origin develop
git push origin feature/foo
仅此而已 2024-10-15 06:30:32

这对我有用:

转到您的GitHub< /a> 存储库,然后选择要创建新分支的分支名称,如下图所示:

Image 2

然后输入您要创建的分支名称,然后单击“创建”分支。。

图片 2

It's worked for me:

Go to your GitHub repository, and select the branch name from where you want to create a new branch, as shown in the below image:

Image 2

Then type the branch name you want to create, and click on Create branch.

Image 2

橘和柠 2024-10-15 06:30:31

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

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

但要回答您的问题:

git checkout -b myFeature dev

dev 创建 MyFeature 分支。完成您的工作,然后

git commit -am "Your message"

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

git checkout dev
git merge --no-ff myFeature

现在将更改推送到服务器

git push origin dev
git push origin myFeature

您将看到您想要的效果。

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

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

But to answer your question:

git checkout -b myFeature dev

Creates the MyFeature branch off dev. Do your work and then

git commit -am "Your message"

Now merge your changes to dev without a fast-forward

git checkout dev
git merge --no-ff myFeature

Now push the changes to the server

git push origin dev
git push origin myFeature

And you'll see it how you want it.

舂唻埖巳落 2024-10-15 06:30:31

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

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

  • ma​​ster
  • dev
  • branch1

因此,如果您想创建一个名为 “subbranch_of_b1”在名为“branch1”的分支下按照以下步骤操作:

  1. 签出或更改为“branch1”

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

     git checkout -b subbranch_of_b1 分支1
    

    以上命令将在分支 branch1 下创建一个名为 subbranch_of_b1 的新分支(请注意,上述命令中的 branch1 不是强制性的,因为HEAD 当前指向它,如果您在不同的分支上,您可以精确它)。

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

示例图形在另一个分支下创建分支的插图

subbranch_of_b1推送到远程:

 git push origin subbranch_of_b1

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

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

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

  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_b1 under the branch branch1 (note that branch1 in 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_b1 you can commit and push or merge it locally or remotely.

A sample Graphical Illustration Of Creating Branches Under another Branch

Push the subbranch_of_b1 to remote:

 git push origin subbranch_of_b1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文