Search code examples
gitgit-rev-parse

Git find first non-local commit


Related: List Git commits not pushed to the origin yet

git rev-parse HEAD gives me the latest commit in the workspace, but this can be a locally committed githash. In other words, a commit which is not pushed to remote yet

how do I find the latest commit which also exists in remote in the workspace


Solution

  • To get the latest commit on the currently checked out branch's configured remote branch, do

    # first get your remote-tracking branches up-to-date with remote
    git fetch
    
    # then do
    git rev-parse @{upstream}
    # or even just
    git rev-parse @{u}
    

    (Note : @{upstream} / @{u} are not placeholders, they're meant to be typed as is)

    From the doc :

    [<branchname>]@{upstream}, e.g. master@{upstream}, @{u}
    The suffix @{upstream} to a branchname (short form @{u}) refers to the branch that the branch specified by branchname is set to build on top of (configured with branch..remote and branch..merge). A missing branchname defaults to the current one.