简体   繁体   中英

Git: cloning repository into new repository

I am using Git (on bitbucket) and have a repository with my code. I now want to use this code and largely modify it for another project all together. I understand that I should abstract the common components to a third library and use that but for the sake of argument, if I want to clone the current repository into a new one, is this possible without manually just copying the code and pushing to a new repo?

Thanks,

Aly

First clone the repository you want to work with. This step could be skipped if you want it all to happen in the folder you are already in.

git clone file:///path/to/repo/

Cloning will bring over the remotes specified in that directory. So you'll need to remove the remotes you don't want.

git remote rm <remote>

And add the ones you do, after you have created your remote repository.

git remote add origin <url>

You will also want to --set-upstream-to , or -u to tell git this is the remote repository this branch will update to, presuming you are on the main (or default) branch.

git push -u origin main

Then you'll need to decide which branches to keep and add to the remote. If you want to push all of them, just do git push --mirror . This will push all your tags and your remotes. But since we edited your remotes in an earlier step, it shouldn't be a problem.

If you only want to keep a few, you can git push -u origin <branch> each one you want.

您可以简单地使用set-url将现有存储库推送到新存储库中:

  1. cd file:///path/to/repo/
  2. git remote set-url origin <url-of-new-repo>
  3. git push -u origin master

you can either clone the existing repo and remove the remotes, or you can copy the folder on the filesystem and change/remove the remotes in one copy.

but as your question is not too clear, here another thing that is possible:

add another remote to an existing remote (besides origin) and push to that remote.

If you need only a particular branch of an existing repo, you can simply pull the branch into the new repo.

This will be very fast since only the commits in the branch are pulled.

Eg

git init newRepo && cd newRepo
git pull <existing repo url> <branch (optional)>

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