Attaching the old and new repo created by copying files (not cloning), keeping the commit history

To keep things simple, I have the following structure in git old_repo for "Subpath":

subpath/old_commit_1
subpath/old_commit_2
subpath/old_commit_latest

      

I decided to go to new_repo and just copied the latest version of "subpath" (from old_commit_latest) into it without any commit history.

So new_repo now has a "subpath" and a bunch of new commits that I made to it:

subpath/new_commit_subpath_added # added subpath here
subpath/new_commit_1
subpath/new_commit_2
subpath/new_commit_latest

      

Now I need to transfer the whole history from old_repo to new_repo to get the following tree in new_repo:

subpath/old_commit_1
subpath/old_commit_2
subpath/old_commit_latest
subpath/new_commit_1
subpath/new_commit_2
subpath/new_commit_latest

      

How to do it?

I only need to do this in order to master the branch, but I have a lot of files in there in the same situation. Both the subpath and filenames in old_repo match those in new_repo.

My guess is that I need to patch the subpaths in old_repo, rollback to the first commit for each subpath in new_repo, remove the commit first, apply the patches, and then reinstall all new commits above it. Not sure how to do it. Thank you for your help.

0


source to share


1 answer


Preparations.

You must have both new and old versions available as valid repositories. The old version should be better on your local machine, the new one might be local or remote (like github).

It is always useful to have backups of both repositories and projects.

My guess is that the last commit of the old version is the first commit of the new one. If it doesn't:



  • Reset the new version with its first commit.
  • Delete files in the old version of the project except .git

    but including .gitignore

    other git settings.
  • Copy files from older versions folder to older versions.
  • Save all changes as a new commit on the old / master.

Merging repositories

# go to the old repo folder
cd path/to/old
# add the new repo as a remote
git add remote newrepo path/to/new/.git

#check that it properly added
git remote show newrepo

#fetch data from new
git fetch newrepo

#create a branch "new", tracking
git checkout -b new newrepo/master

#merge changes to the old master
git checkout master
git merge newrepo

#an editor will open with a merge commit message. If you've done the preparations, there should be no merge conflicts.
#this should show a complete history now
git log --oneline

      

You now have a shared history and recent commits in the old project directory.

+1


source







All Articles