Master Git & GitHub From Scratch

Clear, practical, and structured lessons designed to take you from Git fundamentals to real-world GitHub collaboration.

Connecting Remotes Git Repository

Connecting a remote links your local Git repository on your computer to a hosted repository on the internet, such as GitHub, GitLab, or Bitbucket. This link allows you to synchronize your code, back it up, and collaborate with others.

Learn how to set up, view, and manage your remote connections efficiently.


1. Connecting a Local Repo to a Brand New Remote

If you initialized a repository locally using git init, you can connect it to an empty repository that you created on GitHub.

# Link your local repo to the remote hosting service
git remote add origin <remote-repository-url>

# Verify that the link was created successfully
git remote -v
  • origin: The conventional nickname for a primary remote repository.
  • Remote URL: The address of the hosted repository.

Example remote URL formats:

# HTTPS
https://github.com/username/repo-name.git

# SSH
git@github.com:username/repo-name.git
Note: Replace the example URLs with your actual repository URL. SSH connections require an appropriate SSH key setup.

2. Pushing Your Branches to the Remote

Once the remote is configured, you can upload your local branch and its commits to the hosted repository.

Push a Branch for the First Time
git push -u origin main

The -u flag sets the upstream tracking relationship between your local main branch and the corresponding branch on origin. Future pushes and pulls can usually use git push and git pull.

Push a Feature Branch
git push origin feature-login

Uploads the feature-login branch to the remote repository.

Important: Ensure your local branch exists and that you have permission to push to the remote repository.

3. Inspecting and Managing Remotes

When working on an existing project, you may need to check which hosted repositories are configured.

List All Connected Remotes
git remote -v

Displays remote names and their URLs for fetching and pushing data.

Show Detailed Remote Configuration
git remote show origin

Displays information about the remote, including tracked branches and the status of remote-tracking relationships.


4. Changing or Updating a Remote URL

If a repository is renamed, moved to another account, or changes from HTTPS to SSH, you can update its URL.

Update an Existing Remote URL
git remote set-url origin <new-repository-url>

Changes the URL associated with the origin remote.

Remove a Remote
git remote remove origin

Removes the remote configuration named origin from your local repository. It does not delete the hosted repository or your local project files.


📘 Quick Reference

Command Purpose
git remote add origin <url> Connect a local repository to a remote
git remote -v List configured remotes and URLs
git push -u origin main Push a branch and set upstream tracking
git push origin feature-login Push a feature branch
git remote show origin Inspect remote configuration and branches
git remote set-url origin <url> Update a remote URL
git remote remove origin Remove a remote configuration
Key Takeaway: Use git remote add to connect your local repository, git push to publish branches, and git remote -v to verify your remote URLs.