How can I make git-status check two different remo

2019-03-25 20:11发布

问题:

Every git user is accustomed to this:

> git status
On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working directory clean

However, recently I've started working with two remotes instead of one (heroku and github, pretty standard situation, I think), and it started to annoy me to only see 1 origin in git status output.

How can I add other remote so I would see something like this?

> git status
On branch master
Your branch is up-to-date with 'origin/master'.
Your branch is up-to-date with 'heroku/master'.

nothing to commit, working directory clean

(This question has nothing to do with heroku or github, it's just a convenient example.)

回答1:

git status is the status of your worktree, one-branch-at-a-time status.

if you want to see all-branch status, do

git branch -avvv


回答2:

git status only shows the relative status to the remote tracking branch. But it's easy to change the remote tracking branch temporarily:

git branch -u <remote>/<branch>

Then git status will show the status of that branch.

Note that the changes displayed are the same, but the number of commits ahead/behind for the current remote tracking branch are properly displayed.

A bash script to get all remote branch status:

for o in $(git remote -v | grep fetch | cut -f 1 -); do # remote branch names
  git branch -u $o/master  # set remote tracking branch (git v1.8+ syntax)
  git status
  echo --------------------------------   # separator
  git branch -u origin/master >/dev/null  # restore original tracking branch
done

To get the status of both of your origins using the single command git s:

git config --global alias.s "for o in $(git remote -v | grep fetch | cut -f 1 -); do git branch -u $o/master; git status; echo; git branch -u origin/master >/dev/null; done"

This adds an alias to your ~/.gitconfig file (which you can later edit to change either the main remote branch or the command s).

Note that origin/master is hard-coded as the default branch. To work with any branch, without hard-coding, the script above could be modified to get the current remote+branch first, then restore it.