2

我们有大量的中央 git 存储库,并使用分支来跟踪各种项目的工作。当一个项目完成时,我们合并到 master 并推送到 origin。

在开始一个新项目时,我想列出该存储库上的任何其他当前工作,作为开发人员的提示(例如,以便他们可以传达他们的发布计划)。git branch --all --no-merged origin/master看起来很有希望,但显然它只列出了有提交的分支。从概念上讲,即使是新创建的“空”分支也表明了做一些工作的意图,所以这些不应该也列出来吗?

我怀疑这可能与分支和分支头之间的区别有关,虽然分支头指向它的起点,但没有什么可以合并的。但是由于合并被明确记录在历史中(对吗?)难道不应该将“空”分支识别为未合并吗?这可以做到吗?

一个明显的解决方法是在每个新分支中强制进行虚拟初始提交,但如果可能的话,我想避免这种情况。如果他们不想,开发人员似乎不应该推动他们未完成的更改(所以我希望中央存储库中的大多数分支在他们的项目完成之前都将保持空白)。

例子:

# 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
4

3 回答 3

1

似乎没有对我想要的东西的 git 内置支持,但是通过一些 git 管道,我得到了我正在寻找的结果,灵感来自Find a branch point with Git? .

如果这些假设不正确,请告诉我...

  • 一个分支,其头部与它的分支点匹配origin/master(即没有提交)应该是一个空分支,表明做工作的意图。
  • 最佳合并点origin/master与其头部匹配的分支已经完全合并,可以忽略。
  • 任何其他情况都应该是具有未合并更改的分支。

打印所有远程分支的类别的示例脚本;如果 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
于 2012-11-27T14:40:33.417 回答
0

恐怕我和Doug-W在这件事上。--no-merged指包含当前分支不包含的提交(或许多)的分支。由于您是从 master 分支,而不是进行后续提交,因此分支包含的所有提交也在 master 中。

由于创建分支没​​有关于何时创建分支的概念,因此您将失去一个对您和您的公司未来可能很重要的指标。如果您在仅使用一个简单的“存根”文件创建分支后只进行一次提交,那么您可以使用此存根提交的时间戳和第二次提交时间戳来了解项目在开始之前停放了多长时间。

--no-merged这还具有在您的命令中报告这些分支的额外好处。

于 2014-11-14T08:12:06.483 回答
0

不,因为 branch1 中没有未合并的更改。因此“--no-merged”不会列出分支。一旦 Alice 提交并推送更改,它就会显示出来。

于 2012-11-19T23:23:57.543 回答