Home > Mobile >  How to transfer one repo to another repo in git
How to transfer one repo to another repo in git

Time:07-20

I want to copy all my repo A to repo B with all history details. for which I am following below commands

git clone --mirror old-repo-url new-repo
cd new-repo
git remote remove origin
git remote add origin new-repo-url
git push --all

I get this below error when I try to push git push --all command

! [rejected]          main -> main (fetch first)
error: failed to push some refs to 'https://git.repoB'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

When I try to run git pull command, system says "fatal: this operation must be run in a work tree". Can anyone tell how to resolve this issue?

Reference - https://itnext.io/git-repository-transfer-keeping-all-history-670fe04cd5e4

CodePudding user response:

The repository to which you are attempting to git push (your "repo B") already has some commits. Your git push would wipe out the history in repo B. It therefore says "no" to your request.

Remember that history, in Git, is the set of commits that you can find in the repository. That's all it is! The branch and tag names are just ways to find particular commits. Every commit has some metadata and in the metadata of any one given commit, Git has stored the raw hash ID—the "true name", as it were—of a list of previous commits. Branch names hold the hash ID of the latest commit (by definition: whatever hash ID is in the branch name, that commit is the latest commit). So the history is the set of commits found by starting with the various names and working backwards.

In your case, you copied all the names and commits from repo A, so that your new clone has the same set of commits found by the same names: i.e., the same history. But then you told your Git software to git push --all to repo B, which sends the commits to repo B and then asks repo B to set all of its names. If they already have their own commits—and they do—then for them, setting their name main, in this case, will lose their commits. In other words, it loses their history.

To copy a repository this way, we generally want to git push to a totally empty repository. This means that before we git push to repo B, we must first create repo B as an empty repository. That's a repository with no branches and no commits, so make sure that if you're using, e.g., GitHub's "create new repository" web interface, you select the "create totally empty repository" option.

  • Related