【问题标题】:Multiple branches in the same commit同一提交中的多个分支
【发布时间】:2019-12-31 18:17:31
【问题描述】:

我有一个包含多个(不需要的)分支的提交,由 git log --graph 我得到了这个:

commit 417bb7dfd7d7230fd1c859414d2aa231e72e24e6 (HEAD -> Feature1, master, Feature2)

如何将 Feature1、Feature2 分支从提交 417bb7dfd7d7230fd1c859414d2aa231e72e24e6 移动到不同的提交?

感谢您的帮助。

【问题讨论】:

    标签: git git-branch git-log


    【解决方案1】:

    这似乎是基于误解。

    这些分支不在提交中

    在 git 中,分支是指向提交的标签。一个提交可以有任意数量(零个、四个、一千个等等)指向它的分支。

    A---B---C---D
         \   \   \
          \   \   master
           \   \
            \   branch-abc
             \
              branch-xyz
    

    在上面,masterbranch-abcbranch-xyz 碰巧指向不同的提交,但如果你这样做了

    git checkout branch-abc
    git merge master
    

    你会得到

    A---B---C---D
         \       \
          \       master, branch-abc
           \    
            branch-xyz
    

    ...哪里,是的,masterbranch-abc do 指向同一个提交。


    如果您出于任何原因确实需要移动或删除分支,这很容易(但我必须再次强调重要的部分,即了解分支的用途)

    # move a branch to commit abc123 (when the branch is NOT checked out)
    git branch -f my_branch abc123
    
    # or if the branch IS checked out
    git reset --hard abc123
    
    # delete a branch
    git branch -d my_branch
    
    # ...which will complain in case the branch isn't fully merged yet
    # in which case you can then force it
    git branch -D my_branch
    

    【讨论】:

    • 感谢您的帮助、时间和精力。经过一些练习,它告诉我没有在同一个提交中,它只是显示因为那些是空分支
    • @Andrewboy 是的,这是一种常见的情况。不用担心:-)
    • @Andrewboy:没有“空分支”这样的东西。如果两个分支名称指向同一个提交 a123456,则从 a123456 向后的 所有 提交都在 both 分支中。
    猜你喜欢
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 2011-05-30
    • 2016-03-23
    • 1970-01-01
    • 2021-06-18
    • 2020-03-01
    • 2021-09-16
    相关资源
    最近更新 更多