git 如何从不同分支中的特定提交创建分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8483983/
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 12:29:11  来源:igfitidea点击:

How to create the branch from specific commit in different branch

gitgit-branch

提问by RolandXu

I have made several commits in the master branch and then merged them to dev branch.

我在 master 分支做了几次提交,然后将它们合并到 dev 分支。

I want to create a branch from a specific commit in dev branch, which was first committed in master branch.

我想从 dev 分支中的特定提交创建一个分支,该分支首先在 master 分支中提交。

I used the commands:

我使用了以下命令:

git checkout dev
git branch  <branch name> <commit id>

However, this creates the branch from master branch, not the dev branch I expected. The commit id is same in master branch and dev branch. So, how can I distinguish same commit id in different branch?

但是,这会从 master 分支创建分支,而不是我期望的 dev 分支。提交 id 在 master 分支和 dev 分支中是相同的。那么,如何在不同的分支中区分相同的提交 ID?

PS: I made an example in github here https://github.com/RolandXu/test_for_branch

PS:我在github这里做了一个例子https://github.com/RolandXu/test_for_branch

I used the commands:

我使用了以下命令:

git checkout dev
git branch test 07aeec983bfc17c25f0b0a7c1d47da8e35df7af8

What I expect is that the test branch contains aa.txt bb.txt cc.txt. However, the test branch only contains aa.txt and cc.txt. It most likely created the branch from the master branch.

我期望的是 test 分支包含 aa.txt bb.txt cc.txt。但是,测试分支只包含 aa.txt 和 cc.txt。它很可能从主分支创建了分支。

回答by Gauthier

If you are using this form of the branchcommand (with start point), it does not matter where your HEADis.

如果您正在使用这种形式的branch命令(带有起点),那么您在哪里都没有关系HEAD

What you are doing:

你在做什么:

git checkout dev
git branch test 07aeec983bfc17c25f0b0a7c1d47da8e35df7af8
  • First, you set your HEADto the branch dev,

  • Second, you start a new branch on commit 07aeec98. There is no bb.txt at this commit (according to your github repo).

  • 首先,你设置你HEAD的分支dev

  • 其次,您在 commit 上启动一个新分支07aeec98。此提交中没有 bb.txt(根据您的 github 存储库)。

If you want to start a new branch at the location you have just checked out,you can either run branch with no start point:

如果你想在你刚刚签出的位置开始一个新的分支你可以运行没有起点的分支:

git branch test

or as other have answered, branch and checkout there in one operation:

或者正如其他人所回答的那样,在一个操作中分支和结帐:

git checkout -b test


I think that you might be confused by that fact that 07aeec98is part of the branch dev. It is true that this commit is an ancestor of dev, its changes are needed to reach the latest commit in dev. However, they are other commits that are needed to reach the latest dev, and these are not necessarily in the history of 07aeec98.

我认为您可能07aeec98对分支的一部分这一事实感到困惑dev。确实,此提交是 的祖先dev,需要对其进行更改才能到达dev. 但是,它们是达到最新版本所需的其他提交,dev这些并不一定在07aeec98.

8480e8ae(where you added bb.txt) is for example not in the history of 07aeec98. If you branch from 07aeec98, you won't get the changes introduced by 8480e8ae.

8480e8ae(您添加 bb.txt 的位置)例如不在07aeec98. 如果您从 分支07aeec98,您将不会获得由 引入的更改8480e8ae

In other words: if you merge branch A and branch B into branch C, then create a new branch on a commit of A, you won't get the changes introduced in B.

换句话说:如果您将分支 A 和分支 B 合并到分支 C,然后在 A 的提交上创建一个新分支,您将不会获得 B 中引入的更改。

Same here, you had two parallel branches master and dev, which you merged in dev. Branching out from a commit of master (older than the merge) won't provide you with the changes of dev.

同样在这里,你有两个并行的分支 master 和 dev,你合并到 dev 中。从 master 的提交(早于合并)分支不会为您提供 dev 的更改。



If you want to permanentlyintegrate new changes from master into your feature branches, you should merge masterinto them and go on. This will create merge commits in your feature branches, though.

如果您想将master 的新更改永久集成到您的功能分支中,您应该合并master到它们中并继续。不过,这将在您的功能分支中创建合并提交。

If you have not published your feature branches, you can also rebase them on the updated master: git rebase master featureA. Be prepared to solve possible conflicts.

如果你还没有发布你的功能分支,你也可以在更新的 master 上重新设置它们:git rebase master featureA. 准备好解决可能的冲突。

If you want a workflow where you can work on feature branches free of merge commits and still integrate with newer changes in master, I recommend the following:

如果您想要一个工作流程,您可以在没有合并提交的情况下处理功能分支,并且仍然与 master 中的更新更改集成,我建议以下内容:

  • base every new feature branch on a commit of master
  • create a devbranch on a commit of master
  • when you need to see how your feature branch integrates with new changes in master, merge both master and the feature branch into dev.
  • 每个新功能分支都基于 master 的提交
  • dev在 master 的提交上创建一个分支
  • 当您需要查看您的功能分支如何与 master 中的新更改集成时,请将 master 和功能分支合并到dev.

Do not commit into devdirectly, use it only for merging other branches.

不要dev直接commit into ,只用于合并其他分支。

For example, if you are working on feature A and B:

例如,如果您正在处理功能 A 和 B:

a---b---c---d---e---f---g -master
    \       \
     \       \-x -featureB
      \
       \-j---k -featureA

Merge branches into a devbranch to check if they work well with the new master:

dev分支合并到一个分支中以检查它们是否与新 master 配合良好:

a---b---c---d---e---f---g -master
    \       \            \
     \       \            \--x'---k' -dev
      \       \             /    /   
       \       \-x----------    /    -featureB
        \                      /
         \-j---k--------------- -featureA

You can continue working on your feature branches, and keep merging in new changes from both master and feature branches into devregularly.

您可以继续在您的功能分支上工作,并dev定期将来自主分支和功能分支的新更改合并到其中。

a---b---c---d---e---f---g---h---i----- -master
    \       \            \            \
     \       \            \--x'---k'---i'---l' -dev
      \       \             /    /         /
       \       \-x----------    /         /  -featureB
        \                      /         /  
         \-j---k-----------------l------ -featureA

When it is time to integrate the new features, merge the feature branches (not dev!) into master.

当需要集成新功能时,将功能分支(不是dev!)合并到 master 中。

回答by Cascabel

You have the arguments in the wrong order:

您的参数顺序错误:

git branch <branch-name> <commit>

and for that, it doesn't matter what branch is checked out; it'll do what you say. (If you omit the commit argument, it defaults to creating a branch at the same place as the current one.)

为此,签出哪个分支并不重要;它会按你说的做。(如果省略 commit 参数,它默认会在与当前位置相同的位置创建一个分支。)

If you want to check out the new branch as you create it:

如果要在创建新分支时检出它:

git checkout -b <branch> <commit>

with the same behavior if you omit the commit argument.

如果省略 commit 参数,则具有相同的行为。

回答by Muhammad Soliman

You can do this locally as everyone mentioned using

您可以像每个人提到的那样在本地执行此操作

git checkout -b <branch-name> <sha1-of-commit>

Alternatively, you can do this in github itself, follow the steps:

或者,您可以在 github 本身中执行此操作,请按照以下步骤操作:

1- In the repository, click on the Commits.

1- 在存储库中,单击Commits.

2- on the commit you want to branch from, click on <>to browse the repository at this point in the history.

2- 在您想要分支的提交上,单击<>以浏览历史记录中此时的存储库。

commits history

提交历史

3- Click on the tree: xxxxxxin the upper left. Just type in a new branch name there click Create branch xxxas shown below.

3- 点击tree: xxxxxx左上角的。只需输入一个新的分支名称,然后单击Create branch xxx,如下所示。

create new branch

创建新分支

Now you can fetch the changes from that branch locally and continue from there.

现在您可以从本地该分支获取更改并从那里继续。

回答by manojlds

You have to do:

你必须要做:

git branch <branch_name> <commit>

(you were interchanging the branch name and commit)

(您正在交换分支名称并提交)

Or you can do:

或者你可以这样做:

git checkout -b <branch_name> <commit>

If in place of you use branch name, you get a branch out of tip of the branch.

如果代替您使用分支名称,则会从分支的尖端获得一个分支。

回答by ZMorek

Try

尝试

git checkout <commit hash>
git checkout -b new_branch

The commit should only exist once in your tree, not in two separate branches.

提交应该只在你的树中存在一次,而不是在两个单独的分支中。

This allows you to check out that specific commit and name it what you will.

这使您可以查看该特定提交并为其命名。