Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 818759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:11:55+00:00 2026-05-15T02:11:55+00:00

I would like to extract the information that is printed after a git status

  • 0

I would like to extract the information that is printed after a git status, which looks like:

# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.

Of course I can parse the output of git status but this is not recommended since this human readable output is liable to change.

There are two problems:

  1. How to know the remote tracked branch? It is often origin/branch but need not be.
  2. How to get the numbers? How to know whether it is ahead/behind? By how many commits? And what about the diverged branch case?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:11:56+00:00Added an answer on May 15, 2026 at 2:11 am

    update

    As pointed out by amalloy, recent versions of git support finding the matching tracking branch for a given branch by giving “branchname@{upstream}” (or “branchname@{u}”, or “@{u}” for the tracking branch of HEAD). This effectively supercedes the script below. You can do:

    git rev-list @{u}..
    git rev-list --left-right --boundary @{u}...
    gitk @{u}...
    

    etc. For example, I have git q aliased to git log --pretty='...' @{u}.. to show me “queued” commits ready for pushing.

    original answer

    There doesn’t seem to be an easy way to find the tracking branch in general, without parsing lots more git config than is practical in a few shell commands. But for many cases this will go a long way:

    # work out the current branch name
    currentbranch=$(expr $(git symbolic-ref HEAD) : 'refs/heads/\(.*\)')
    [ -n "$currentbranch" ] || die "You don't seem to be on a branch"
    # look up this branch in the configuration
    remote=$(git config branch.$currentbranch.remote)
    remote_ref=$(git config branch.$currentbranch.merge)
    # convert the remote ref into the tracking ref... this is a hack
    remote_branch=$(expr $remote_ref : 'refs/heads/\(.*\)')
    tracking_branch=refs/remotes/$remote/$remote_branch
    # now $tracking_branch should be the local ref tracking HEAD
    git rev-list $tracking_branch..HEAD
    

    Another, more brute-force, approach:

    git rev-list HEAD --not --remotes
    

    jamessan’s answer explains how to find the relative differences between $tracking_branch and HEAD using git rev-list. One fun thing you can do:

    git rev-list --left-right $tracking_branch...HEAD
    

    (note three dots between $tracking_branch and HEAD). This will show commits on both “arms” with a distinguishing mark at the front: “<” for commits on $tracking_branch, and “>” for commits on HEAD.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a full MS SQL Backup file that I would like to extract
I would like to extract the date a jpg file was created. Java has
I would like to extract from a general HTML page, all the text (displayed
I have the following snippet where I would like to extract code between the
I would like to know if it is possible, To extract the userid from
In Nant, I would like to be able to extract the last name of
Would like to get a list of advantages and disadvantages of using Stored Procedures.
Would like to create a strong password in C++. Any suggestions? I assume it
I would like to test a string containing a path to a file for
I would like to sort an array in ascending order using C/C++ . The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.