Fine-tuning a script to pull out all branches of remotes

I started using one script added to this thread :

#!/bin/bash

main() {
  REMOTES="$@";
  if [ -z "$REMOTES" ]; then
    REMOTES=$(git remote);
  fi
  REMOTES=$(echo "$REMOTES" | xargs -n1 echo)
  CLB=$(git branch -l|awk '/^\*/{print $2}');
  echo "$REMOTES" | while read REMOTE; do
    git remote update $REMOTE
    git remote show $REMOTE -n \
    | awk '/merges with remote/{print $5" "$1}' \
    | while read line; do
      RB=$(echo "$line"|cut -f1 -d" ");
      ARB="refs/remotes/$REMOTE/$RB";
      LB=$(echo "$line"|cut -f2 -d" ");
      ALB="refs/heads/$LB";
      NBEHIND=$(( $(git rev-list --count $ALB..$ARB 2>/dev/null) +0));
      NAHEAD=$(( $(git rev-list --count $ARB..$ALB 2>/dev/null) +0));
      if [ "$NBEHIND" -gt 0 ]; then
        if [ "$NAHEAD" -gt 0 ]; then
          echo " branch $LB is $NBEHIND commit(s) behind and $NAHEAD commit(s) ahead of $REMOTE/$RB. could not be fast-forwarded";
        elif [ "$LB" = "$CLB" ]; then
          echo " branch $LB was $NBEHIND commit(s) behind of $REMOTE/$RB. fast-forward merge";
          git merge -q $ARB;
        else
          echo " branch $LB was $NBEHIND commit(s) behind of $REMOTE/$RB. reseting local branch to remote";
          git branch -l -f $LB -t $ARB >/dev/null;
        fi
      fi
    done
  done
}

main $@

It has been working fine so far, but I was wondering how to configure it to pull out all the branches, for some reason it will not pull certain local branches.

First example : I have 4 local branches left with the leading one being the current one, after running this script several branches (4) that were behind were deleted, but I can still see the main branch behind the console, as shown below:

enter image description here

Second example : before running the script, I got this, 2 local branches behind and the current (release) in order:

enter image description here

I run the script and I got this:

enter image description here

So, how can I configure the script to "all" local branches?

+4
2

, , , script reset .
, %(upstream:track)

script

  • git 2.11.0.windows.1
  • SourceTree 1.9.10.0

:

#!/bin/bash

branches=$(git for-each-ref --format="%(refname) %(upstream) %(upstream:track)" refs/heads)

echo "${branches}"
branch_checkedout=$(cat .git/HEAD|cut -f2 -d" ")
echo branch checked out: "${branch_checkedout}"

while read -r branch_line; do
  ahead=0
  behind=0
  branch_local=$(echo ${branch_line}|cut -f1 -d" ")
  branch_remote=$(echo ${branch_line}|cut -f2 -d" ")
  echo ${branch_line} | grep "ahead" >/dev/null && ahead=1
  echo ${branch_line} | grep "behind" >/dev/null && behind=1
  NAHEAD=$(( $(git rev-list --count ${branch_remote}..${branch_local} 2>/dev/null) +0))
  NBEHIND=$(( $(git rev-list --count ${branch_local}..${branch_remote} 2>/dev/null) +0));

  if [ "$NBEHIND" -gt 0 ]; then
    if [ "$NAHEAD" -gt 0 ]; then
      echo " branch $LB is $NBEHIND commit(s) behind and $NAHEAD commit(s) ahead of $REMOTE/$RB. could not be fast-forwarded";
    elif [ "${branch_local}" = "${branch_checkedout}" ]; then
      echo " branch ${branch_local} was $NBEHIND commit(s) behind of ${branch_remote}. fast-forward merge";
      echo "git merge -q ${branch_remote}"
      git merge -q ${branch_remote};
    else
      echo " branch ${branch_local} was $NBEHIND commit(s) behind of ${branch_remote}. reseting local branch to remote";
      bl=${branch_local#*/}
      bl=${bl#*/}
      echo "git branch -l -f ${bl} -t ${branch_remote}"
      git branch -l -f ${bl} -t ${branch_remote} >/dev/null;
    fi
  fi
done <<< "${branches}"

:

  • branches=$(git for-each-ref --format="%(refname) %(upstream) %(upstream:track)" refs/heads)
    , /
  • branch_checkedout=$(cat .git/HEAD|cut -f2 -d" "),
  • while read -r branch_line; do , " " ( )
+1

upstream master: origin, master.

git rev-parse --abbrev-ref --symbolic-full-name master@{u}

-, script /, Git 1.9 %(upstream:track):

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

, ( , , ) , .
, push- :

git config --get-regexp branch.master
+1

Source: https://habr.com/ru/post/1660200/


All Articles