Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make git-status check two different remotes?

Tags:

git

git-status

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.)

like image 504
Max Yankov Avatar asked May 04 '14 11:05

Max Yankov


People also ask

How do I track remote git repository?

You can tell Git to track the newly created remote branch simply by using the -u flag with "git push".

How do I diff a remote branch?

1 Answer. You can use git branch -a to list all branches then choose the branch name from the list from the remote branch name. Example: git diff master origin/master (where "master" is a local master branch and "origin/master" is a remote namely origin and master branch.)

How do I tell the difference between local master and remote master?

You can git branch -a to list all branches (local and remote) and then choose the branch name from the list (just remove remotes/ from the remote branch name. Example: git diff main origin/main (where "main" is the local main branch and "origin/main" is a remote, namely the origin and main branch.)


2 Answers

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
like image 189
jthill Avatar answered Oct 20 '22 17:10

jthill


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.

like image 23
Brent Faust Avatar answered Oct 20 '22 17:10

Brent Faust