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

Re: Git rescue mission

From
Junio C Hamano <junkio@cox.net>
Date
Feb 8, 2007, 00:48 UTC
Message-ID
<7vr6t13251.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<17866.27739.701406.722074@lisa.zopyra.com>
Bill Lear <rael@zopyra.com> writes:
> 2) Why does git pull do the right thing when on master, but seemingly
> changes behavior when on topic?
Because you told it to.
      %  cat .git/remotes/origin
      URL: /repos/git/project
      Pull: refs/heads/master:refs/heads/origin
      Pull: refs/heads/topic:refs/heads/topic

It tells "git pull" to drive "git fetch" to copy their master to your origin and overwrite your topic with their topic, and then merge their master to whatever branch you are currently on.

The sane/safe thing to do in the traditional layout (I'll talk about non-traditional one in a second) is:

 - do your 'pull' only and always while on your 'master' and not
   anywhere else.
 - never build on a branch that appears on the RHS of ':'.

This layout is convenient when you always do fetches and pulls while on 'master', but has burned enough people. So what people on the list seem to recommend is to use a separate remote layout in the repository.

The principle is:
 * The branches you work on in the repository are kept in refs/heads/
 * Copies of branches from other repositories (it does not
   matter who is in control of them -- some of them may be your
   repository) are kept in refs/remotes/<symbolic name>.

So the current "git clone" (if you are using 1.4.4 series, you can say "git clone --use-separate-remote") creates something like this instead:

      URL: /repos/git/project
      Pull: refs/heads/master:refs/remotes/origin/master
      Pull: refs/heads/topic:refs/remotes/origin/topic

(git-clone from 1.5.0 does not actually make remotes/origin file in .git/ that has the above -- it creates the moral equivalent in .git/config).

So whatever you do the first step of "git pull", which is "git fetch", will _not_ overwrite the current branch.

In order to prevent merging their 'master' into your 'topic' when you are on 'topic', git-fetch/git-pull from 1.5.0 uses further safety which is left by 'git clone'. The real configuration created by 'git clone' looks like this:

	[remote "origin"]
        	url = /repos/git/project
                fetch = refs/heads/*:refs/remotes/origin/*
	[branch "master"]
        	remote = origin
                merge = refs/heads/master

The 'fetch' lines correspond to 'Pull' in .git/remotes/origin file; it uses globbing pattern so if there are new branches on the remote side you can automatically track them, which is a plus.

But more importantly, when 'fetch' lines only do the globbing pattern, 'git pull' without explicitly saying which remote branch you want to merge to the current branch (perhaps by mistake) refuses to do a merge, if there is no branch.*.merge configuration ("refs/heads/master" in the above example). So with the above configuration, 'git pull' from 1.5.0 will fetch two remote branches and keep them in remotes/origin/master and remotes/origin/topic, and if you are on 'master' their refs/heads/master is merged into your current branch, but if you are on 'topic', it will not do the merge step (this only applies to "git pull" without any refspec parameters).

With 1.4.4 series, I think you can create the [branch] section yourself and do something like this:

	[remote "origin"]
        	url = /repos/git/project
                fetch = refs/heads/master:refs/remotes/origin/master
                fetch = refs/heads/topic:refs/remotes/origin/topic
	[branch "master"]
        	remote = origin
                merge = refs/heads/master
	[branch "topic"]
        	remote = origin
                merge = refs/heads/topic

This configuration also works with 1.5.0, so if you are happy with this (I am assuming you are using 1.4.4 series, preferably the last one, 1.4.4.4), after you upgrade to 1.5.0, it will continue to do its thing (but you would want to upgrade the fetch line).

Previous: Bill LearNext: Alexander Litvinov
Message 6 of 51 in “Git rescue mission”
  1. Bill LearFeb 8, 2007
  2. Johannes SchindelinFeb 8, 2007
  3. Bill LearFeb 8, 2007
  4. Johannes SchindelinFeb 8, 2007
  5. Bill LearFeb 8, 2007
  6. Junio C HamanoFeb 8, 2007
  7. Alexander LitvinovFeb 8, 2007
  8. Junio C HamanoFeb 9, 2007
  9. Alexander LitvinovFeb 9, 2007
  10. Bill LearFeb 8, 2007
  11. Jakub NarebskiFeb 8, 2007
  12. Jeff KingFeb 8, 2007
  13. Bill LearFeb 8, 2007
  14. Linus TorvaldsFeb 8, 2007
  15. Kalle PokkiFeb 8, 2007
  16. Linus TorvaldsFeb 8, 2007
  17. Kalle PokkiFeb 8, 2007
  18. Shawn O. PearceFeb 8, 2007
  19. Theodore TsoFeb 9, 2007
  20. Shawn O. PearceFeb 9, 2007
  21. Jakub NarebskiFeb 9, 2007
  22. Theodore Ts'oFeb 10, 2007
  23. Print a sane error message if an alias expands to an invalid git commandTheodore Ts'o, Feb 10, 2007
  24. Allow aliases to expand to shell commandsTheodore Ts'o, Feb 10, 2007
  25. Linus TorvaldsFeb 10, 2007
  26. Theodore TsoFeb 10, 2007
  27. Johannes SchindelinFeb 10, 2007
  28. Theodore TsoFeb 11, 2007
  29. Johannes SchindelinFeb 11, 2007
  30. Theodore TsoFeb 11, 2007
  31. Johannes SchindelinFeb 11, 2007
  32. Junio C HamanoFeb 11, 2007
  33. Johannes SchindelinFeb 11, 2007
  34. Theodore TsoFeb 12, 2007
  35. Shawn O. PearceFeb 12, 2007
  36. Junio C HamanoFeb 10, 2007
  37. Kalle PokkiFeb 9, 2007
  38. Bill LearFeb 8, 2007
  39. Linus TorvaldsFeb 8, 2007
  40. Bill LearFeb 8, 2007
  41. Bill LearFeb 8, 2007
  42. Shawn O. PearceFeb 8, 2007
  43. Bill LearFeb 8, 2007
  44. Shawn O. PearceFeb 8, 2007
  45. Jakub NarebskiFeb 9, 2007
  46. Linus TorvaldsFeb 9, 2007
  47. Michael S. TsirkinFeb 9, 2007
  48. Jakub NarebskiFeb 8, 2007
  49. Linus TorvaldsFeb 8, 2007
  50. Junio C HamanoFeb 9, 2007
  51. Jakub NarebskiFeb 8, 2007

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.