简体   繁体   中英

How to add back the original project history when the whole project was committed with a “first commit” to a brand new repo

Because of inexperience at the time, I copied the code of a github repo1 into a brand new git repo2, committed it with a "first commit" message and push it to my remote in bitbucket. Some changes were made to this repo2 and some branches added.

Now, a year later, there were some updates to the original github repo1 that I'd like to pass to the bitbucket repo2, but both repos have unrelated histories.

How could I add the original repo1 history to my repo2?

I think that a possibility could be rebasing my entire repo2 (with all the new branches) onto the head of the repo1 (which now includes some updates), but keep the results in my repo2 (possibly in a new branch). However, I'm not sure how to do that. Any help would be appreciated.

You can "stitch" the histories back together using git replace .

Here is a way to set this up on your local repository:

  • add the original repo as a remote of your current repository, and fetch its history:
git remote add history https://github.com/some/repo
git fetch history
  • in your local history: note the sha1 of your "first commit" (we'll call it sha1-local )

  • in history 's commits: spot the commit which has the same content as your own "first commit", and mark its sha1 (we'll call it sha1-history )

  • run:

git replace sha1-local sha1-history

From now on: several git commands will see your history as if it were combined with the history 's history.

For example, if feature is a branch in that repository, you can run:

git checkout -b feature history/feature
git rebase master

If you want to rewrite all the commits in your local repository and make that replacement permanent, call git filter-branch or git filter-repo :

# to rewrite only your local master branch :
git filter-branch --index-filter true -- master

# to rewrite all your local branches :
git filter-branch --index-filter true -- --branches

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