'Pull request from a different repository
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?
Solution 1:[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.
- Click "Fork" on the upstream project's GitHub page.
- After the forking process is complete, clone that repository to your local computer (I'll call this "the forked repo").
- Copy all changed files from the copied repo into the forked repo.
- Verify that it's working as you expect, then look at the changes using
git statusandgit diffand commit them. - Push those changes back to GitHub.
- 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.
Solution 2:[2]
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:
- fork and clone the original repo
- in that repo, checkout to the commit that you initially downloaded.
- in your non-forked, downloaded repo, checkout to the first commit you made after having downloaded it.
- copy those files into the forked repo
- Recreate the commit in the forked repo
- repeat steps 3-5, but for each successive commit.
- 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)
Solution 3:[3]
You need first to connect the old repository OLD (containing proper git history) to the new one NEW (disconnected but containing modifications) by:
- from NEW, revert to the first commit:
git checkout $(git rev-list --max-parents=0 HEAD | tail -n 1) - synchronize OLD with this state:
rsync -avrI . /path/to/NEW - 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') - from OLD, apply the create patch: git am < ../path/to/patch.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Xiong Chiamiov |
| Solution 2 | Marcus Gosselin |
| Solution 3 | guhur |
