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 3220666
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:47:16+00:00 2026-05-17T15:47:16+00:00

How can I iterate through all the local branches in my repository using bash

  • 0

How can I iterate through all the local branches in my repository using bash script.
I need to iterate and check is there any difference between the branch and some remote branches.
Ex

for branch in $(git branch); 
do
    git log --oneline $branch ^remotes/origin/master;
done

I need to do something like given above, but the issue I’m facing is $(git branch) gives me the folders inside the repository folder along with the branches present in the repository.

Is this the correct way to solve this issue? Or is there another way to do it?

Thank you

  • 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-17T15:47:16+00:00Added an answer on May 17, 2026 at 3:47 pm

    You should not use git branch when writing scripts. Git provides a “plumbing” interface that is explicitly designed for use in scripting (many current and historical implementations of normal Git commands (add, checkout, merge, etc.) use this same interface).

    The plumbing command you want is git for-each-ref:

    git for-each-ref --shell \
      --format='git log --oneline %(refname) ^origin/master' \
      refs/heads/
    


    Note: You do not need the remotes/ prefix on the remote ref unless you have other refs that cause origin/master to match multiple places in the ref name search path (see “A symbolic ref name. …” in the Specifying Revisions section of git-rev-parse(1)). If you are trying to explictly avoid ambiguity, then go with the full ref name: refs/remotes/origin/master.

    You will get output like this:

    git log --oneline 'refs/heads/master' ^origin/master
    git log --oneline 'refs/heads/other' ^origin/master
    git log --oneline 'refs/heads/pu' ^origin/master
    

    You can pipe this output into sh.

    If you do not like the idea of generating the shell code, you could give up a bit of robustness* and do this:

    for branch in $(git for-each-ref --format='%(refname)' refs/heads/); do
        git log --oneline "$branch" ^origin/master
    done
    

    *
    Ref names should be safe from the shell’s word splitting (see git-check-ref-format(1)). Personally I would stick with the former version (generated shell code); I am more confident that nothing inappropriate can happen with it.

    Since you specified bash and it supports arrays, you could maintain safety and still avoid generating the guts of your loop:

    branches=()
    eval "$(git for-each-ref --shell --format='branches+=(%(refname))' refs/heads/)"
    for branch in "${branches[@]}"; do
        # …
    done
    

    You could do something similar with $@ if you are not using a shell that supports arrays (set -- to initialize and set -- "$@" %(refname) to add elements).

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

Sidebar

Related Questions

Is there a method in which I can iterate through all objects that exist
How can I iterate over each file in a directory using a for loop?
I need to access the bluetooth network interface from Java. All local network interfaces
I'm attempting to iterate through all User Profiles in SharePoint 2010 from a Silverlight
I'd like to iterate through all the <HeadA> and <HeadB> elements in an XML
I have class method that returns a list of employees that I can iterate
How can I iterate over a string in Python (get each character from the
In Python, given a module X and a class Y, how can I iterate
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can you cast a List<int> to List<string> somehow? I know I could loop through

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.