简体   繁体   English

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

[英]How to create the branch from specific commit in different branch

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. 但是,这会从master分支创建分支,而不是我期望的dev分支。 The commit id is same in master branch and dev branch. master分支和dev分支中的commit id相同。 So, how can I distinguish same commit id in different branch? 那么,我如何区分不同分支中的相同提交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. 我期望的是测试分支包含aa.txt bb.txt cc.txt。 However, the test branch only contains aa.txt and cc.txt. 但是,测试分支仅包含aa.txt和cc.txt。 It most likely created the branch from the master branch. 它很可能是从主分支创建了分支。

If you are using this form of the branch command (with start point), it does not matter where your HEAD is. 如果您使用这种形式的branch命令(带起点),则HEAD位置无关紧要。

What you are doing: 你在做什么:

git checkout dev
git branch test 07aeec983bfc17c25f0b0a7c1d47da8e35df7af8
  • First, you set your HEAD to the branch dev , 首先,将HEAD设置为分支dev

  • Second, you start a new branch on commit 07aeec98 . 其次,在commit 07aeec98上启动一个新分支。 There is no bb.txt at this commit (according to your github repo). 此提交中没有bb.txt(根据您的github repo)。

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 07aeec98 is part of the branch dev . 我认为你可能会因为07aeec98是分支dev一部分而感到困惑。 It is true that this commit is an ancestor of dev , its changes are needed to reach the latest commit in dev . 确实,这个提交是dev的祖先,它需要进行更改以达到dev的最新提交。 However, they are other commits that are needed to reach the latest dev , and these are not necessarily in the history of 07aeec98 . 但是,它们是达到最新dev所需的其他提交,这些不一定是07aeec98的历史。

8480e8ae (where you added bb.txt) is for example not in the history of 07aeec98 . 8480e8ae (您添加bb.txt的地方)不在07aeec98的历史记录中。 If you branch from 07aeec98 , you won't get the changes introduced by 8480e8ae . 如果从07aeec98分支,则不会得到8480e8ae引入的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. 在这里,你有两个并行分支master和dev,你在dev中合并。 Branching out from a commit of master (older than the merge) won't provide you with the changes of dev. 从master的提交(比合并更早)分支将不会为您提供dev的更改。


If you want to permanently integrate new changes from master into your feature branches, you should merge master into them and go on. 如果要将master中的新更改永久集成到功能分支中,则应将master合并到其中并继续。 This will create merge commits in your feature branches, though. 但是,这将在您的功能分支中创建合并提交。

If you have not published your feature branches, you can also rebase them on the updated master: git rebase master featureA . 如果您尚未发布功能分支,则还可以在更新的主分支上重新定义它们: git rebase master featureA Be prepared to solve possible conflicts. 准备好解决可能的冲突。

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 将每个新功能分支基于master的提交
  • create a dev branch on a commit of master 在master的提交上创建一个dev分支
  • 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中的新更改集成时,将master和feature分支合并到dev

Do not commit into dev directly, use it only for merging other branches. 不要直接提交到dev ,只将它用于合并其他分支。

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 dev branch to check if they work well with the new master: 将分支合并到dev分支以检查它们是否与新主服务器配合良好:

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 dev regularly. 您可以继续处理功能分支,并定期将主数据库和功能分支的新更改合并到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中。

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参数,则具有相同的行为。

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. 如果代替您使用分支名称,您将从分支的末端获得分支。

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. 这允许您检查特定的提交并为其命名。

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-在要分支的提交上,单击<>以在历史记录中的此时浏览存储库。

承诺历史

3- Click on the tree: xxxxxx in the upper left. 3-单击tree: xxxxxx左上角的tree: xxxxxx Just type in a new branch name there click Create branch xxx as shown below. 只需键入一个新的分支名称,单击Create branch xxx ,如下所示。

创建新的分支

Now you can fetch the changes from that branch locally and continue from there. 现在,您可以在本地从该分支获取更改并从那里继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM