简体   繁体   English

列表分支未合并到其上游

[英]List branches not merged to their upstream

I'd like a command similar to git branch that will list all local branches that are not merged with their upstream. 我想要一个类似于git branch的命令,它将列出所有与其上游合并的本地分支。

This is something git branch knows about, as git branch -d <branch-name> will fail for branches that are not merged. 这是git branch知道的,因为git branch -d <branch-name>对于未合并的分支将失败。

Are you looking for this: 你在找这个:

git branch --no-merged

Above command will list out all the branches which are not merged to your current branch. 上面的命令将列出所有未合并到当前分支的分支。 if you need to check for other branch then 如果你需要检查其他分支那么

git branch --no-merged master

You can also compare upstream branches by specifying the -r flag and a ref to check against, which can be local or remote: 您还可以通过指定-r标志和要检查的ref来比较上游分支,可以是本地或远程:

git branch -r --no-merged origin/master

As far as I know, there is no single built-in Git command to list all local branches that are behind their upstream branch (if any). 据我所知,没有单一的内置Git命令列出其上游分支后面的所有本地分支(如果有的话)。 However, you can implement the desired functionality using existing Git commands. 但是,您可以使用现有的Git命令实现所需的功能。 See below. 见下文。

Script 脚本

#!/bin/sh

# git-bbu.sh
#
# List the local Branches Behind their Upstream branch, if any.
#
# Usage: git bbu
#
# To make a Git alias called 'bbu' out of this script,
# put the latter on your search path, and run
#
#   git config --global alias.bbu \
#       '!sh git-bbu.sh'

if [ $# -ne 0 ]; then
    printf "%s\n\n" "usage: git bbu"
    exit 1
fi

git for-each-ref --format='%(refname:short)' refs/heads | \
    while read ref; do
      if (git rev-parse --verify --quiet "$ref"@{upstream} &&
          ! git diff --quiet "$ref"..."$ref"@{upstream}) \
              >/dev/null 2>&1; then
        printf "%s\n" "$ref"
      fi
    done

Explanation 说明

Run

git for-each-ref --format='%(refname:short)' refs/heads

to list all local branches (Why not just use git branch , here? Because, in a script, you should try to use plumbing Git commands instead of porcelain ones). 列出所有本地分支(为什么不在这里使用git branch ?因为,在脚本中,你应该尝试使用管道Git命令而不是瓷器命令)。 Pipe it to a while loop, and, for each such local branch, 将它传递给while循环,对于每个这样的本地分支,

  • Check whether the local branch has an upstream branch, using 使用,检查本地分支是否有上游分支

     git rev-parse --verify --quiet "$ref"@{upstream} 
  • Check whether the local branch is behind its upstream branch, using (the logical negation of) 使用(逻辑否定)检查本地分支是否在其上游分支之后

     git diff --quiet "$ref"..."$ref"@{upstream} 

If both conditions are verified, print the name of the branch. 如果两个条件都已验证,请打印分支的名称。

References 参考

git for-each-ref has an "upstream" field, which can show if the branch is up-to-date with its upstream. git for-each-ref有一个“上游”字段,可以显示分支是否与其上游最新。 You can then filter its output to show only those which are ahead of their upstreams: 然后,您可以过滤其输出以仅显示其上游之前的输出:

$ git for-each-ref --format='%(upstream:trackshort)..%(refname:short)' | awk -F '\\.\\.' '$1~/>/{print $2}'

this skips branches which do not have upstreams, modify the filter if it's not what you need (the $1 is empty then in the awk record). 这会跳过没有上游的分支,如果不是你需要的话,修改过滤器( $1为空,然后在awk记录中)。 ".." is used aa separator because you cannot have the sequence in reference name ".."用于分隔符,因为您不能在引用名称中包含序列

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

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