git – 调整脚本以拉出所有遥控器分支

我已经开始在另一个
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 $@

它到目前为止工作得很好,但我想知道如何调整它以拉动所有分支,由于某种原因它不会拉动某些本地分支.

第一个例子:后面有4个本地分支,master是当前的一个,在运行这个脚本后,几个分支(4)后面的遥控器被拉了但是我仍然可以看到遥控器背后的主分支,如下图所示:

《git – 调整脚本以拉出所有遥控器分支》

第二个例子:在运行脚本之前我得到了这个,后面有2个本地分支,当前的一个(发布)很好:

《git – 调整脚本以拉出所有遥控器分支》

我运行脚本,我得到了这个:

《git – 调整脚本以拉出所有遥控器分支》

那么,我怎么能调整脚本来拉动“所有”本地分支?

最佳答案 另外
to my previous answer,我想补充一点,你的脚本应该只限制给定分支的合并或重置到它的上游分支.

因此我使用%(上游:轨道)

这是我刚刚测试过的脚本:

> 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)%(上游:track)”refs / heads)
获取完整的上游分支名称和前进/后退状态
> branch_checkedout = $(cat .git / HEAD | cut -f2 -d“”)获取签出分支的名称
> read -r branch_line; do仅在具有上游的分支上循环,而不是“所有远程”(可能包括或可能不包括给定分支)

点赞