I've got a project hosted on GitHub which somebody has forked. On their fork, they've created a new branch "foo" and made some changes. How do I pull their "foo" into a new branch also named "foo" in my repo?
I understand they could submit a pull request to me, but I'd like to initiate this process myself.
Assume the following:
- Because they forked my project, both our repos share the same 'history'
- Although GitHub shows their project was forked from mine, my local repository doesn't have any references to this person's project. Do I need to add theirs as a remote?
- I don't have a branch called "foo" yet - dunno if I need to manually create this first or what.
- I definitely want this pulled into a separate branch and not my master.
I hope that makes some sense.
No, you don't need to add them as a remote. That would be clumbersome and a pain to do each time.
Grabbing their commits:
This creates a local branch named
ournameforbranch
which is exactly the same as whattheirbranch
was for them. For the question example, the last argument would befoo:foo
.Note
:ournameforbranch
part can be further left off if thinking up a name that doesn't conflict with one of your own branches is bothersome. In that case, a reference calledFETCH_HEAD
is available. You cangit log FETCH_HEAD
to see their commits then do things likecherry-picked
to cherry pick their commits.Pushing it back to them:
Oftentimes, you want to fix something of theirs and push it right back. That's possible too:
If working in detached state worries you, by all means create a branch using
:ournameforbranch
and replaceFETCH_HEAD
andHEAD
above withournameforbranch
.If the forked repo is protected so you can't push directly into it, and your goal is to make changes to their foo, then you need to get their branch foo into your repo like so:
Now you have a local copy of foo with no upstream associated to it. You can commit changes to it (or not) and then push your foo to your own remote repo.
Now foo is in your repo on GitHub and your local foo is tracking it. If they continue to make changes to foo you can fetch theirs and merge into your foo.
This will setup a local branch
foo
, tracking the remote branchcoworker/foo
. So when your co-worker has made some changes, you can easily pull them:Response to comments:
You don't need to create a new branch, even though I recommend it. You might as well commit directly to
foo
and have your co-worker pull your branch. But that branch already exists and your branchfoo
need to be setup as an upstream branch to it:assuming
colin
is your repository (a remote to your co-workers repository) defined in similar way:If antak's answer:
gives you:
Then (following Przemek D's advice) use
The following is a nice expedient solution that works with GitHub for checking out the PR branch from another user's fork. You need to know the pull request ID (which GitHub displays along with the PR title).
Example:
Fixing your insecure code #8
alice wants to merge 1 commit into
your_repo:master
fromher_repo:branch
Substitute your remote if different from
origin
.Substitute
8
with the correct pull request ID.