简体   繁体   中英

Merge a git repository lacking commits history to a repository that preserved history

I downloaded the source code of a C++ project instead of cloning its git repository at git://www.calatk.org/calatk.git

Using this source code (with no git history), I created a git repo, worked on the code for a while, and pushed all my work/branches to my github at https://github.com/agirault/CalaTK

I would now like to make a pull request to merge my code to the original git repository on calatk.git.

I was wondering how I could do this since I don't have the track from the previous commits in my repository. This repository on calatk.org should not have been updated since then, which means their last commit would be the same as my first commit available on my github.

Assuming you don't mind replacing your published GitHub repository with one that contains the original project's history†, here is what I would try:

  1. Add the upstream repository as a second remote:

     git remote add upstream <clone-url> 
  2. Fetch from upstream:

     git fetch upstream 

    This should generate new branch pointers including upstream/master . upstream/master should not share history with your existing commits. That is, you'll have two root commits: one from the upstream project and one from your fork.

  3. Rebase your work onto the upstream commits (assuming we only have master to worry about):

     git checkout master git rebase upstream/master 

    At this point you should only have one root commit: the one from the upstream project. The hash for your old root commit, which should now no longer be a root commit, should match what is shown in the upstream project ( 3cee904 ).

    You can use something like git show 3cee904 to make sure that the commit is correct.

  4. Force push your new history to GitHub:

     git push --force origin master 
  5. Submit your changes to the upstream repository according to their contribution guidelines.

    Since it's not on GitHub or BitBucket it's not likely that they'll be able to accept a pull request (that's a proprietary feature). You should be able to request push access or use git format-patch , though. The upstream maintainers could also fetch from your public GitHub repository.

Important note: If you've got collaborators on your GitHub project this may not be the best option. Modifying shared commits in Git is generally discouraged. I'm only recommending it since it sounds like your GitHub repository is effectively a personal repository.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM