Home > Back-end >  Is there a way to import the code from a repository into the code of a different repository?
Is there a way to import the code from a repository into the code of a different repository?

Time:12-03

I have the following problem: I've have created a repository and made changes to it, and commited them in git. Now, I've realized that the name of the repository is wrong, and I have created another with the right name, and I wonder if there is a way to import all the code (together with the changes I've made) in the first repository into the second repository using git and not copy pasting it manually. In the first repository there is a branch with all of my changes.

Mind that I cannot directly change the name of the first repository, as it doesn't belong to me and I am unable to do so.

Any ideas?

My original idea is to copy-paste all of the classes and code, manually, from one repository to the other, but that is absolutely time-consuming as the first repository has to many classes and a lot of code, and some copy-pasting errors might take place. I just want a safe and fast way to do this.

CodePudding user response:

  • Option A: In your new repository you could add the old one as a remote and then git fetch from it.
  • Option B: Add your new repository as a remote in your old one and then git push the branch.
  • Option C: Create another, temporary repository, add it as a remote in both of your repositories and then first git push from the old one to the temporary and after that git fetch in your new one from the temporary.

Option C is especially usefull if both repositories are behind a firewall and there is no way of them talking directly to each other.

CodePudding user response:

The phrase the code ... in the repository is imprecise:

  • A Git repository consists of commits.
  • Each commit holds a full snapshot of every file, plus metadata.

Thus, "the code" could refer to "some or all files in one specific commit", "all versions of some or all files in some set of some specific commits", or even "some or all specific commits".

If you want to preserve the actual commits themselves, that's one thing (and relatively easy to do but comes with some constraints). Methods for doing this are the options in SebDieBln's answer.

If you only want some or all files from some or all commits, that tends to be a little harder, unless you just want some or all files from one specific commit.

To get "some but not all files from some set of commits", use git checkout or git switch to extract each of those commits from the repository that has them, copy the files of interest elsewhere, and use Git to create new commits in the other repository as appropriate.

To get "all files from some or all commits", consider automating the above.

To get "some files from one specific commit", just check out the commit in question, save the files somehow, and move to the repository where you want those files and extract the saved files (this entire process could just be a matter of cp *.py ../other-repo for instance). Make one new commit in the target repository, and you're done.

  • Related