As we learned in one of the previous tutorials that GitHub repository is a repository over the cloud.  This means, whatever the data is available on Local Repository can be uploaded to Remote Repository on GitHub. We created an account on GitHub, now it is time that we push our local data to a remote location at GitHub.

Throwing some light on the subject of this post, this post will walk you through:

  • What is Git Remote command?
  • How to connect Local Repository with Remote Repository?
  • How to get Remote Repository details?

How to Link an Existing Git Local Repository to Remote Repository?

There are always a few ways to link the local repository to the remote repository.

  • From remote: This happens with the help of the Git Fork command when the repository is already available on GitHub. User makes a git fork or git clone to the same repository at local. (Note: We cover this in the next chapter)
  • From local: This happens with the help of the Git Remote command when the repository is first created on local. And then the user connects it to the remote repository.

Below we are going to go through the second approach, where a user has already a local repository and an empty remote repository, but these repositories are not linked with each other.

Since we Created a GitHub Repository in the last tutorial, in this we will try to connect it with the local repository. So that data can be pushed from local to remote.

Git Remote Command in Git

A git remote command is used to make the remote connections such as connecting a Git local repository with GitHub remote repository.

Now, it might look like that git remote is a live exchange of data (everything you do locally) between a local and a remote repository, this is not the case. Git remote is just a connection between the local and GitHub repository. Since GitHub repositories contain twisted URLs that one cannot remember for every repository, we provide names to those links to remember. We use git remote for the same. Through git remote, we provide a name to the repository through which we can refer to the GitHub repository.

In other words, git remote can be considered as a reference to the GitHub repositories which do not provide any real-time access to what you do locally i.e. whatever you do locally will not be reflected on your GitHub repository without your permission.

Git remote can be used to connect to your own repository (as we will do in the next section) or to connect to someone else's repository. Now, let's see how to link an existing local Git repository to a remote GitHub repository.

Coming back to the same GitHub page that we left above, notice that we have a section named ...or push an existing repository from the command line.

Local Repository Remote repository - Push an existing repository

Connect Local Repository with GitHub Remote Repository

The first command in that section of the image will be used to link the repository to the GitHub repository.

  1. Open your Git Bash and navigate to the repository that needs to be linked. (Learn how to navigate to the repository)

First_Project_Repo

The above image shows that the Git Bash has been opened in the First Project repository.

  1. Check if the repository is clean and there is nothing outstanding by using git status command.

Clean_Reporitory_Check

3, Execute the git remote following command:

Git Remote command

Since there is no linked repository, no output was received from Git.

  1. Now, with the help of the URL given above, we will link the repository. To link a repository, execute the following command and press enter:

git remote add origin https://github.com/harishrajora805/myFirstRepo.git

git_remote_add_origin

Note: *The above command follows the generic syntax of git remote add <name> <repository url>

  • Add: To add a new URL to the repository.
  • Name: To give a name that you will use instead of the URL of the repository.
  • URL: The URL of the repository.

Once this is done, the local repository will be linked to the GitHub repository. A couple of things should be kept in mind though.

Note: The URL that I used in the above image is the URL of my repository. Please use your own URL for the linking. Also, the name origin is the recommended or by default used by the developer for the first or primary repository on GitHub. It is not restricted and you can use your own name.

How to check if the Local Repository is connected with Remote Repository?

To check whether we linked our repository or not, execute the git remote command again

Local Repository Remote repository - Git Remote command to check if the repository is connected with Origin

As seen, the origin repository (alias for the GitHub myFirstRepo repository) is available. Go ahead and use git remote -v command to view the same result along with the URL as shown.

Local Repository Remote repository - Git Remote command to show link remote repository

The meaning of the output that you receive will be discussed in the tutorial when we push some changes to the GitHub repository. Till then, keep practicing.

Create GitHub Repository
Create GitHub Repository
Previous Article
What is Git Fork and How to Fork a Repository in GitHub?
What is Git Fork and How to Fork a Repository in GitHub?
Next Article
Harish Rajora
I am a computer science engineer. I love to keep growing as the technological world grows. I feel there is no powerful tool than a computer to change the world in any way. Apart from my field of study, I like reading books a lot and developing new stuff.
Reviewers
Lakshay Sharma's Photo
Lakshay Sharma

Similar Articles

Feedback