Search code examples
gitgit-clonegit-log

git log not showing latest commits in cloned repository


A while ago one of my team members cloned my code by using

git clone --bare -l /home/jaimemontoya/public_html/app/public.git /home/johndoe/app/public.git

He continued coding and when he uses git remote -v he gets this:

origin    ssh://johndoe@www.example.com/home/johndoe/app.git (fetch)
origin    ssh://johndoe@www.example.com/home/johndoe/app.git (push)

He had the latest code so at one point I wanted to get the latest version of his code by using this:

git clone --progress -v "ssh://jaimemontoya@example.com/home/johndoe/app.git" "C:\Users\jaimemontoya\Apps\Marketing\app"

To my surprise, when I navigate to C:\Users\jaimemontoya\Apps\Marketing\app and use git log -1, I'm getting a very old commit, not the most recent one that my team member has when he uses git log -1 from his local. He claims to have already pushed to remote, meaning ssh://johndoe@www.example.com/home/johndoe/app.git should already have his latest code so that when I clone from it, I should also be able to fetch the latest. My first guess is that he may not have pushed to remote. But assuming he has already done it, what could explain why I'm not being able to retrieve the latest code from ssh://johndoe@www.example.com/home/johndoe/app.git?

Note: I used root to try to explore ssh://johndoe@www.example.com/home/johndoe/app.git and see what I get:

root@sub.example.com [~]# cd ../home/johndoe/app.git/
root@sub.example.com [app.git]# ls -al
total 68
..................
..................
drwxr-x--x 5 johndoe gitgrp 4096 Nov  25  2021 app
..................
..................
drwxr-x--x 7 johndoe gitgroup 4096 Nov  25  2021 .git
..................
..................
root@sub.example.com [androidapp.git]# git log -1

I'm still seeing an old commit, not the latest one that my team member claims to have already pushed to remote. This happens even after logging in as root to the server where the code is being hosted. This is a bare repository. However, shouldn't .git list the latest code pushed to ssh://johndoe@www.example.com/home/johndoe/app.git, when use use git log -1 from /home/johndoe/app.git/ on the server where the remote repository is hosted? Thank you.


Solution

  • git log by default shows the history for the HEAD. So git log -1 is simply showing you the commit pointed to by HEAD ref. Most likely your team member's latest code was pushed onto a branch other than the one pointed to by HEAD, or for some reason HEAD in the remote repo was not updated to point to the latest commit they pushed.

    You can verify this with the following command either on the remote or your clone of the remote:

    git log --graph --pretty=format:'%C(yellow)%h%Creset %Cgreen(%cd) %C(bold blue)<%an>%Creset %C(red)%d%Creset %s' --all
    

    If my conjecture is correct, you will see in the history graph HEAD pointing to a very old commit (further down in the history graph), and you will see your team member's newer commits on a different branch. You will find the name of that branch next to the last commit of that branch (in red if you use my command above).

    How did this happen? There are many possibilities, such as:

    • your teammate created a new branch for their commits.
    • your repo was not pointing to the main/master branch when your teammate cloned it. When you clone a repo, not only are all of the branches cloned, but so is the current value of HEAD. Your teammate then perhaps switched to the main/master branch before making their changes.