Moving a Git Repository

Search for a command to run...

No comments yet. Be the first to comment.
In this series, you can find useful Git commands, configuration and productivity tips.
You might need to use multiple Git accounts on a single computer. For example, you might have personal, work, and university accounts. Most Git tutorials show you how to configure your global username and email which will be used when you make commit...
This blog post is fourteenth in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described in Day 14. This challenge was about optimizing an algorithm that generates exponentially larger and larger strings. I found ...

This blog post is thirteenth in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described in Day 13. All solutions are in this GitHub repository. Solving Part One Given a list of x, y coordinates representing dots ...

This blog post is twelfth in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described on Day 12. All solutions are in this GitHub repository. Solving Part One Given a set of connections between underground caves, ...

This blog post is eleventh in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described in Day 11. This day had many similarities to Day 9, and I solved the challenge using similar methods. All solutions are in thi...

This blog post is the tenth in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described in Day 10. All solutions are in this GitHub repository. Solving Part One Given a set of lines with opening and closing bracke...

Every once in a while you might need to move a Git repository while preserving the history.
Here is how you can move or duplicate a Git repository from an initial repository
https://github.com/username/old.git to a new repository https://github.com/username/new.git
git clone https://github.com/username/old.git
git remote rm origin
git remote add origin https://github.com/username/new.git
git push -u origin main
My friend Leila built a website for Fearless into Tech community. She shared the code of the website on her GitHub profile.
Since more members in the community wished to contribute to the website, we created an organization on GitHub. That way anyone in the community could create issues, write code and improve the website. We then needed to move the repository from Leila's personal GitHub profile to the new organization while preserving the commit history to reflect her contributions.
Here is how you can move a Git repository:
Clone the repository you want to move by running
git clone https://github.com/username/old.git
Check remote repositories it is connected to by running
git remote -v
Output example showing a remote called origin which points to the original git repository.
origin https://github.com/username/old.git (fetch)
origin https://github.com/username/old.git (push)
Remove the remote called origin by running
git remote rm origin
Check that the list of remotes was empty by running git remote -v
Add a new remote and call it origin
git remote add origin https://github.com/username/new.git
Push to the main branch of the new repository
git push -u origin main
Let me know if there is an easier way to do this :)