How to Create Remote Git Repository

How to Create Remote Git Repository

Git is a popular version control system that allows you to manage files locally as well as remotely. Typically, software development teams maintain a remote git repository on a third-party service such as GitHub or BitBucket and also keep a clone of it on their local machines for development. As they make changes to their local repository, they push these changes to the remote repository. Other developers are required to pull these changes to their local repository to keep it in sync with remote repository. The remote repository acts as the single source of truth and other local repositories can easily sync themselves with it. In this article, we will learn how to create remote git repository.

How to Create Remote Git Repository

Here are the steps to create remote git repository. We have assumed that you already have a local code repository ready and you need to create a remote git repository to push files into it. There are two aspects to this solution. You need to perform some steps on your local machine, and some on the remote server where you create new repository. We will look at both these things one by one.

On Remote Server

You need to create an empty repository on your remote server. If you are using cloud services such as GitHub or BitBucket, they have a setup wizard that allows you to create an empty repository with a few clicks, and returns a URL to the repository after it is created. Note this repository since we will need it later. On the other hand, if you want to create a remote repository on a self-managed server then log in to it and run the following command to create an empty folder there.

$ sudo mkdir my_repo
$ cd my_repo

Once we are inside this folder, run the following command to initialize a new repo.

$ sudo git init

The above command will create an empty git index with information about its location, and branches.

On Local Machine

Next, you need to go to your local machine and navigate to the local repository’s folder.

$ cd /path/to/local_repo

Then create a git repository out of the local folder.

$ sudo git init

Then add a remote origin to your local repository. Replace the link in the following command with the URL of your remote repository that you had noted earlier. This will link your local repository with remote one and allow you to push files from local repository to remote repository.

$ sudo git remote add origin ssh://myserver/my_repo

Then run the following commands to add all files in your local repository folder to the local git index and commit changes.

$ sudo git add .
$ sudo git commit -m "Initial commit"

Lastly, push files to remote repository.

$ sudo git push -u origin master

That’s it. Now you can make changes to your local repository, commit those changes, and push them to remote repository. In this article, we have learnt how to create a remote git repository and push files from local repository to it.

Also read:

How to Enable Keep Alive in NGINX
How to Use Git Shallow Clone
How to Redirect 403 to 404 in Apache
How to Install Pip in Ubuntu
How to Redirect Subfolder to Root in Apache

Leave a Reply

Your email address will not be published. Required fields are marked *