从另一个分支在 Git 中创建一个分支
我有两个分支:master 和 dev
我想从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
从另一个分支在 Git 中创建分支的各种方法:
此答案添加了一些额外的见解,这些见解尚未出现在现有答案中,仅涉及问题本身的标题(在来自另一个分支的 Git),但是没有解决问题的更狭窄的细节,这里已经有足够的答案。
我添加这个是因为我现在确实需要知道如何执行下面的#1(从我未签出的分支创建一个新分支),而且目前还不清楚如何做这样做,谷歌搜索将这里列为热门搜索结果。所以,我将在这里分享我的发现。这里的任何其他答案都没有很好地触及这一点(如果有的话)。
在执行此操作时,我还将添加我在常规工作流程中使用的其他最常见的
gitbranch
命令,如下所示。1. 要从您未签出的分支创建新分支:
从
branch1
创建branch2
while你有任何分支签出(例如:假设你有master
签出):一般格式是:
man gitbranch
显示如下。我所说的
就是他们所说的
,我所说的[from_branch]
就是他们所说的[<起点>]
:2. 要从您所做已签出的分支创建新分支:
这非常适合在变基、压缩、硬重置等操作之前进行备份 - 在执行任何操作之前这可能会严重扰乱你的分支。
示例:我在
feature_branch1
上,我打算使用git rebase -i master
将 20 个提交压缩为 1 个。如果我想“撤消”此操作,让我们先备份此分支!我所有......时间都这样做,并且发现它非常有帮助和令人欣慰,因为我知道我总是可以轻松返回到这个备份分支并重新分支重试,以防我在此过程中弄乱feature_branch1
:20200814-1320hrs
部分是格式为YYYYMMDD-HHMMhrs
的日期和时间>,所以这将是 2020 年 8 月 14 日 13:20(下午 1:20)。这样我就可以轻松找到我的备份分支,直到我确定我准备好删除它们。如果你不这样做并且搞砸了,你必须使用 git reflog 在搞砸之前找到你的分支,这会更困难,压力更大,也更容易出错。感叹词:关于
git checkout
与git switch
的注释git checkout
。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 switch
和man git Restore
注意:因此,如果您愿意,请随意坚持使用 git checkout 。它的行为不太可能改变。
我自己几乎只使用 git checkout ,但欢迎您使用 git switch (和 git restore )来恢复或“签出”文件)如果你喜欢的话。
3. 要从您执行已检出的分支中创建并检出一个新分支:
为了使那里发生的情况变得显而易见,请了解上面的这个命令相当于这两个单独的命令:
4. 要创建并从一个分支中签出一个新分支,您不已经检查过:
为了清楚地表明那里发生了什么,请知道上面的这个命令相当于这三个单独的命令:
5. 重命名分支
就像重命名分支中的常规文件或文件夹一样在终端中,
git
认为“重命名”更像是 'm'ove 命令,因此您可以使用git Branch -m
来重命名分支。这是一般格式:man gitbranch
显示如下:示例:让我们将
branch_1
重命名为branch_1.5
:或者,如果您已签出
branch_1
,则可以重命名当前-签出分支到branch_1.5
,如下所示:6. 根据
上最新的上游更改创建新的功能分支 (
当您当前已签出feature2
) >mainfeature1
时让我们将上面学到的大量内容放在一起,以呈现一个非常常见的示例,每当我们完成一项功能并需要启动另一项功能时,我们都需要运行该示例一。
因此,当我在分支
feature1
上时,刚刚完成它并准备开始基于最新main
的新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+ GB 的git lfs
数据(具体到我工作的存储库 - 您的情况可能会有所不同)在运行看似无辜的git checkout
后。我不想那样做!因此,我跳过git checkout
过程以避免这种麻烦,并立即开始我的feature2
分支,无需下载所有我不下载的东西。不需要先!另请参阅:
--diff-filter=
使用git diff
过滤器grep
或类似方式)的所有内容git lfs 和 git lfs 有什么区别fetch
、git lfs fetch --all
和git 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
frombranch1
while you have any branch whatsoever checked out (ex: let's say you havemaster
checked out):The general format is:
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>]
:2. To create a new branch from the branch you do have checked out:
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 usinggit 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 upfeature_branch1
in the process:The
20200814-1320hrs
part is the date and time in formatYYYYMMDD-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 usegit 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
vsgit switch
git checkout
.git checkout
:git switch
+git restore
.git switch
was added recently in Git v2.23. See your git version withgit --version
. It is designed to be an extremely limited form ofgit checkout
, designed only to switch branches rather than also having the ability to check out or restore files, likegit 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 ofgit checkout
whichgit switch
does not contain.Both
man git switch
andman git restore
caution: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 usegit switch
(andgit 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:
To make it obvious what is happening there, know that this one command above is equivalent to these two separate commands:
4. To create and check out a new branch from a branch you do not have checked out:
To make it obvious what is happening there, know that this one command above is equivalent to these three separate commands:
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 usegit branch -m
to rename a branch. Here's the general format:man git branch
shows it like this:Example: let's rename
branch_1
tobranch_1.5
:OR, if you already have
branch_1
checked out, you can rename the currently-checked-out branch tobranch_1.5
like this:6. To create a new feature branch (
feature2
) based off of the latest upstream changes onmain
when you currently havefeature1
checked outLet'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.
So, when I'm on branch
feature1
, and have just finished it and am ready to start on a newfeature2
based off of the latestmain
branch, why do I do this:...instead of this?:
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 utilizinggit 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 rungit checkout
. If it contains a call togit lfs post-checkout "$@"
, then it may try to download 20+ GB ofgit lfs
data (specific to the repo I work in—your scenario may vary) after running an innocent-lookinggit checkout
. I don't want to do that! So, I skip thegit checkout
process to avoid that hassle and get started on myfeature2
branch immediately without downloading all of that stuff I don't need first!See also:
git diff
filters via--diff-filter=
grep
or similar) in your git repositoriesgit lfs
as a basic user: What is the difference betweengit lfs fetch
,git lfs fetch --all
, andgit lfs pull
?创建分支
在签出主分支时创建分支。这里 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
要从本地目录中的另一个分支创建分支,可以使用以下命令。
例如:
To create a branch from another branch in your local directory you can use the following command.
For example:
Git 2.23 引入了
git switch
和git Restore
来拆分git checkout
的职责。从 Git 2.23 开始,从现有分支创建新分支:
git switch -c my-new-branch
切换到新分支 'my-new-branch '
看看在 这篇 GitHub 博客文章中解释了以下更改更详细的信息:
Git 2.23 introduces
git switch
andgit restore
to split the responsibilities ofgit 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'
Take a look at this GitHub blog post explaining the changes in greater detail:
在
dev
分支上同时进行工作。在您的场景中,功能分支从 dev 分支的尖端向前移动,但 dev 分支不会改变。绘制直线更容易,因为它可以被视为向前运动。您在 dev 上到达了 A 点,然后从那里继续沿着平行路径前进。这两个分支并没有真正分歧。现在,如果您在合并之前对 dev 进行提交,您将再次从同一提交 A 开始,但现在功能将转到 C,dev 转到 B。这将显示您试图想象的分裂,因为分支现在已经分叉。
相对
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.
Versus
如果您想从另一个分支创建分支,请按照以下步骤操作:
假设:
BranchExisting
是分支的名称,您需要从中创建一个名为BranchMyNew
的新分支。步骤:
将分支提取到本地计算机。
此命令将在本地创建一个具有相同分支名称的新分支。
现在,从master分支签出到新获取的分支
您现在处于BranchExisting中。现在从这个现有分支创建一个新分支。
干得好!
If you want to make a branch from some another branch then follow the below steps:
Assumptions:
BranchExisting
is the name of branch from which you need to make a new branch with nameBranchMyNew
.Steps:
Fetch the branch to your local machine.
This command will create a new branch in your local with same branch name.
Now, from the master branch checkout to the newly fetched branch
You are now in BranchExisting. Now create a new branch from this existing branch.
Here you go!
这将从当前分支创建新分支:
如果您想创建现有分支的 new_branch 那么:
This is going to create new branch from current branch:
If you want to create new_branch of an existing branch then:
要从另一个分支创建分支,也可以使用以下语法:
它比“git checkout -b”+“git push origin”短一点
For creating a branch from another one can use this syntax as well:
It is a little shorter than "git checkout -b " + "git push origin "
切换到 develop 分支:
创建 develop 的 feature/foo 分支。
合并更改以进行开发,无需快进
现在将更改推送到服务器:
Switch to the develop branch:
Creates feature/foo branch of develop.
Merge the changes to develop without a fast-forward
Now push changes to the server:
这对我有用:
转到您的GitHub< /a> 存储库,然后选择要创建新分支的分支名称,如下图所示:
然后输入您要创建的分支名称,然后单击“创建”分支。。
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:
Then type the branch name you want to create, and click on Create branch.
如果您喜欢您发布的链接中的方法,请查看 Git Flow。
这是他为该工作流程创建的一组脚本。
但要回答您的问题:
从 dev 创建 MyFeature 分支。完成您的工作,然后
现在将您的更改合并到dev,无需快进
现在将更改推送到服务器
您将看到您想要的效果。
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:
Creates the MyFeature branch off dev. Do your work and then
Now merge your changes to dev without a fast-forward
Now push the changes to the server
And you'll see it how you want it.
如果您想从 Git 中的任何现有分支创建新分支,只需按照选项操作即可。
首先更改/检出到要创建新分支的分支。例如,如果您有以下分支:
因此,如果您想创建一个名为 “subbranch_of_b1”在名为“branch1”的分支下按照以下步骤操作:
签出或更改为“branch1”
现在使用以下命令在“branch1”下创建名为“subbranch_of_b1”的新分支。
以上命令将在分支 branch1 下创建一个名为 subbranch_of_b1 的新分支(请注意,上述命令中的
branch1
不是强制性的,因为HEAD 当前指向它,如果您在不同的分支上,您可以精确它)。现在,在使用 subbranch_of_b1 后,您可以在本地或远程提交、推送或合并它。
将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:
So if you want to create a new branch called "subbranch_of_b1" under the branch named "branch1" follow the steps:
Checkout or change into "branch1"
Now create your new branch called "subbranch_of_b1" under the "branch1" using the following command.
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).Now after working with the subbranch_of_b1 you can commit and push or merge it locally or remotely.
Push the subbranch_of_b1 to remote: