In this article, we’re going to learn how to connect a local project to a Github repository and—on the flip side—how to “clone” a GitHub repository to your computer. We have already demonstrated how you can use your InMotion Hosting account as a Git server, but we are using free Github hosting in this example.
There are two cases we will take into account here.
- Connect a local project to a remote repository hosted by GitHub
- Clone a GitHub repository as a local project
The second case automatically creates a working directory with all the remote repository settings in place. This is the popular option if you want to work on somebody else’s code or if you are going to be working on your code from a different workstation.
How to Add Your GitHub Repository to an Existing Project
In this first case, we are going to take the following points as given:
- You have a local project under Git management
- You want to use a remote repo at GitHub
- All you need to do is add the GitHub repo as a remote location for your local project
As we have done in previous tutorials, we’re going to be using your operating system’s native command line utility to complete these steps. All you need to do is open your computer’s default Terminal app.
- Once you are on a command line, navigate to your project’s directory
- Optional: to make sure you’re in the right place, you can type git status to receive git-related output:
[email protected]# git status On branch master Your branch is up-to-date with 'origin/master'.
With your web browser, log into your GitHub account
- Open your desired repository
Copy the remote repository URL by clicking the Clone or download button followed by the copy icon
- Back in your terminal app, copy the remote repository URL after the git remote add command (substitute “name-of-repo” with whatever word you would like to associate with this remote repository, or call it “origin”):
[email protected]# git remote add name-of-repo <URL>
- Use git remote -v to check if the remote repository was added successfully
How to Clone a Repository from GitHub
This case is a little easier to accomplish and requires only the following circumstances:
- You are interested in a certain remote repository on GitHub
- You want to copy all of the working files for that project to your computer
- You want the local instance to have the “origin” repository already added
In order to accomplish the above, all you need to run is one simple command, git clone followed by the remote repository URL.
- Open your terminal app
- Navigate to the desired location on your computer (a spot where you would like the project directory to reside, usually your user’s home folder or “~”)
Copy the remote repository URL from GitHub, as we did in the above case
- Run the git clone command followed by the URL you copied:
[email protected]# git clone <URL>
Following this command, Git will automatically grab all of the contents of the remote repository and copy them into a new directory named after the repository itself.