168

I have two repositories, and I need to copy whole of one onto the other empty one which has different access levels from the first one. The copy and the mother repository should not be linked together.

I am new to git and it would be awesome if someone could help me with this.

3
  • 6
    not sure, but i guess you could just clone it and then use git config remote.origin.url git://new.url/proj.git to set the remote.origin to your new rep. Commented Jul 7, 2011 at 15:38
  • ya the link I just posted does something like that. Commented Jul 7, 2011 at 15:42
  • @rudinus I did just that. Thanks. Commented Jul 7, 2011 at 16:08

14 Answers 14

279

See https://help.github.com/articles/duplicating-a-repository

Short version:

In order to make an exact duplicate, you need to perform both a bare-clone and a mirror-push:

mkdir foo; cd foo 
# move to a scratch dir

git clone --bare https://github.com/exampleuser/old-repository.git
# Make a bare clone of the repository

cd old-repository.git
git push --mirror https://github.com/exampleuser/new-repository.git
# Mirror-push to the new repository

cd ..
rm -rf old-repository.git  
# Remove our temporary local repository

NOTE: the above will work fine with any remote git repo, the instructions are not specific to github

The above creates a new remote copy of the repo. Then clone it down to your working machine.

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

8 Comments

What happens if the old and new Repositories had the same name (not the same git URL). As in I just clones Jeremy.git into another Jeremy.git
This won't be an exact duplicate. Issues will not be transferred.
Does this preserve history?
@Thomas I just did this and yes, it preserved all of my history in the copy. Note that after you push and cd into the new repo, there's still nothing there because you've pushed to the remote, so you need to do a pull
For this answer to work with GitHub, one must first create the new-repository on GitHub.
|
28

You can also use git-copy.

Note: You need to install Ruby first. Run ruby -v to check if you have it installed. If you don't:

Linux: run sudo apt install ruby, Windows: use RubyInstaller, macOS: use Homebrew

Now install git-copy

gem install git-copy

Then you can use it

git copy https://github.com/exampleuser/old-repository.git https://github.com/exampleuser/new-repository.git

3 Comments

How do you install gem in the first place? This seems like the most hassle-free solution. Upvoted!
It is usually part of a Ruby install. On Ubuntu do sudo apt install ruby.
"how do you install gem in the first place?" disqualifies this from being the most hassle-free solution.
24

If you are copying to GitHub, you may use the GitHub Importer to do that for you. The original repo can even be from other version control systems.

4 Comments

This works perfectly and even keeps history - should be higher upvoted
I agree. This approach is much more intuitive, it can be done entirely within the Github GUI, and doesn't involve any of that confusing command line nonsense. Steps: 1) In the GUI make the new repo but don't click the check box labeled "Initialize this repository with a README" After you do this Github will present you with a new page and an option labeled "…or import code from another repository." That's it!
Doesn't work if you have read only permissions on the repo that you want to copy.
The easiest way to do the job, great! And also, if someone has read only permissions (or has 2 factor authentication on GitHub), they will need to generate a personal access token and then use that as the password (docs.github.com/en/authentication/…).
16

Update Nov 2021

You can easily copy files and folder of from another VC in github now.

Steps:

  1. Create a new repository
  2. Click on the Import code button at the bottom

Import code

  1. Paste your repository's clone URL and click Begin import

Begin import

You will be notified by email when the import is successfully completed.

3 Comments

Trying to import a private repository, and it keeps asking for my credentials. Didn't work.
Same here, looks like it doesn't work with private repos
For private repo's, when prompted, use a personal access token with repo read-only access instead of your password. Documented here: docs.github.com/en/get-started/…
8

If you just want to create a new repository using all or most of the files from an existing one (i.e., as a kind of template), I find the easiest approach is to make a new repo with the desired name etc, clone it to your desktop, then just add the files and folders you want in it.

You don't get all the history etc, but you probably don't want that in this case.

1 Comment

That is how I used to do it, but thought there has to be a better way. There isn't. Reassuring to verify.
2

You can always do it directly on Github which I find it easier. After creating a new repository (child repository) which is empty and you want it to have the same code as the (Parent repository), click on the Code on the top bar. At the buttom, pick the last opion which says (...or import code from another repository). You will be prompted to enter a clone link of the repository you wish to import your code from. Paste the clone link of your (Parent repository) and press import.

Comments

1

easier and beginner friendly Option: If you want to avoid command lines and terminal to duplicate a repository, this is what you do:

  1. Go to github page that shows all your repositories (https://github.com/your-account-name?tab=repositories)
  2. Click the button "New" to create a new repository.
  3. Click "repository template" button "No template". This will give you a list of your current repositories that are configured as a template.
  4. Choose the repository that you want to duplicate.*
  5. Finish creating the repository.
  • If the repository that you would like to duplicate is not available in the list from step 3, you need to configure the repository as a template: Go to "the repository page" > Settings > "template repository" checkbox.

Comments

1

The quickest option seems to be to enable the template repository option in your repo settings.

  1. Enable Template Repository enter image description here
  2. Go back to the repository dashboard and click Use this template enter image description here
  3. Disable Template Repository again.

Comments

1
  • You need to copy the link of any repository you need to clone and work on.

    (here!)

  • Click on Import repository from the dropdown by clicking on the + on the top right corner of the GitHub Dashboard.

    (here!)

  • It will redirect to the import page where you need to paste the repo link in the first input box and type in the name of the new repository. Click on Begin.

    (here!)

  • It will then again redirect you to another page where it will clone/import the repo to your repos. It will be something like this:

    this.

  • It will mail you when the import is completed or you can click on the link underneath to open the new repo.

  • You can continue working on the repo and can click on '.' on the keyboard to activate GitHub Developers and then you can continue editing it.

    (here!)

I hope this answers your query.

Comments

0

An easy method using the github GUI. Click on the repository of choice, and within settings then tick template repository. When you navigate back to the main repository. You can then create a direct copy

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

I have noticed some of the other answers here use a bare clone and then mirror push to the new repository, however this does not work for me and when I open the new repository after that, the files look mixed up and not as I expect.

Here is a solution that definitely works for copying the files, although it does not preserve the original repository's revision history.

  1. git clone the repository onto your local machine.
  2. cd into the project directory and then run rm -rf .git to remove all the old git metadata including commit history, etc.
  3. Initialize a fresh git repository by running git init.
  4. Run git add . ; git commit -am "Initial commit" - Of course you can call this commit what you want.
  5. Set the origin to your new repository git remote add origin https://github.com/user/repo-name (replace the url with the url to your new repository)
  6. Push it to your new repository with git push origin master.

Full steps:

mkdir my-repo
cd my-repo
git clone <repo> .
rm -rf .git
git add . ; git commit -am "Initial commit"
git remote add origin https://github.com/user/repo-name
git push origin master

1 Comment

This won't preserve history.
0

Safe version on command line, without need to rewriting origin, for public or private repository.

Create a new repository on github eg: "my-repo-copy"

# Run the command 
git clone https://github.com/myprofil/my-repo-copy.git

# The url can be found on the repository, click the button code and https.
# You now have a empty folder my-repo-copy.

# Inside the folder my-repo-copy, run:
git clone https://github.com/myprofil/my-repo-original.git
# You now have a folder my-repo-original, go inside and copy all files/folders except .git

# Go in the folder my-repo-copy and paste, delete the folder my-repo-original

# Run
git add .
git commit -m "clone my-repo-original"
git push origin main

# If you'r branch name is main and you want to be master, after the commit, run
git checkout -b master
git push origin master

1 Comment

This won't preserve history though.
0

i don't about your case but mine was that i wanted to fork this repository and for some reasons they wouldn't allow me because saying no location to fork repository and i have another repo that looks exactly like the one i wanted to fork, so here is how i solve my problem.

  • First Clone the repo you want to fork or duplicate to your vscode or local machine

  • create a new repository from github dashboard and give it the name you want

  • now make sure you are inside the clone repo in your local machaine and enter this commands:

  • git remote add origin https://github.com/YOUR-REPO-URL

  • git branch -M master

  • git push -u origin master

if it displays this " fatal: remote origin already exists." after you run the first command above then run this: git remote rm origin then repeat the commands in 3 above.

All your files in the clone repository will be transfered to the newly created repository with different name

Comments

-1

Open Terminal.

Create a bare clone of the repository.

git clone --bare https://github.com/exampleuser/old-repository.git

Mirror-push to the new repository.

cd old-repository.git

git push --mirror https://github.com/exampleuser/new-repository.git

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.