Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have used <a href="https://www.kernel.org/pub/software/scm/git/docs/git-fetch.html" rel="noreferrer"><code>fetch</code></a> followed by <a href="https://www.kernel.org/pub/software/scm/git/docs/git-checkout.html" rel="noreferrer"><code>checkout</code></a> ...</p> <pre><code>git fetch &lt;remote&gt; &lt;rbranch&gt;:&lt;lbranch&gt; git checkout &lt;lbranch&gt; </code></pre> <p>... where <code>&lt;rbranch&gt;</code> is the remote branch or <em>source ref</em> and <code>&lt;lbranch&gt;</code> is the as yet <strong>non-existent</strong> local branch or <em>destination ref</em> you want to track and which you probably want to name the same as the remote branch or source ref. This is explained under <a href="https://www.kernel.org/pub/software/scm/git/docs/git-fetch.html#_options" rel="noreferrer">options</a> in the explanation of <code>&lt;refspec&gt;</code>.</p> <p><strong>Git is so smart it auto completes the first command if I <em>tab</em> after the first few letters of the remote branch. IE: I don't even have to name the local branch, Git automatically copies the name of the remote branch for me. Thanks Git!</strong></p> <p>Also as <a href="https://stackoverflow.com/a/1783426/1020470">the answer in this similar SO post</a> shows, if you don't name the local branch in <code>fetch</code>, you can still create it when you check it out by using the <code>-b</code> flag. <em>IE:</em> <code>git fetch &lt;remote&gt; &lt;branch&gt;</code> followed by <code>git checkout -b &lt;branch&gt; &lt;remote&gt;/&lt;branch&gt;</code> does exactly the same as my initial answer. And evidently if your repo has <em>only one</em> remote, then you can just do <code>git checkout &lt;branch&gt;</code> after <code>fetch</code> and it will create a local branch for you. <em>EG:</em> You just cloned a repo and want to check out additional branches from the remote.</p> <p>I believe that some of the documentation for <code>fetch</code> may have been copied verbatim from <a href="https://www.kernel.org/pub/software/scm/git/docs/git-pull.html" rel="noreferrer"><code>pull</code></a>. In particular the section on <code>&lt;refspec&gt;</code> in <a href="https://www.kernel.org/pub/software/scm/git/docs/git-pull.html#_options" rel="noreferrer">options</a> is the same. However, I do not believe that <code>fetch</code> will ever <a href="https://www.kernel.org/pub/software/scm/git/docs/git-merge.html" rel="noreferrer"><code>merge</code></a>, so that if you leave the destination side of the colon empty <code>fetch</code> <strong>should do nothing</strong>.</p> <p>NOTE: That <code>git fetch &lt;remote&gt; &lt;refspec&gt;</code> is short for <code>git fetch &lt;remote&gt; &lt;refspec&gt;:</code> which would therefore do nothing, but <code>git fetch &lt;remote&gt; &lt;tag&gt;</code> is the same as <code>git fetch &lt;remote&gt; &lt;tag&gt;:&lt;tag&gt;</code> which should copy the remote <code>&lt;tag&gt;</code> locally.</p> <p>I guess this is only helpful if you want to copy a remote branch locally, but not necessarily check it out right away. Otherwise I now would use <a href="https://stackoverflow.com/a/9537923/1020470">the accepted answer above</a>, which is explained in detail in the first section of the <a href="https://www.kernel.org/pub/software/scm/git/docs/git-checkout.html#_description" rel="noreferrer">checkout description</a> and later in the <a href="https://www.kernel.org/pub/software/scm/git/docs/git-checkout.html#_options" rel="noreferrer">options</a> section under the explanation of <code>--track</code>, since it's a 1-liner. <em>Well...</em> sort of a 1-liner, because you would <em>still</em> have to run <code>git fetch &lt;remote&gt;</code> first.</p> <p>FYI: The order of the <code>&lt;refspecs&gt;</code> (source:destination) explains the bizarre pre Git-1.7 method for <a href="http://git-scm.com/book/en/Git-Branching-Remote-Branches#Deleting-Remote-Branches" rel="noreferrer">deleting remote branches</a>. IE: Push nothing into the destination refspec.</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. 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