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

  • Home
  • SEARCH
  • 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 827057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:30:49+00:00 2026-05-15T03:30:49+00:00

I periodically get message from git that look like this: Your branch is behind

  • 0

I periodically get message from git that look like this:

Your branch is behind the tracked remote branch 'local-master/master' 
by 3 commits, and can be fast-forwarded.

I would like to be able to write commands in a shell script that can do the following:

  1. How can I tell if my current branch can be fast-forwarded from the remote branch it is tracking?

  2. How can I tell how many commits “behind” my branch is?

  3. How can I fast-forward by just one commit, so that for example, my local branch would go from “behind by 3 commits” to “behind by 2 commits”?

(For those who are interested, I am trying to put together a quality git/darcs mirror.)

  • 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-15T03:30:50+00:00Added an answer on May 15, 2026 at 3:30 am

    The remote branch can be fast-forwarded to the local branch if the current commit is the ancestor of the remote branch head. In other words, if the “one-branch history” of the remote branch contains the current commit (because if it does, it is sure that the new commits were committed “onto” the current commit)

    So a safe way to determine whether the remote branch can be fast-forwarded:

    # Convert reference names to commit IDs
    current_commit=$(git rev-parse HEAD)
    remote_commit=$(git rev-parse remote_name/remote_branch_name)
    
    # Call git log so that it prints only commit IDs
    log=$(git log --topo-order --format='%H' $remote_commit | grep $current_commit)
    
    # Check the existence of the current commit in the log
    if [ ! -z "$log" ]
      then echo 'Remote branch can be fast-forwarded!'
    fi
    

    Note that git log was called without the –all parameter (which would list all branches), so it is not possible that the current commit is on a “side branch” and is still printed on the output.

    The number of commits ahead of the current commit equals the number of rows in $log before $current_commit.

    If you want to fast-forward only one commit, you take the row previous to the current commit (with grep -B 1, for example), and reset the local branch to this commit.

    UPDATE: you can use git log commit1..commit2 to determine the number of fast-forwarding commits:

    if [ ! -z "$log" ]
    then
      # print the number of commits ahead of the current commit
      ff_commits=$(git log --topo-order --format='%H' \
        $current_commit..$remote_commit | wc -l)
      echo "Number of fast-forwarding commits: $ff_commits"
    
      # fast-forward only one commit
      if [ $ff_commits -gt 1 ]
      then
        next_commit=$(git log --topo-order --format='%H' \
          $current_commit..$remote_commit | tail -1)
        git reset --hard $next_commit
      fi
    fi
    

    Of course, you can do this with one git log call if you save the result of the first call into a file.

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

Sidebar

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.