102

I was reading on: https://wiki.diasporafoundation.org/Git_workflow#Rebase_your_development_branch_on_the_latest_upstream

Here is an extract:

Your Repository Up to Date

In order to get the latest updates from the development trunk do a one-time setup to establish the main GitHub repo as a remote by entering:

$ git remote add upstream git://github.com/diaspora/diaspora.git

Rebase Your Development Branch on the Latest Upstream

To keep your development branch up to date, rebase your changes on top of the current state of the upstream master. See the What’s git-rebase? section below to learn more about rebasing.

If you’ve set up an upstream branch as detailed above, and a development branch called 100-retweet-bugfix, you’d update upstream, update your local master, and rebase your branch from it like so:

$ git fetch upstream

$ git checkout master

$ git rebase upstream/master

$ git checkout 100-retweet-bugfix

[make sure all is committed as necessary in branch]

$ git rebase master

Why is adding a 'remote upstream' needed in this case? Coudn't I have just done:

$ git checkout master

$ git pull origin master

$ git checkout 100-retweet-bugfix

[make sure all is committed as necessary in branch]

$ git rebase master
tshepang
  • 12,111
  • 21
  • 91
  • 136
ben39
  • 2,427
  • 4
  • 20
  • 19

4 Answers4

111

The wiki is talking from a forked repo point of view. You have access to pull and push from origin, which will be your fork of the main diaspora repo. To pull in changes from this main repo, you add a remote, "upstream" in your local repo, pointing to this original and pull from it.

So "origin" is a clone of your fork repo, from which you push and pull. "Upstream" is a name for the main repo, from where you pull and keep a clone of your fork updated, but you don't have push access to it.

codekaizen
  • 26,990
  • 7
  • 84
  • 140
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 1
    Thank you @manojlds! I have a follow up question then here: [link](http://stackoverflow.com/questions/8948964/git-rebase-master-then-push-origin-branch-results-in-non-fast-forward-error) – ben39 Jan 20 '12 at 23:03
19

This is useful when you have your own origin which is not upstream. In other words, you might have your own origin repo that you do development and local changes in and then occasionally merge upstream changes. The difference between your example and the highlighted text is that your example assumes you're working with a clone of the upstream repo directly. The highlighted text assumes you're working on a clone of your own repo that was, presumably, originally a clone of upstream.

smparkes
  • 13,807
  • 4
  • 36
  • 61
9

Let's take an example: You want to contribute to django, so you fork its repository. In the while you work on your feature, there is much work done on the original repo by other people. So the code you forked is not the most up to date. setting a remote upstream and fetching it time to time makes sure your forked repo is in sync with the original repo.

Anmol Deep
  • 463
  • 1
  • 5
  • 16
4

I think it could be used for "retroactively forking"

If you have a Git repo, and have now decided that it should have forked another repo. Retroactively you would like it to become a fork, without disrupting the team that uses the repo by needing them to target a new repo.

But I could be wrong.

Onceler
  • 67
  • 6
  • 1
    What value does this answer bring after the question was answered 5 years ago? And you are still not even sure of the correct answer... – Maciej Jureczko Sep 13 '17 at 20:53
  • 1
    @MaciejJureczko I'm not alone in wondering how to "retroactively fork", if this question/answer is discussing how (which I think it is, but I'm not sure). Then this question could be used as an answer to other questions. https://stackoverflow.com/questions/30472771/marking-a-repo-as-a-fork-in-github-after-the-fact ... if this is how to retroactively fork, it may now be findable with those search terms. It took me a while to think that perhaps adding an upstream repository might be what i was looking for. If OP was the only audience, they wouldn't archive questions from "5 years ago". – Onceler Sep 14 '17 at 14:57