You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

咨询Git branch -rvv过滤后输出列的分解命令及各字段含义

Great question! Let's break down how to parse the output of your git branch command, explain each column, and share the specific commands to extract individual values.

First, let's clarify the typical output structure

After running your filtered command, a sample line might look like this (I'll use this as a reference for column explanations):

origin/release/v1.0  789abc Merge pull request #123 from myorg/release/v0.9 [origin/main: ahead 3]

What each column means

  • 1st Column: The full name of the remote branch (e.g., origin/release/v1.0). This is the unique identifier for the branch on your configured remote repository.
  • 2nd Column: The short SHA1 hash (first 7 characters of the full 40-character hash) of the branch's head commit. This uniquely identifies the latest commit on that branch.
  • 3rd to Nth Columns (until [ or end of line): The commit's subject line. In your case, this is the merge commit message: Merge pull request #123 from myorg/release/v0.9, which indicates this commit merges pull request #123 from the specified source branch.
  • Last Column (optional, wrapped in []): The upstream branch relationship status. For example, [origin/main: ahead 3] means this remote branch is 3 commits ahead of its upstream branch origin/main.

Commands to extract individual columns

Since the commit subject line contains spaces, we can't just use simple cut (it would split the subject into multiple columns). Instead, use awk or sed for flexible parsing:

Extract the 1st column (remote branch name)

Grab the first token before the first space:

git branch -rvv | grep "Merge pull request" | grep "$fromCurrentRepoTo" | awk '{print $1}'

Extract the 2nd column (short SHA1 hash)

Grab the second token:

git branch -rvv | grep "Merge pull request" | grep "$fromCurrentRepoTo" | awk '{print $2}'

Extract the commit subject line

We need to capture everything from the 3rd token until we hit the [ (if present) or the end of the line:

Option 1: Using awk (handles lines with or without the upstream status)
git branch -rvv | grep "Merge pull request" | grep "$fromCurrentRepoTo" | awk '{
    for(i=3; i<=NF; i++) {
        if ($i ~ /^\[/) break
        printf "%s ", $i
    }
    print ""
}'
Option 2: Using sed (clean regex approach)

For lines with the upstream status:

git branch -rvv | grep "Merge pull request" | grep "$fromCurrentRepoTo" | sed -E 's/^[^ ]+ [^ ]+ (.*?) \[.*\]$/\1/'

For lines without the upstream status:

git branch -rvv | grep "Merge pull request" | grep "$fromCurrentRepoTo" | sed -E 's/^[^ ]+ [^ ]+ (.*)$/\1/'

Extract the upstream relationship status (if present)

Grab the last token, or just the content inside the []:

Option 1: Using awk (gets the full [] string)
git branch -rvv | grep "Merge pull request" | grep "$fromCurrentRepoTo" | awk '{print $NF}'
Option 2: Using sed (extracts only the content inside [])
git branch -rvv | grep "Merge pull request" | grep "$fromCurrentRepoTo" | sed -E 's/.*\[([^\]]+)\].*/\1/'

内容的提问来源于stack exchange,提问作者CodeMed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:25:31