简体   繁体   English

git branch不应该-不合并列出新的分支吗?

[英]Shouldn't git branch --no-merged list new branches?

We have a large number of central git repositories and use branches to track work on various projects. 我们有大量的中央git存储库,并使用分支来跟踪各种项目的工作。 When a project finishes, we merge to master and push to origin. 项目完成后,我们合并到母版并推送到原点。

When starting a new project, I'd like to list any other current work on that repository, as a heads-up for the developer (eg so they can communicate their release plans). 当开始一个新项目时,我想列出该存储库上的任何其他当前工作,以提醒开发人员(例如,以便他们可以交流其发布计划)。 git branch --all --no-merged origin/master seems promising but apparently it only lists branches that have commits. git branch --all --no-merged origin/master看起来很有希望,但显然它只列出有提交的分支。 Conceptually, even newly created, "empty" branches state the intention of doing some work, so shouldn't these be listed too? 从概念上讲,即使是新创建的“空”分支也说明了进行某些工作的意图 ,因此也不应列出这些分支吗?

I suspect that this may be related to the distinction between a branch and a branch head, and while the branch head points to its start point there is nothing to merge. 我怀疑这可能与分支和分支头之间的区别有关,并且当分支头指向其起点时,没有什么可合并的。 But since merges are explicitly recorded in history (right?) shouldn't it be possible to identify even "empty" branches as unmerged? 但是由于合并已明确记录在历史记录中(对吗?),是否有可能将“空”分支也标识为未合并? Can this be done? 能做到吗?

An obvious workaround is to force a dummy initial commit in each new branch but I'd like to avoid this if possible. 一个明显的解决方法是在每个新分支中强制执行虚拟初始提交,但我想尽可能避免这种情况。 And it seems developers should not have to push their unfinished changes if they don't want to (so I expect most branches in the central repository would remain empty until their project is completed). 而且,如果开发人员不愿意,他们似乎不必推送未完成的更改(因此,我希望中央存储库中的大多数分支在项目完成之前都将保持空白)。

Example: 例:

# Alice creates and pushes branch1
git clone $REPO clone1
git checkout -b branch1
git push -u --all
# ...continues development in her local repository...

# Bob wants to know if anybody is working on $REPO
git clone $REPO clone2
git branch --all --no-merged origin/master
# no output - he doesn't realize Alice is working in the same repository

It seems there is no git built-in support for what I want, but with some git plumbing I got the results I was looking for, inspired by Finding a branch point with Git? 似乎我没有想要的git内置支持,但是通过git管道,我得到了我想要的结果,这是受Git查找分支点启发的 .

Let me know if these assumptions are not correct... 让我知道这些假设是否正确...

  • A branch whose head matches its branch point from origin/master (ie. has no commits) should be an empty branch, indicating the intention of doing work. 分支的头部与origin/master节点的分支点相匹配的分支(即没有提交)应为空分支,表明有工作意图。
  • A branch whose best merge point with origin/master matches its head is already fully merged, and can be ignored. 分支与origin/master的最佳合并点匹配其头部的分支已经完全合并,可以忽略。
  • Any other case should be a branch with unmerged changes. 其他任何情况都应是具有未合并更改的分支。

Example script that prints the category for all remote branches; 为所有远程分支打印类别的示例脚本; if Bob ran this it would tell him refs/remotes/origin/branch1: new branch without any commits : 如果Bob运行了它,它将告诉他refs/remotes/origin/branch1: new branch without any commits

#! /usr/bin/env bash

# check branches created from and merged to this branch
MASTER=refs/remotes/origin/master

# for each remote branch...
for BRANCH in $(git for-each-ref --format='%(refname)' refs/remotes/); do
    # ...except the $MASTER branch and the current HEAD
    [[ $BRANCH == $MASTER || $BRANCH =~ /HEAD$ ]] && continue

    # get the hash for the head of the branch
    BRANCH_HEAD=$(git show-ref --head --hash $BRANCH)

    # returns the first shared commit among $MASTER and $BRANCH commits
    BRANCH_POINT=$(fgrep -m 1 -f <(git rev-list --first-parent $BRANCH) \
                                 <(git rev-list --first-parent $MASTER))

    # find the best merge point
    BRANCH_MERGE=$(git merge-base $MASTER $BRANCH)

    # determine the type of branch
    if [[ $BRANCH_POINT == $BRANCH_HEAD ]]; then
        echo "$BRANCH: new branch without any commits"
    elif [[ $BRANCH_MERGE == $BRANCH_HEAD ]]; then
        echo "$BRANCH: fully merged into $MASTER"
    else
        echo "$BRANCH: branch with unmerged changes"
    fi
done

No, because there are no unmerged changes in branch1. 否,因为branch1中没有未合并的更改。 Therefore "--no-merged" won't list the branch. 因此,“-no-merged”不会列出分支。 Once Alice commits and pushes a change, then it will show up. 爱丽丝提交并推送更改后,它将显示出来。

I am afraid I am with Doug-W on this one. 恐怕我与Doug-W在一起。 --no-merged refers to a branch which contains a commit (or many) that the current branch does not contain. --no-merged是指包含当前分支不包含的一个(或多个)提交的分支。 Since you are branching from master, than not making a follow up commit then all commits that the branch contain are also within master. 因为您是从master分支的,所以不进行后续提交,那么分支包含的所有提交也都在master内。

Since creating a branch has no concept of when the branch was created, you are losing out on a metric that may be important to you and your company in the future. 由于创建分支机构没有创建分支机构的时间的概念,因此您将失去一个可能对您和您的公司将来很重要的指标。 If you were to make just a single commit once a branch had been created with just a simple "stub" file then you could use the timestamp of this stub commit and the second commits timestamp to know how long a project was parked for before starting. 如果仅使用一个简单的“存根”文件创建分支后就只进行一次提交,则可以使用此存根提交的时间戳和第二个提交时间戳来了解项目在开始之前要停放多长时间。

This also has the added benefit of reporting these branches in your --no-merged command. 这还具有在--no-merged命令中报告这些分支的额外好处。

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

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