Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First use <a href="https://git-scm.com/docs/git-remote" rel="noreferrer"><code>git remote update</code></a>, to bring your remote refs up to date. Then you can do one of several things, such as:</p> <ol> <li><p><code>git status -uno</code> will tell you whether the branch you are tracking is ahead, behind or has diverged. If it says nothing, the local and remote are the same.</p></li> <li><p><code>git show-branch *master</code> will show you the commits in all of the branches whose names end in 'master' (eg <em>master</em> and <em>origin/master</em>).</p></li> </ol> <p>If you use <code>-v</code> with <code>git remote update</code> (<code>git remote -v update</code>) you can see which branches got updated, so you don't really need any further commands.</p> <p>However, it looks like you want to do this in a script or program and end up with a true/false value. If so, there are ways to check the relationship between your current <em>HEAD</em> commit and the head of the branch you're tracking, although since there are four possible outcomes you can't reduce it to a yes/no answer. However, if you're prepared to do a <code>pull --rebase</code> then you can treat "local is behind" and "local has diverged" as "need to pull", and the other two as "don't need to pull".</p> <p>You can get the commit id of any ref using <code>git rev-parse &lt;ref&gt;</code>, so you can do this for <em>master</em> and <em>origin/master</em> and compare them. If they're equal, the branches are the same. If they're unequal, you want to know which is ahead of the other. Using <code>git merge-base master origin/master</code> will tell you the common ancestor of both branches, and if they haven't diverged this will be the same as one or the other. If you get three different ids, the branches have diverged.</p> <p>To do this properly, eg in a script, you need to be able to refer to the current branch, and the remote branch it's tracking. The bash prompt-setting function in <code>/etc/bash_completion.d</code> has some useful code for getting branch names. However, you probably don't actually need to get the names. Git has some neat shorthands for referring to branches and commits (as documented in <code>git rev-parse --help</code>). In particular, you can use <code>@</code> for the current branch (assuming you're not in a detached-head state) and <code>@{u}</code> for its upstream branch (eg <code>origin/master</code>). So <code>git merge-base @ @{u}</code> will return the (hash of) the commit at which the current branch and its upstream diverge and <code>git rev-parse @</code> and <code>git rev-parse @{u}</code> will give you the hashes of the two tips. This can be summarized in the following script:</p> <pre><code>#!/bin/sh UPSTREAM=${1:-'@{u}'} LOCAL=$(git rev-parse @) REMOTE=$(git rev-parse "$UPSTREAM") BASE=$(git merge-base @ "$UPSTREAM") if [ $LOCAL = $REMOTE ]; then echo "Up-to-date" elif [ $LOCAL = $BASE ]; then echo "Need to pull" elif [ $REMOTE = $BASE ]; then echo "Need to push" else echo "Diverged" fi </code></pre> <p><em>Note:</em> older versions of git didn't allow <code>@</code> on its own, so you may have to use <code>@{0}</code> instead.</p> <p>The line <code>UPSTREAM=${1:-'@{u}'}</code> allows you optionally to pass an upstream branch explicitly, in case you want to check against a different remote branch than the one configured for the current branch. This would typically be of the form <em>remotename/branchname</em>. If no parameter is given, the value defaults to <code>@{u}</code>.</p> <p>The script assumes that you've done a <code>git fetch</code> or <code>git remote update</code> first, to bring the tracking branches up to date. I didn't build this into the script because it's more flexible to be able to do the fetching and the comparing as separate operations, for example if you want to compare without fetching because you already fetched recently.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload