Git Remote – Add online repository
Git Remote is used to manage the list of remote repositories connected to your local Git repository. When you add an online repository with git remote add, Git stores a short name such as origin and maps it to a repository URL used for fetch and push operations.
This command is commonly used after creating a new repository on GitHub, GitLab, Bitbucket, or another Git hosting service, and then connecting that online repository to an existing local project.
Git remote add command syntax
The syntax of git command to add an online repository, without options which is generally used is
git remote add <name> <url>
Here, <name> is the local nickname for the remote repository. The default and most commonly used name is origin. The <url> is the HTTPS or SSH URL of the online repository.
The same git command to add an online repository with all available options is
git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=<fetch|push>] <name> <url>
For most beginner and everyday workflows, the short form is enough. Advanced options are useful when you want to track only selected branches, fetch immediately after adding the remote, or configure mirror behavior.
Add origin remote URL to a local Git repository
In the following example, we shall add an online repository to the git remote list, without any options.
git remote add origin https://github.com/tutorialkart/git-tutorial.git
Before adding a new remote, check whether the local repository already has a remote configured.
Let us check the existing list in git remote
~$ git remote -v
Well! There are no any. Lets go and add https://github.com/tutorialkart/git-tutorial.git to the git remote with name origin. You may use any other name for the online (remote) repository
~$ git remote add origin https://github.com/tutorialkart/git-tutorial.git
The online repository has been added to git remote, with name origin. Check the list once again.
~$ git remote -v
origin https://github.com/tutorialkart/git-tutorial.git (fetch)
origin https://github.com/tutorialkart/git-tutorial.git (push)
The repository has been added for push and fetch operations.
Use HTTPS or SSH remote URL with git remote add
You can add a remote repository using either an HTTPS URL or an SSH URL. Both point to the same online repository, but authentication works differently.
| Remote URL type | Example format | When to use it |
|---|---|---|
| HTTPS | https://github.com/user/repo.git | Useful for simple setup and personal access token based authentication. |
| SSH | git@github.com:user/repo.git | Useful when SSH keys are configured on your computer and Git hosting account. |
Example using an SSH URL:
git remote add origin git@github.com:tutorialkart/git-tutorial.git
After adding the remote, use git remote -v to confirm that the fetch and push URLs are set as expected.
Verify remote origin fetch and push URLs
The -v option shows the remote URL in verbose mode. It displays one URL for fetching from the remote and another URL for pushing to the remote.
git remote -v
origin https://github.com/tutorialkart/git-tutorial.git (fetch)
origin https://github.com/tutorialkart/git-tutorial.git (push)
If the command does not print anything, the local repository does not have any remote configured yet. If it prints a different URL, you may need to update the existing remote instead of adding a new one.
Push local branch after adding remote origin
Adding a remote repository only saves the remote name and URL. It does not automatically upload local commits. To upload your current branch for the first time, use git push with the -u option.
git push -u origin main
If your local branch is named master, use the following command instead.
git push -u origin master
The -u option sets the upstream branch. After this, you can usually run git push and git pull without specifying the remote and branch each time.
Fix origin already exists error in git remote add
If a remote named origin already exists, Git will not add another remote with the same name. You may see an error similar to the following.
fatal: remote origin already exists.
First, check the existing remote URL.
git remote -v
If the remote name is correct but the URL is wrong, update the URL with git remote set-url.
git remote set-url origin https://github.com/tutorialkart/git-tutorial.git
If you really want to remove the old remote and add it again, use the following commands carefully.
git remote remove origin
git remote add origin https://github.com/tutorialkart/git-tutorial.git
Add a local repository as a Git remote
A Git remote does not have to be an online repository. It can also point to another repository on the same computer or on a shared network path. This is useful for testing remote workflows locally.
git remote add backup ../backup-repository.git
In this example, backup is the remote name, and ../backup-repository.git is the path to another Git repository.
Common git remote add options
The full command supports several options, but these are the ones you are most likely to see in practice.
| Option | Purpose | Example use case |
|---|---|---|
-f | Fetch from the remote immediately after adding it. | Add a remote and download its refs in one step. |
-t <branch> | Track only a specific branch from the remote. | Track only main instead of all branches. |
--tags | Import tags from the remote. | Useful when tag history is needed locally. |
--no-tags | Do not import tags automatically. | Useful for narrower fetch behavior. |
Example of adding a remote and fetching from it immediately:
git remote add -f origin https://github.com/tutorialkart/git-tutorial.git
Git remote add troubleshooting checklist
- Run
git statusand make sure you are inside the correct local Git repository. - Run
git remote -vbefore adding a remote, especially if the repository already has history. - Use
originfor the main remote unless your workflow needs a different name. - Check whether the repository URL ends with the correct project name and owner or organization.
- Use
git remote set-urlwhen the remote exists but the URL is wrong. - Use
git push -u origin mainor the correct branch name after adding the remote.
Frequently asked questions about git remote add origin
What does git remote add origin do?
git remote add origin <url> creates a remote named origin in your local repository and connects it to the given repository URL. After that, Git can use origin for fetch, pull, and push operations.
Is origin required as the remote name in Git?
No. origin is only a common default name. You can use another name such as github, upstream, or backup. However, many tutorials and tools assume origin for the main remote repository.
Why does Git say remote origin already exists?
This error means your local repository already has a remote named origin. Use git remote -v to inspect it. If only the URL is wrong, update it with git remote set-url origin <new-url>.
Does git remote add upload my files to GitHub?
No. The git remote add command only adds the remote reference. To upload commits, run a push command such as git push -u origin main after committing your changes locally.
How do I check which remote repository my local Git project uses?
Run git remote -v. Git will display the configured remote names and URLs for fetch and push operations.
Editorial QA checklist for this git remote add tutorial
- Confirm that all examples use the same repository URL format consistently.
- Check that command examples are separated from output examples.
- Verify that
mainandmasterare both explained where first push examples are shown. - Ensure the troubleshooting section covers the
remote origin already existserror. - Confirm that existing code examples from the original tutorial remain unchanged.
Conclusion
In this Git Tutorial, we have learnt to add Remote/Online repository to git remote list.
The usual workflow is to create or open a local Git repository, add the online repository using git remote add origin <url>, verify it with git remote -v, and then push the local branch using git push -u origin main or the correct branch name for your project.
TutorialKart.com