Pull request from a different repository
Asked Answered
N

3

12

I didn't know how forks and clones work. So in order to copy someone else's repo and work on it, I downloaded the repo's files (using no source control), created my own new repository, and then committed those files to it.

I want to pull request my repo's master to the original repo, but I can't because it's not a fork. Furthermore - git doesn't even know that they originate from the same source, and hence if I checkout the original repo, open a new fork, copy-paste all the files from my private repo to the new fork, and pull request it back in, it will show it as a single giant commit, and I'll lose all of the commit and comment history on the old repo, which will be terrible.

Is there a way for me to pull request my changes back into the original repo without losing all the history of my copied repo?

Niagara answered 12/8, 2016 at 13:15 Comment(0)
U
8
  1. Make sure you have a copy of your changes on your local computer (I'll call this "the copied repo") and delete the project on GitHub, if you've created one.
  2. Click "Fork" on the upstream project's GitHub page.
  3. After the forking process is complete, clone that repository to your local computer (I'll call this "the forked repo").
  4. Copy all changed files from the copied repo into the forked repo.
  5. Verify that it's working as you expect, then look at the changes using git status and git diff and commit them.
  6. Push those changes back to GitHub.
  7. In a moment, a banner should appear on the cloned repo's GitHub page that provides a button to open a pull request back to the upstream repo. Click that and open the pull.
Undirected answered 12/8, 2016 at 17:49 Comment(0)
L
5

unfortunately there is no way to merge two branches that dont have a common ancestor commit. even if the files are the same.

A very hacky workaround however goes as follows:

  1. fork and clone the original repo
  2. in that repo, checkout to the commit that you initially downloaded.
  3. in your non-forked, downloaded repo, checkout to the first commit you made after having downloaded it.
  4. copy those files into the forked repo
  5. Recreate the commit in the forked repo
  6. repeat steps 3-5, but for each successive commit.
  7. Once completed, you can open up a pull request with you newly created git history

Its a laborious process, and you could probably create a tool to do it, but if its not a lot of commits, or you dont mind combining a few commits into one. (to do this, in step 3, just skip a couple of commits, the code from previous commits will still be there)

Lagos answered 28/3, 2018 at 14:35 Comment(0)
N
0

You need first to connect the old repository OLD (containing proper git history) to the new one NEW (disconnected but containing modifications) by:

  1. from NEW, revert to the first commit: git checkout $(git rev-list --max-parents=0 HEAD | tail -n 1)
  2. synchronize OLD with this state: rsync -avrI . /path/to/NEW
  3. create a patch with commits from NEW latest commit to second commit: git checkout main && git format-patch -1 $(git rev-list --max-parents=0 HEAD | sed 'x;$!d')
  4. from OLD, apply the create patch: git am < ../path/to/patch.
Nesto answered 13/3, 2022 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.