调整脚本以拉动所有遥控器分支

时间:2016-11-08 15:38:29

标签: 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)后面的遥控器被拉了但是我仍然可以看到遥控器背后的主分支如下图所示:

enter image description here

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

enter image description here

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

enter image description here

那么,我怎么能调整脚本以拉动所有"当地分公司?

2 个答案:

答案 0 :(得分:1)

首先检查您的upstream branch for master:它可能不是origin,这就是为什么从原点拉取可能对master没有任何影响。

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

其次,要在脚本中显示前后提交的数量,请使用Git 1.9 syntax %(upstream:track)

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

请注意,上游分支(或远程跟踪分支,即您要从中提取的分支)可能与您要推送的分支不同。
检查您是否看到了推送网址:

git config --get-regexp branch.master

答案 1 :(得分:1)

另外to my previous answer,我想补充一点,你的脚本应该只限制给定分支到它上游分支的合并或重置。
因此我使用了isEmpty = (adoRs.BOF And adoRs.EOF)

以下是我刚刚测试过的脚本:

  • git version 2.11.0.windows.1
  • SourceTree 1.9.10.0

那是:

%(upstream:track)

请注意使用:

  • #!/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" ")仅在带有上游的分支上循环 ,而不是“所有远程”(可能包含或不包含给定分支)