How to Create a Remote Repository From a Local Repository in Git

John Wachira Feb 15, 2024
How to Create a Remote Repository From a Local Repository in Git

This article discusses the steps necessary to create a remote repository based on a local repository. This is ideal when you have a local repository that you need to be available on a remote or SSH-enabled server.

Create a Remote Repository From a Local Repository in Git

We will employ a practical example to see how we can create a remote repo from a local repo.

Example:

We will start by creating a simple repo on GitHub. We will call this repo Secondaryrepo.

Follow these steps:

  1. Sign in to your GitHub account, tap Repositories, and click New to create a remote repo.
  2. Once created, go to Code and copy the repository’s URL.

On our computer, we have a Delftscopetech repository with a remote-tracking repository. To create a remote repo from this repository, we need to initiate a bare repository in our delftscopetech repo, as shown below.

$ git init --bare

We can now add our remote (Secondaryrepo) repository to our empty repository. Recall that we already have remote tracking in the Delftscopetech repo.

To differentiate between the two remotes, we will use orign for this remote repo.

$ git remote add orign https://github.com/Wachira11ke/Secondaryrepo.git

Let’s check the remotes present.

$ git remote -v

git add remote orign

Now we can push to the remote Secondaryrepo repository, as shown below.

$ git push -u orign

Note that you will be pushing individual branches to the remote.

Output:

git push -u orign

Let’s look at our remote repository on GitHub.

remote repository on GitHub

As we can see from the two images above, Git created a Dev2.1 branch in the remote repo and pushed all the commits to our local repository. Now, our local repositories can push and pull from this remote.

In conclusion, we can create a remote repository based on a local repository. If your local repo has a tracking remote repository, it is best to initiate a bare repository before adding another remote.

Giving your remote a new name rather than origin is advisable. It makes it easy to differentiate multiple remotes in one repo.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Remote