The first question you might ask is why you would want to copy something across a repository when you could easily Fork it.
And that is a very valid question. There are a few reasons why you might want to do this. Although I wanted to copy a large amount of the repository, including branches and tags, I didn't want everything.
Forks are publicly visible on GitHub, which might not be ideal if you want to keep your work entirely separate or private.
I had an original personal project that I then used for a work thing. I wanted to keep some of the bug fixes I'd found but remove some of the specific work items I'd added and be able to go down my original path for the project.
I wanted them to fully separate from each other.
So how do I copy a repository then?
There are a couple of ways you can do this, but to copy everything I wanted, which includes history, branches and tags, follow my solution below.
Clone repository
You might already have a copy of the repo locally, but we want to create a new one somewhere. So I created a personal folder where I then cloned down the repository that I wanted copied.
Get all the branches and tags
Next, you want to make sure you have everything you need. You can check the branches by running
You can then check all the tags with
If you are missing branches or tags, you can run this command to pull down everything. Run the above commands to confirm.
Change remote origin
Once we are happy we have everything, now it's time to change the location of our remote repository.
You can check the location of where you are currently pointed with
Which should give you the fetch
and push
URLs. They should be pointing to the directory from which you originally cloned.
Create your new remote location
We now need to create a place to put the new copy.
Head over to GitHub and create a new empty repository wherever you want the copy to reside.
It should give you a new URL that looks something like https://github.com/your-username/new-repository-name.git
. This is what we are going to change the remote paths to.
To change the paths, run the following command
You can run git remote -v
again to check that the paths have changed.
Push everything up to Github
Now that we have cloned the repository, gathered everything we wanted to be copied over to the new world, and changed the location of our remote origin, now it is time to push everything up to Github.
There are two commands that we need to run. The first pushed up the code and all the branches. The second pushes up all the code.
Conclusion
This might not be the most straightforward way to copy a repository, but it's also not that difficult. As I said at the start, there are a few reasons why you might want to do that. I have come up with some guides on how to decide which is best for you.
Forking is like borrowing a book from a library with a copy-paste feature while copying and reuploading is like rewriting the book yourself and publishing it again.
Use Forking When:
- You plan to contribute to the original project.
- You want to keep your changes up to date with the original repository.
- The project is open-source and you’re following collaborative best practices.
Use Copying When:
- You want to fully detach your version from the original (e.g., internal/company use or significant customization).
- You want the repository to appear entirely as your own, with no visible link to the original.
- The original repository is private or you have explicit permission to copy.