2

I’m currently contributing to a GitHub repository upstream from which I forked origin. After cloning the forked repo, I’m confused about whether I should checkout remote branches from upstream or origin. If I checkout a remote branch x from origin, and then later pull from upstream, will new changes to branch x in upstream be merged into my remote branch x checked out from origin? If not, and I instead checkout remote branch x from upstream, when I want to push this branch, do I have to specify origin as the destination repo? If I do git push -u origin x doesn’t this reset the tracking remote of branch x to origin?

Emerson
  • 21
  • 1

1 Answers1

0

If I checkout a remote branch x from origin, and then later pull from upstream, will new changes to branch x in upstream be merged into my remote branch x checked out from origin

You don't pull from upstream, you fetch only.

Then you rebase your branch x on top of upstream/master
Then you force push it to origin.

See also "Pull new updates from original GitHub repository into forked GitHub repository"

The first push is a git push -u origin x.
The other pushes are simple git push, except when you rebase x on top of upstream/master, in which case it is (it no other people are working on origin/x) git push --force.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I'm still somewhat of a Git newbie, but what is the point of a `git pull` then? My understanding is that `git pull` is essentially a `git fetch` followed by a `git merge`. If you're saying not to pull from `upstream` it makes no more sense to pull from `origin` since `origin` is never going to be in a more up-to-date state than the clone of `origin`. – Emerson Mar 16 '17 at 20:17
  • @Emerson git pull merges (it fetches from origin, which is not very interesting, and then merges origin/x to x). When you want to update from upstream, you don't want to merge, but to rebase (ie replay your commit on top of the new base). That will make the future merge/pull request trivial to be applied. – VonC Mar 16 '17 at 20:22
  • @Emerson You don't pull from origin since it is your fork and you are the only one working on (ie pushing to) it. – VonC Mar 16 '17 at 20:23
  • Okay, thanks for the clarification VonC. And just to confirm, I checkout the remote branch from `upstream`? (I think this is what was implied in your answer, but wanted to make sure) – Emerson Mar 16 '17 at 20:27