The git log
command will show you commit logs your repository. But it only shows the commits of your local repository. What can you do to view the commit logs of a remote repository?
There’s no way to directly query the remote repository. Instead, you must add it first (if it doesn’t already exist):
1 |
git remote add origin <remote reference> |
The fetch the changes:
1 |
git fetch |
And finally do a git log specifying the remote name and branch name:
1 |
git log remotename/branchname |
Here’s how this works. The git log
command is of the following syntax:
1 |
git log [<options>] [<revision range>] [[--] <path>...] |
By default <revision range>
is HEAD
– i.e. the whole history up to the current state of the tree. Note that it is a range. To explain that look at the following output from my repository:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
> git log 98b6fa9cf13b5d39c593fb4e06a938b8294afdf4 Dir name changes. 6577b70101b0192ff2b2e09b208366dfb8e502b3 Modified install.bat with correect paths. cecc83fb7a5fee986ab823b8c9b2a94466eac314 Added comments to fixperms.bat. f94775014e702f47cbcd7e17677583f6b547d5be Added comments to fixperms.bat. 9b5281d707801a6269c674becbb77d7fce7b7d8b Added LICENSE 7af03482fecb0965c3afdd25e64a7c625f5f0867 README.md changes 50d6e00b42ce73ad89f61fc1263dd5beea194e2d README.md changes d4c5d40f4f3a49e089bf5a6c76d0a7820674e15c fixperms changes before lunch 5d0b25f86bf1b268eb7a05a10eadbfc3a295f830 fixperms changes 53d25614774bf86c6e36bbc174770b03986dbee3 moved fixperms.bat to a folder of its own 36e0d7bd0a716792898b871027794d564dd2b758 added modifications to the laptop lid closing script 06ead67289a71cef0619a438985e05209511f57e added my older scripts for laptop lid closing 39e3d402d43e3717dd5806d29be4b42f1d1036c6 fixperms.bat added 48dbd1199584df3b882aad05314af5785a84771d first commit |
On the left side you have the commits/ revisions. HEAD
currently points to the top most one in the list. You can specify a range of commits you want the logs for, like:
1 2 3 |
> git log 39e3d402d43e3717dd5806d29be4b42f1d1036c6..36e0d7bd0a716792898b871027794d564dd2b758 36e0d7bd0a716792898b871027794d564dd2b758 added modifications to the laptop lid closing script 06ead67289a71cef0619a438985e05209511f57e added my older scripts for laptop lid closing |
Here I am asking for logs from the second from last commit (“fixperms.bat added”) to the fourth from last commit (“added modifications to the laptop lid closing script”). Result is the third from down and fourth from down commit. So the range is always from the earlier commit to the later commit. If I reverse the range, for instance, I get nothing:
1 |
> git log 36e0d7bd0a716792898b871027794d564dd2b758..39e3d402d43e3717dd5806d29be4b42f1d1036c6 |
When you omit a range, it defaults to HEAD
, which means everything from the first commit to the latest.
Also worth knowing, you can refer to previous commits starting from HEAD
via HEAD~1
, HEAD~2
, etc. Thus you can do the following:
1 2 3 |
> git log HEAD~2..HEAD 98b6fa9cf13b5d39c593fb4e06a938b8294afdf4 Dir name changes. 6577b70101b0192ff2b2e09b208366dfb8e502b3 Modified install.bat with correect paths. |
Check out this StackExchange answer on what the ~ (and ^) operators stand for. Also worth looking at the git-rev-parse
help page for a diagram explaining this.
Back to git log
the range can also specify an remote name and branch. So you could have:
1 2 3 4 5 6 7 |
> git log HEAD..origin > git log HEAD..origin/master > git log origin/master..HEAD > git log origin..HEAD |
These simply show the commits that are present in HEAD
(of your local copy) but not present in origin
or origin/master
. Or the reverse, as I do in the last two commands. (You can also omit HEAD
, leaving the two dots, so ..origin
is same as HEAD..origin
, and origin..
is same as origin..HEAD
.)
The word origin
in this case is the name of your remote repository but is also a pointer to the HEAD
of that remote repository. And that’s why you can do git log origin
to get all changes on the remote end because it too marks a range. Or you can compare between remote repository and local repository.
The origin
is only updated locally once you do a fetch
, which is why you must do a git fetch
first.