111

I have forked out a Parent: project to Child: this. Now, I want to update my child with parents current updates. Can I do that, if yes how?

When I update my github repo, then I can do a "git pull" to update my local repo.

zengr
  • 38,346
  • 37
  • 130
  • 192
  • You've got it backwards - you do your work in your local repo, and then do git push to update the remote. – Cascabel Nov 12 '10 at 23:38
  • 11
    Yes, I understand that, but there are few changes in the original project which I need to incorporate in my working copy. That's why I need to update my local repo with original project's updates while keeping my code changes in local intact. – zengr Nov 12 '10 at 23:41
  • 2
    That's exactly what we've all explained how to do. I'm simply pointing out that you don't "update the github repo, then... pull to update your local repo" - you update the local repo, then push to the github repo. – Cascabel Nov 12 '10 at 23:45

4 Answers4

116

In your local clone of Child, pull from Parent, adding it as a remote if you like:

cd child
git remote add parent <parent-url>
git pull parent

The url of the parent could be the public github repo, or your local clone of it - the local clone will of course be faster. If you want to pull a branch other than the current HEAD of the parent repo, just add an argument (e.g. git pull parent topic-branch). If this is a one-time thing, you can just skip adding the remote: git pull <parent-url> [branch].

Pulling is a combination of fetching and merging, so once you've done that, you've got a new merge commit you'll presumably want to push back to your public repo at some point.

The key point here, in case it's not clear, is that pulling from the parent (upstream) repository is not different from pulling from your public clone of child, your current repository. Either way, you're fetching from a repository with some common history, and merging that into your current branch. And of course, since you're merging, a work tree is required - so this is something that must be done in your local repo. The repo hosted on github is essentially a way of publishing what you've done locally. All you can really do with it is push/pull, and browse what's there.

Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • 4
    I got an error `You asked to pull from the remote 'origin', but did not specify a branch. Because this is not the default configured remote for your current branch, you must specify a branch on the command line` when I did `git pull parent`. You can quickly go over it with `git pull parent master`. This is just a solution if you are working directly in your master branch or just have one branch or don't have idea of what a branch is and just want to merge quickly. Kind of the new-bee solution or if you just a quick solution to introduce a small change. – toto_tico Jul 03 '13 at 07:08
16
  1. Clone your repo to your local machine, if you haven't already: git clone git@github.com:utkarsh2012/voldemort.git
  2. Add the upstream as a new remote: git remote add upstream git://github.com/voldemort/voldemort.git
  3. With your branch checked out, pull the upstream into your branch, which will create a merge between the two sets of changes: git pull upstream or git pull upstream branch-to-merge. If you're working on an unpushed branch you can also use git fetch and git rebase to rebase your work without needing a merge.
hobbs
  • 223,387
  • 19
  • 210
  • 288
14

You want:

git pull git://github.com/voldemort/voldemort.git

Luke Francl
  • 31,028
  • 18
  • 69
  • 91
  • so, by that command, my local repo will be updated but what about my child project on github? I will push that to my github child? – zengr Nov 12 '10 at 23:16
  • @zengr: `git pull` is a combination of `git fetch` and `git merge`. In effect, it merges a branch from the remote repository into your current branch. Just as with any other work you do in your local repository, to get it to the remote, you push it. – Cascabel Nov 12 '10 at 23:17
  • As others have recommended, `git remote add parent`, how is that different from your appraoch of just `git pull`? – zengr Nov 12 '10 at 23:23
  • 2
    @zneger: A remote is a named remote repository. It saves you from having to type the URL every single time you want to reference; you can instead just type the name of the remote. It also automates some common tasks like setting up remote branches. – Cascabel Nov 12 '10 at 23:31
5

This can also be done simply on GitHub's web interface: issue a Pull Request but swap the base repo and head repo. If the pull can be performed automatically, it will be.

thSoft
  • 21,755
  • 5
  • 88
  • 103