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

Re: Git rescue mission

From
Jeff King <peff@peff.net>
Date
Feb 8, 2007, 23:24 UTC
Message-ID
<20070208232404.GA9493@coredump.intra.peff.net>
In-Reply-To
<17867.16740.875694.789664@lisa.zopyra.com>
On Thu, Feb 08, 2007 at 09:27:32AM -0600, Bill Lear wrote:
> > - never build on a branch that appears on the RHS of ':'.
> 
> This I don't quite understand.  So, if it is on the LHS, it is ok?
> But, if it is ALSO on the RHS it is not?
If it is on the LHS, it's talking about the branch in the _remote_ repo.
Show 6 quoted lines
> So, this:
> 
>       Pull: refs/heads/topic:refs/heads/topic
> 
> really means don't don't work on a branch named topic in this
> repository?

It means to fetch the remote's branch "refs/heads/topic" and store the current head in _your_ "refs/heads/topic" as a tracking branch, but only if it's a fast-forward. Yes, I know it says "Pull:", but it's really about fetching.

You can then merge the result of that fetch into your current branch. So what you want is something like:

Pull: refs/heads/topics:refs/heads/remote-topic

which will use 'remote-topic' as a tracking branch, always updating it at fetch time to reflect the remote's version of topic. You can then merge remote-topic into your local topic branch by fetching+merging, or by doing 'git-pull remote refs/heads/topic:refs/heads/remote-topic'.

> I assume by "build on" you mean "work, compile, check stuff in,
> etc."?.  Did you have something else in mind when you said "build on"?

I believe he means 'store commits in'. That is, the RHS of the refspec should be used solely for tracking fetches from the remote. If you make a commit on top of it (either directly, or by doing a merge), then the fetch must either throw away your commits, or fail to fast-forward to the remote's new position for that branch.

> I don't currently have any 'refs/remotes' of any sort, so I guess you
> mean that the new principle, using git clone --use-separate-remote
> will effect this.

Yes. It will basically give you a RHS of "refs/remotes/$REMOTE/$BRANCH" to track a branch $BRANCH coming from $REMOTE (generally "origin"); it's a more organized way of doing what I mentioned above (RHS of refs/heads/remote-topic).

Show 6 quoted lines
> >(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, using 1.4.4 series, or 1.5, the "sane" way to work in git
> is to use clone --use-separate-remotes.

Most people think so (though I think Junio actually still uses the traditional layout, because he finds it more convenient). Note that in 1.5, --use-separate-remote is the default (the option is still accepted, but has no effect; note also that the option is singular).

> I assume by this you mean that if I do the separate remote trick, I
> will not shoot myself by doing a 'git pull' while on my topic branch,
> as the setup will cause git to refuse to do it.

No, you will not shoot yourself because the fetch part of the pull will store the remote's position of 'refs/heads/topic' at 'refs/remotes/origin/topic' instead of trying to overwrite the branch you've been working on.

As an additional bonus, you can put this in your .git/config:

[branch "topic"] remote = origin merge = refs/heads/topic

which means "When I'm on my refs/heads/topic branch and I issue a git-pull without any arguments, do a git-fetch on origin. Then, merge what the remote end calls refs/heads/topic into my current branch." Without this, git-pull in v1.4.* will attempt to merge the remote's 'master' branch. In v1.5, I believe it will refuse to make a merge.

Show 15 quoted lines
> Ok, so if I am on master, I do this:
> 
> [master] % git pull
> 
> and this will fetch the remote master and merge it to my master, and
> fetch the remote topic and merge it to my local topic.
> 
> While, if I am on my topic branch, if I do this:
> 
> [topic] % git pull
> 
> it sill fetches from the remote master and the remote topic, but will
> not merge at all.
> 
> Could you verify if I have stated your position correctly?

That is correct. If you add the config I mentioned above, you can get the "automatically merge from remote topic into my topic" behavior that you get for master.

Show 9 quoted lines
> If I am, this still seems bizarre.  I really just want a way to sync
> two repos that works consistently, and is invoked consistently, no
> matter what branch I am currently on.  And, again, by "sync", I just
> mean no cross-branch merging --- no "crossing of the streams".  Even
> if it were limited to syncing the current branch only, that would be
> ok, but this variable behavior seems rather odd and confusing.  In
> other words, I just want to type the equivalent of 'git sync' and have
> it work, and not have to give a branch name, or be in the "right
> place" for it to work as I expect.

Syncing repositories doesn't involve pulling. It just involves fetching. So in either case, you can simply do a 'git-fetch origin'. In v1.5, the repositories won't be exactly identical; your remote branches will be stored in refs/remotes/origin instead of refs/heads.

However, your original setup is still broken for syncing. You are doing local work on refs/heads/topic, but you also are claiming you want to store the sync of the remote in refs/heads/topic. You obviously can't do both.

> Thus, I don't want to have to think "oh, I'm on my topic branch, and
> if I really want to sync from my remote repo, I need to get on my
> master branch".  It seems that the only difference in the "insane" way

Maybe I don't understand what you mean by sync here, but I don't see the mental leap. Whenever you fetch, from whatever branch, using the 'origin' remote, it will update all tracking branches in your local repository. You can then selectively do merges to any local branches you're working on. You _can't_ do an operation that is "for every local branch I have, merge the matching remote branch into my local branch". And I don't think you'd want to: a merge may or may not be a trivial thing, since it might have conflicts.

The workflows I suspect you want are:
1. I'm working on my topic, and somebody else is working on topic. Pull
   their work from the remote 'origin':
     git checkout topic
     hack hack hack
     git commit
     git pull origin
  Using separate remotes, you'll get a copy of the remote topic branch
  in your refs/remotes/origin/topic. If you have the config magic I
  mentioned above, it will automagically pull from his topic branch. If
  not, it will either pull from his master (v1.4) or complain (v1.5).
2. I'm working on my topic, and now I want to pull from the remote
   master to do some testing.
     git checkout topic
     hack hack hack
     git commit
     git fetch origin
     git merge origin/master
   This will only work in v1.5.  Using separate remotes, the
   'origin/master' branch refers to the 'refs/remotes/origin/master'
   tracking branch. You could do it in v1.4 like this (without using a
   tracking branch at all):
     git pull origin master
3. I'm interested in what's happening on the topic branch, but I don't
   care about making local commits. I just want to build it.
     git checkout origin/topic
     build build build
   It assumes you are tracking using separate remotes (hence the
   origin/topic branch). This will _only_ work in v1.5, since you're
   not allowed to checkout any non-branch refs in v1.4 (including tags
   or remote tracking branches). The moral equivalent in v1.4 using
   separate remotes is:
     git checkout -b topic origin/topic
     build build build
   which leaves you with your own local 'topic' branch, which you can
   then merge into if you want.
Hope that makes sense.
-Peff
Previous: Jakub NarebskiNext: Bill Lear
Message 12 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.