2
  • I am looking for the step by step commands to be run.
  • Assuming that the required branch may not yet be present in my local repository, and my workspace could be pointing to another branch, what set of commands should I run, so that I get only the minimum required data. Should I try to get the log information first, so that I can get the revisions corresponding to a branch?

Or should I do a git fetch of that branch and then get the revisions corresponding to that branch?

Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144

2 Answers2

0

(Assuming your remote is called "origin" and your branch is called "master")

First you want to make sure the branch in your local repository is up to date with the remote equivalent:

git fetch origin master

Then you can get the latest commit hash:

git log -1 --format="%H" origin/master

Related: https://stackoverflow.com/a/13944180/1973409

Community
  • 1
  • 1
816-8055
  • 509
  • 7
  • 15
  • I observed that git fetch does not reflect the new revisions in git log output. However, a git push does so. Please update your answer or correct me if I am missing something. – Sandeepan Nath Aug 10 '16 at 12:35
  • Make sure to include the `origin/master` part. Alternatively you could use the `--all` option. I think you meant pull and not push in your comment, which would also work, but also updates the workspace (unlike fetch). – 816-8055 Aug 10 '16 at 13:04
  • I got confused. And yes I meant pull. I was about to ask what is the difference between `git log` and `git log origin master`? Why does `git log` not update the info even after a fetch? – Sandeepan Nath Aug 10 '16 at 13:06
  • 1
    See [this image](http://assets.osteele.com/images/2008/git-transport.png) for reference. `git log` shows the log for the current branch of your workspace (`git log master` the same for the branch "master" of your workspace). If your workspace hasn't been updated with `git pull` yet, it may contain fewer commits than your remote repo. `git log origin/master` shows the commit log for the **reference** to the branch "master" of the remote repo "origin". This reference has to be updated with the info from the real remote repo (may be in the internet etc) though. To do this, we do `git fetch`. – 816-8055 Aug 10 '16 at 13:48
0
git ls-remote origin master

will get you every ref on on the remote that matches "master", ls-remote is lower level than the ref-naming conventions. Branch refnames are names that start refs/heads/.

You can list any repository's refs directly, try

git ls-remote git://git.kernel.org/pub/scm/git/git.git refs/heads/master

or just leave off the refname pattern for example.

This also works on the current repo, try

git ls-remote .

in the obvious place.

jthill
  • 55,082
  • 5
  • 77
  • 137