4

How do I move repository (old_rep) to another repository (new_rep) as a directory without loosing the history and files for repositories?

Here is what I tried from other stack questions. I have files in new_repo like directories, .txt, .sql files with history. When I run the codes below, looks like the --mirror is replacing everything from old_rep into new_rep.

mkdir foo cd foo git clone --bare ssh://[email protected]/test/old_rep.git cd old_rep.git git clone --bare ssh://[email protected]/test/new_rep.git cd .. rm -rf old_rep.git 
1
  • If you go into old_rep.git, do some operation, cd .. and rm -Rf old_rep.git, did you not just delete everything? Commented Nov 16, 2016 at 19:41

2 Answers 2

8

Assuming you want to move all the content from old_repo into the directory dir1 of new_repo:

  1. In old_repo create dir1, move all the files and directories from the working directory into dir1 and commit.

    That's all with old_repo. The other steps below happen in new_repo.

  2. In new_repo use git remote add old_repo <path-to-old-repo> to add the old repo as a remote for the new repo. Replace <path-to-old-repo> with the actual path (full or relative) of the old repository. I assume you have both repositories on the local computer.

  3. Make sure there are not uncommitted changes. If there are then commit them on a branch or stash them.

  4. Run git fetch old_repo to get the commits from the old repo into the new repo. Assuming you already have checked out the branch where you want to import the code from old_repo, run git merge old_repo/master. Replace master with the actual branch from old_repo you want to import.

  5. Check that everything looks good in new_repo and you have all the files from old_repo and their history.

  6. Cleanup. Only after you double-checked step 5, run git remote remove old_repo to unlink the repos and then, if you are sure you don't need anything from old_repo, you can remove the directory where old_repo is located. I would do the removal after some time (1-2 weeks), just to make sure I don't lose anything.

  7. That's all.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that for some Git versions, the merge will need the --allow-unrelated-histories flag
1

This is something I found to be a lot simpler. The answer below assumes like me you don't care too much for this old repo as much as I used to because it's old or the new repo is just storage for old repos. Which is why I don't mind doing it this way.

  1. Clone the old repo(s) for move on to your local machine
  2. Make new destination repo from your local machine or git clone from an existing repo
  3. Copy and Paste all folders and files from old repo(s) into destination repo folder on your machine
  4. git add . then git commit and then git push
  5. Delete Old Repo

1 Comment

The OP said they wanted to preserve all commit history.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.