How to Set Up a Remote Repository

How to Connect a Local Repository to a Remote on GitHub

If your project already exists locally but has no Git history yet, follow these steps.

1. Initialize the local repository

Navigate into your project's root directory and run:

git init

This turns the directory into a Git repository by creating a hidden .git folder.

2. Create a remote repository

Once you have initialized the repository, create a remote repository somewhere like GitHub.com.

3. Add the remote URL to your local repository

Run the following command, replacing <URL> with your repository's URL:

git remote add origin <URL>

This stores the remote URL under the human-friendly name origin, which is the default.

4. Stage and commit your existing files

Shape your history into at least one commit by using git add to stage the existing files, and git commit to make the snapshot.

5. Push to the remote and set up tracking

Once you have at least one commit, push to the remote and set up the tracking relationship with:

git push -u origin main

This configures the relationship between the remote and your local repository so that you can use git pull and git push with no additional options in the future.


Useful commands to know

  • git remote -v — List the current remotes associated with the local repository.
  • git remote add [name] [URL] — Add a remote.
  • git remote remove [name] — Remove a remote.

What if the remote already exists?

If the repository already exists on a remote, use git clone [url] instead of git init. You should use one or the other, not both.

Source: https://github.com/git-guides/git-remote, https://github.com/git-guides/git-init

Related articles