git branch -r vs git remote show origin

2019-09-18 08:29发布

问题:

So I was looking for a way to see a list of all the branches that exist on the remote, and found the following 2 commands:

1. git branch -r 2. git remote show origin

Whats the difference between the 2 commands?

回答1:

The git branch command looks at (or, invoked differently, modifies) your own repository's information. For remote-tracking branches (git branch -r), this shows what's in your cached copy of what was on the remotes the last time you had your git contact those remotes and get updates.

The git remote show name command, by default, runs git ls-remote, which actually calls up the remote's server over the Internet-phone (or whatever other transport you use) and gets information from it right now. This is what would be put into your cached copy, if you ran git fetch. (Note that if you do run git fetch afterward, what you get by then could be totally different, since even a few milliseconds can be plenty of time to have massive changes occur. It all depends on how active the remote is.)

You can tell git remote show to use only your cached copy, rather than calling up the remote on the Internet-phone. In this case, the two commands use the same basic information (but present it very differently—git remote show is intended to help you show what fetch and push would do, while git branch -r simply lists what's in your cache).