Categories: Technology

Moving git repository and all its branches, tags to a new remote repository

#!/bin/bash
# Sometimes you need to move your existing git repository
# to a new remote repository (/new remote origin).
# Here are a simple and quick steps that does exactly this.
#
# Let's assume we call "old repo" the repository you wish
# to move, and "new repo" the one you wish to move to.
#
### Step 1. Make sure you have a local copy of all "old repo"
### branches and tags.
# Fetch all of the remote branches and tags:
git fetch origin

# View all "old repo" local and remote branches:
git branch -a

# If some of the remotes/ branches doesn't have a local copy,
# checkout to create a local copy of the missing ones:
git checkout -b <branch> origin/<branch>

# Now we have to have all remote branches locally.


### Step 2. Add a "new repo" as a new remote origin:
git remote add new-origin git@github.com:user/repo.git


### Step 3. Push all local branches and tags to a "new repo".
# Push all local branches (note we're pushing to new-origin):
git push --all new-origin

# Push all tags:
git push --tags new-origin


### Step 4. Remove "old repo" origin and its dependencies.
# View existing remotes (you'll see 2 remotes for both fetch and push)
git remote -v

# Remove "old repo" remote:
git remote rm origin

# Rename "new repo" remote into just 'origin':
git remote rename new-origin origin


### Done! Now your local git repo is connected to "new repo" remote
### which has all the branches, tags and commits history.
Koushik CH

KOUSHIK CH is a Young Software Developer, who enjoys challenges, Traveling, eating out, and cookery. He is Accountable and Geek, but can also be very Mobile/Laptop Addicted and a bit Foodie Selfish.

Recent Posts

Installing Magento 2 on Ubuntu using a Bash Script

Here is the magento2 bash script’s that you can you to install Magento using LAMP and elasticsearch #!/bin/bash # Update… Read More

1 year ago

Expert guide on boosting employee health and safety when their ‘workplace’ is the road

The boom in ridesharing, food delivery and parcel delivery services has seen more cars on the road for work purposes.… Read More

3 years ago

Southeast Asia beckons – dairy exports into ASEAN-6 nations set to boom over the next decade

Demand for dairy in Southeast Asia is set to surge over the coming decade, creating future opportunities for key dairy-exporting… Read More

4 years ago

Sony PlayStation5 prices in India announced: All details

Sony has announced that it will be bringing the new PlayStation5 in India soon. While the company hasn’t provided the… Read More

4 years ago

Microsoft’s attempt to hobble Trickbot botnet wasn’t successful, Intel 471 says

But the U.S.-based threat intelligence company Intel 471 found that Trickbot continues to operate four days after Microsoft’s seizure of… Read More

4 years ago

Twitter changes hacking policy after blocking New York Post story on Hunter Biden

Twitter declined to comment about the most recent change on the record. Late Thursday night, Twitter executive Vijaya Gadde tweeted… Read More

4 years ago

This website uses cookies.