Git

How to Duplicate a Git Repository? (Without Forking)

Git developers create different remote repositories and work on them. Sometimes, they want to copy one repository’s content into another directory. Git allows you to clone the remote repository to the local computer for adding or removing files and fixing conflicts. Moreover, you can also clone the remote repository to another existing remote repository to contribute to a large project.

This article will illustrate the procedure to duplicate a Git repository.

How to Duplicate a Git Repository?

To duplicate a Git repository, follow the provided steps:

    • Navigate to the local Repository.
    • Check remote origin.
    • Create a duplicate copy of the desired GitHub repository.
    • Redirect to the bare-cloned repository.
    • Mirror-push to the targeted repository.
    • Go back to the local directory.
    • Delete temporary local bare-cloned repository.
    • Verify changes in the targeted repository.

Step 1: Redirect to Local Directory

First, execute the below-listed command and switch to the particular directory:

$ cd "C:\Git\test_2"

 
Step 2: Verify the Remote Origin

Then, ensure whether the remote origin is added to the local repository using the “git remote -v” command:

$ git remote -v

 

Step 3: Check Old Remote Repository Content

It can be observed that the old repository contains three branches:


Now, create a bare copy of the desired remote repository with the help of the “git clone” command along with the “–bare” option:

$ git clone --bare https://github.com/laibayounas/newRepo.git

 
Here, the “–bare” option is used to make a duplicate copy of the existing GitHub repository in the local repository:


Step 4: Navigate to Bare Cloned Repository

Then, redirect to the bare-cloned repository by typing out the below-stated command:

$ cd newRepo.git

 

Step 5: Push Duplicate Changes to Target Repository

Next, utilize the below-provided command to push the bare-cloned changes to the target GitHub repository:

$ git push --mirror https://github.com/laibayounas/demo.git

 
Here, the “–mirror” option is used to create a duplicate copy of the repository:


Step 6: Move Back to Local Repository

To switch back to the local repository, write out the “cd ..” command:

$ cd ..

 
Step 7: Remove Temporary Local Repository

Finally, delete the bare-cloned temporary repository from the local system:

$ rm -rf newRepo.git

 
In the above command, the “rm” option is used for removing a temporary repository, and the “-rf” command will delete everything in the repository:


Step 8: Verify Changes in Target Repository

Lastly, verify the new changes in the target repository:


It can be observed that the old repository has been duplicated to the target repository successfully.

Conclusion

To duplicate a Git repository, first, move to the local repository and check its remote origin. Then, make a bare(duplicate) copy of the particular remote repository and switch to it. Next, execute the “git push –mirror <target-repo-URL>” command to push the bare-cloned changes to the target GitHub repository. Switch back to the local directory and remove the temporary bare-cloned repository from the local system. Lastly, verify the added changes in the target GitHub repository. This article demonstrated the process to duplicate a Git repository.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.