admin管理员组

文章数量:1434889

I have this repos on GitHub:

  • [user 1] original
  • [user 2] copy(original)
  • \ -> [ME] fork(copy(original))

copy has added some commits on top of original, and my fork has other commits on top of copy (chronologically sequential, there is no time overlapping between repos)

I want to preserve the structure of the forks in order to put, in the future, my commits to original, like making a fork of original, then add to it the copy commits, then add the fork commits, preserving the authors of them and hopefully also the timestamps. Something like this:

  • [user 1] original
  • \ -> [ME] fork(original) <-- but with the commits that user 2 has on his own copy(original), and my commits on top of them

Is there a way to do this?

I have this repos on GitHub:

  • [user 1] original
  • [user 2] copy(original)
  • \ -> [ME] fork(copy(original))

copy has added some commits on top of original, and my fork has other commits on top of copy (chronologically sequential, there is no time overlapping between repos)

I want to preserve the structure of the forks in order to put, in the future, my commits to original, like making a fork of original, then add to it the copy commits, then add the fork commits, preserving the authors of them and hopefully also the timestamps. Something like this:

  • [user 1] original
  • \ -> [ME] fork(original) <-- but with the commits that user 2 has on his own copy(original), and my commits on top of them

Is there a way to do this?

Share Improve this question asked Nov 17, 2024 at 4:10 G. IanniG. Ianni 1491 silver badge10 bronze badges 7
  • 1 To clarify: You wish to make a fork of the original that has the commits of User 2's fork? What's wrong with making a fork of User 2's repo, like it is currently? – stickynotememo Commented Nov 17, 2024 at 4:15
  • If you want to "put your commits to original" you can always make a PR from your fork to the original, no matter what repo its forked from. – stickynotememo Commented Nov 17, 2024 at 4:20
  • Hi, thanks for your comment. Your interpretation is correct except for the fact that user 2 does not have a fork, but a copy (i.e., literally copy-paste of user 1's files) of the original user 1's repo, with user 2's subsequent commits on top of them. The reason of my will to make this sort of tree is that in the future my commits will be pushed to the original repo, along with user 2's ones, but this cannot happen now because user 2 does not have a fork, but a copy, so pull requests cannot be done. – G. Ianni Commented Nov 17, 2024 at 4:22
  • 1 Odd behavior from User 2. Potentially, you could fork User 1's repo, and then paste in the files from User 2's repo, but new commits wouldn't be automatically downloaded. – stickynotememo Commented Nov 17, 2024 at 4:30
  • 1 Did User 2 copy only the files, or did they copy the history? Even though they did not fork User 1's repository, they may still have taken the history (cloned User 1's repository and pushed to User 2's repository) and made commits on top. You should clarify that, because then the case becomes simple. But if they didn't copy the history (but only the files at a particular state), you are in for a bit of work. – j6t Commented Nov 17, 2024 at 8:52
 |  Show 2 more comments

1 Answer 1

Reset to default 2

Yes, its possible. First, clone your current fork, add original repo as a remote and fetch all data

git clone [email protected]:YOUR-USERNAME/fork.git
cd fork
git remote add upstream [email protected]:user1/original.git
git fetch --all

Then, create a new branch and add user2's repo as another remote

git checkout -b restructured upstream/main
git remote add user2repo [email protected]:user2/copy.git
git fetch --all

Then, cherry-pick user2's commits and your own commits from your fork.

git log user2repo/main
git cherry-pick FIRST_COMMIT^..LAST_COMMIT
git cherry-pick YOUR_FIRST_COMMIT^..YOUR_LAST_COMMIT

Finally push the restructured branch to your fork

git push -u origin restructured

It will create a new branch with the exact structure you want: original base + user2's commits + your commits, all with proper attribution and timestamps

You can then use this restructured branch as the base for your future pull request to the original repository. The commit history will be clean and linear, making it easier to review and merge.

Thank you.

本文标签: GitGithub fixing repos historyStack Overflow