Git status over all repo's

后端 未结 4 983
隐瞒了意图╮
隐瞒了意图╮ 2020-12-06 07:17

What tools, if any, are available to show the status of my git repo\'s and how much they are forward/behind versus their remotes? I\'m imagining something like this exists:<

4条回答
  •  执念已碎
    2020-12-06 07:55

    I'm not entirely sure what it means to say that one repo is ahead or behind another repo. They are just sets, and without any sort of reference, there is no ordering. You could say that the local repo has commits that the remote one doesn't. But you can't say whether it's ahead or behind without talking about branches. You might be able to say whether a repo is up-to-date or not though.

    Also, when communicating with a remote, the local git client has no idea how many commits are in-between the local branch tip and the remote one. It won't know that until the data has been transferred locally.

    What you can do instead, is run git fetch --all, and have a script like this (I called it `git-repo-status locally):

    #!/bin/bash
    
    # Taken from http://stackoverflow.com/questions/620650/can-i-easily-update-all-local-git-branches-from-remote-branches-simultaneously/answer-9076361
    # tweaked by me to help give status of local versus upstream branches.
    main() {
      REMOTES="$@";
      if [ -z "$REMOTES" ]; then
        REMOTES=$(git remote);
      fi
      REMOTES=$(echo "$REMOTES" | xargs -n1 echo)
      echo "$REMOTES" | while read REMOTE; do
        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";
    
          # This is in reference to the local branch.
          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 -a "$NAHEAD" -gt 0 ]; then
            echo "$LB <--> $REMOTE/$RB: -$NBEHIND +$NAHEAD";
          elif [ "$NBEHIND" -gt 0 ]; then
            echo "$LB <--> $REMOTE/$RB: -$NBEHIND";
          elif [ "$NAHEAD" -gt 0 ]; then
            echo "$LB <--> $REMOTE/$RB: +$NAHEAD";
          fi
        done
      done
    }
    
    main $@
    

    Running it produces something like:

    :: git repo-status
    master <--> upstream/master: -14
    

    I realize it's not a direct answer to your issue, but I don't think what you want is possible without running git fetch --all and grabbing the revisions from upstream. If you're willing to do that, then the above may be useful to you.

提交回复
热议问题