git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: How to specify remote branch correctly

From
Andrew Ardill <andrew.ardill@gmail.com>
Date
Dec 17, 2012, 04:27 UTC
Message-ID
<CAH5451=7frqa-YHXubvO=dMK2CvVoWR-VFZ3XCmKouNiQz4gAg@mail.gmail.com>
In-Reply-To
<slrnkct0r3.dsp.narkewoody@zuhnb712.local.com>
On 17 December 2012 13:30, Woody Wu <narkewoody@gmail.com> wrote:
Show 7 quoted lines
> 1. git checkout foo
> 2. git checkout origin/foo
>
> The first method run silently with success, but the second method
> complains that I got a 'detached HEAD'.  So, I think I don't understand
> the difference between 'foo' and 'origin/foo'.  Can someone give me a
> hint?
Hi Woody,

I think you are just missing a couple of important distinctions that git makes about the different references that exist in your repository.

A remote reference (origin/foo) describes exactly the state of somebody else's branch at the time you last synchronised with them. It does not make sense for you to be able to 'edit' this state, as it doesn't belong to you. Instead, we create a copy of that reference and give it a name (git checkout foo origin/foo) and call this a local reference (foo). Git then provides machinery around keeping these in sync with each other (git branch --set-upstream foo origin/foo) but we don't _have_ to keep these in sync at all! In fact, the names can be completely arbitrary and we don't have to track the upstream at all.

If I have some other remote (remote-x) that has the same branch as origin but with some other changes I want to look at, we can just check that out to another branch (git checkout remote-x-foo remote-x/foo), or simply download it as a remote ref and merge the changes on top of my existing local branch (git fetch remote-x; git checkout foo; git merge remote-x/foo).

There are lots of patterns that can emerge from this functionality, but the main thing to remember is that to create changes on top of a remote branch, we first need to create a local copy of it. A 'detached HEAD' here means that we are looking at the remote repository's branch but don't have a local copy of it, so any changes we make might be 'lost' (that is, not have an easy to find branch name).

Regards,
Andrew Ardill
Previous: Woody WuNext: Woody Wu
Message 2 of 11 in “How to specify remote branch correctly”
  1. Woody WuDec 17, 2012
  2. Andrew ArdillDec 17, 2012
  3. Woody WuDec 17, 2012
  4. Andrew ArdillDec 17, 2012
  5. Tomas CarneckyDec 17, 2012
  6. Andrew ArdillDec 17, 2012
  7. Chris RorvickDec 17, 2012
  8. Woody WuDec 17, 2012
  9. Tomas CarneckyDec 17, 2012
  10. Woody WuDec 17, 2012
  11. Woody WuDec 17, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.