4

I work for an open-source project and forked it already. Someone has forked it too and has been working with some interesting ideas which won't be pushed to origin soon. I want to help him thus I cloned his fork to my computer. However, I can't push directly my work to his fork. I can't fork him either since I have a fork from origin already. What should I do? Thanks

Tony
  • 1,551
  • 20
  • 21
  • https://stackoverflow.com/q/28119733/7976758, https://stackoverflow.com/search?q=%5Bgithub%5D+create+second+fork – phd Aug 21 '20 at 05:28
  • Add the forked repo as another remote to your local repositry, fetch the changes from that remote, rebase the work you want to send onto the fork's work, then push your changes to the other remote (or send a PR). – Jeff Mercado Aug 21 '20 at 05:37
  • @JeffMercado : can you add an answer with all necessary git commands? Thanks – Tony Aug 21 '20 at 06:49

2 Answers2

3

Suppose your repository looked like this:

enter image description here

#Add the forked repo as another remote to your local repository
git remote add someone https://fork.url                 #create remote called "someone"

#fetch the changes from that remote
git fetch someone

enter image description here

#rebase the work you want to send onto the fork's work
git branch for-someone my-branch                        #create branch "for-someone" at "my-branch"
git rebase master for-someone --onto someone/master     #take commits from the "for-someone" branch down to your "master" branch and rebase it onto someone's "master" branch

enter image description here

#then push your changes to the other remote
git push someone for-someone                            #push branch "for-someone" to the "someone" remote

Insert more appropriate remote and branch names.

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
  • BTW, if you just want to commit a small thing/push to a PR/contributors fork/branch, you can omit the `git remote add` step and just use the URL of the fork (in this example `https://fork.url`) as the origin. I.e. just do: `git push https://fork.url for-someone` works too. (At least in recent git versions.) – rugk Jul 30 '22 at 09:13
0

You can do the two things as @jeff-mercado mentioned. This will be a complex task and I would advise you to use a GUI based git client for this. I have faced the same problem as you faced and found Sublime Merge to be very useful.

I would suggest asking him to send you a PR. Then you will be able to merge his changes into your repository Finally you can push the merged repo on your github and ask him to clone it and push. (This is assuming the other guy won't do the work of merging your PR, which will be easier for you.)

Anmol Deep
  • 463
  • 1
  • 5
  • 16
  • Look like the scenario you mentioned is not my current one. There is the main repo, his fork and I (the 3rd person) want to contribute to his fork, not the main repo, not my repo. I want to make a PR to his repo. – Tony Aug 21 '20 at 09:03
  • This link might help https://stackoverflow.com/questions/5606048/git-merge-from-someone-elses-fork – Anmol Deep Aug 21 '20 at 14:24
  • No, that doesn't help since it is about only 2 people, and merging from 2nd person to the 1st one – Tony Aug 21 '20 at 14:51