-2

I accidentally dropped all prior Git history from a clone of a third-party maintained "SOME_TEMPLATE" repo when I began adding my own custom code (my "CUSTOM" repo).

The recommended way to use SOME_TEMPLATE is to clone it (with history), add your custom code, then periodically re-merge in the desired new changes from the regularly-updated SOME_TEMPLATE.

So to get back in sync with the SOME_TEMPLATE repo, I need to re-apply my code changes (100 commits) to a fresh copy of the SOME_TEMPLATE repo ... but first "rewind" the SOME_TEMPLATE back to the same commit it was on when I started adding my code.

SOME_TEMPLATE currently has 1000 commits. It was on commit #950 when I started adding my code (and when I accidentally dropped all the old history).

My CUSTOM code added 100 commits.

So I think I need to do something like this:

  • make a fresh clone of SOME_TEMPLATE (with the git history this time), call it "REDO".
  • somehow drop the last 50 commits in REDO so it's re-wound back to commit #950 (which is where I started adding my code)
  • somehow re-add my same 100 commits from my CUSTOM repo to the new, corrected REDO repo. (So when done the HEAD of REDO has the exact same code as the HEAD of CUSTOM, but REDO has all the old SOME_TEMPLATE history that I should not have dropped.)

If I do that, then I can start reviewing and merging the 50 new updates to SOME_TEMPLATE using their recommended merge/update process.

If absolutely necessary, I could probably "squash" my 100 commits down to one commit, to maybe make it easier to re-add all my changes to the REDO repo, but if there's a way to stack my 100 commits on top of commits 1-950 in REDO, that would be ideal.

jpw
  • 18,697
  • 25
  • 111
  • 187
  • 2
    How can you "accidentally drop all old history" in Git? What do you mean by that? – j6t Jul 11 '21 at 11:50
  • Was a month ago don't recall exactly "how" but may have made a shallow clone at some step in the (IMO obtuse instructions) without realizing the (undocumented) implications for future updates. Exact same thing happened independently to at least one other user of the template recently who is now facing the same issue. But curious which word was confusing - "accidentally" or "drop"? It means what it sounds like: none of the 1000+ commits in the original repo were there when I did the "first commit". 1st time using a repo where you add your own code and merge new updates later. 3rd degree over? – jpw Jul 11 '21 at 12:26
  • 1
    I am asking because with Git it is not possible to "drop all old history" unless you try really, really hard. At any rate, "shallow clone" is a valuable hint. In reality, you did not drop history, you just did not download it in the first place. The solution is probably just to [unshallow the repository](https://stackoverflow.com/questions/6802145/how-to-convert-a-git-shallow-clone-to-a-full-clone). – j6t Jul 11 '21 at 14:43
  • 3
    Then it may still be possible that you started an orphaned branch (one that is not connected with the original history), but that again requires some extra strong will to do if you started with a clone (shallow or not). So, unless you did something really stupid or something mildly advanced, unshallowing the repo should get you back to your normal workflow. – j6t Jul 11 '21 at 14:44
  • Advanced is out of the question; but stupid is not, especially it comes to my gitnorance (hmm, good word). I THINK the steps were: 1) clone AAAA to local (maybe shallow), 2) remove old remote "origin", 3) *maybe* a git init (how can I tell?), 4) point "origin" to an empty Github repo BBBB and push "first commit" which shows up in github as "1,013 changed files with 33,174 additions and 0 deletions" and that commit ID# in BBBB does NOT match any in source AAAA. (Then a hundred commits to add a bunch of code.) Might unshallow still work, and if so, I assume I'd need to re-add AAAA as a remote? – jpw Jul 11 '21 at 19:28
  • Actually, looking at my Github repo BBBB, it says "*generated* from aaaa/AAAA". Which makes me think that perhaps I started in Github, somehow duplicated (clone? shallow? fork? idk) AAAA as my own copy BBBB (whose first commit has a different commit ID, but is the same code as commit #950 in AAAA) then used it as "origin" during development. So no idea if it still has links back to AAAA that can be used to restore AAAA's commit history. – jpw Jul 11 '21 at 20:11
  • 1
    It sounds like you downloaded an archive (zip or tar file) of one particular commit, rather than cloning the repository. GItHub provide a clicky link to do that. – torek Jul 11 '21 at 20:13
  • torek hit the nail on the head. thanks. – jpw Jul 18 '21 at 06:20

1 Answers1

0

It should possible. Git allows you to add an arbitrary number of arbitrary remotes:

git clone server/some_template.git REDO
cd REDO
git remote add mycommits ../path/to/your/repo
git fetch mycommits
git checkout -b feature commit-number-950
git cherry-pick your-first-commit-hash..your-last-commit-hash

The branch feature will now be starting at commit #950 and contain all your commits

knittl
  • 246,190
  • 53
  • 318
  • 364
  • Cool. If I'm understanding that, it should (temporarily) avoid any merge conflicts in REDO in commits 951-1000 since "mycommits" were made at the same 950 starting point that is where "feature" starts. So at this point "feature" is same as my current repo's "master"? Is there a way to make "feature" the "master" then one-by one try merging commits 951...1000 and dealing with merge conflicts as they arise? – jpw Jul 11 '21 at 20:25
  • Sure. You can name your branch anything (well, almost anything) you want and cherry-pick/rebase/merge history in any way you want and as often as you want – knittl Jul 11 '21 at 20:28