Git remote origin commands in a terminal

How to set Git remote origin in a local repository

To set Git remote origin for a local repository, add a remote named origin, verify its fetch and push URLs, then make the first push with upstream tracking. Run these commands from your local repository after creating at least one commit.

If the repository already has an origin, do not add it again. Check its current URL first, then change it with git remote set-url if necessary.

Set Git remote origin: check existing remotes with git remote -v

Check whether your local repository already has a Git remote origin:

git remote -v

Git lists each remote’s name and its fetch and push URLs. A configured origin may look like this:

origin  https://github.com/example/project.git (fetch)

origin  https://github.com/example/project.git (push)

If the command produces no output, your local repository has no configured remotes. If it lists a different remote name, such as upstream, you can still add origin separately.

Add a Git remote repository with git remote add origin <YOUR_REMOTE_URL>

To add a Git remote repository named origin, run the following command. Replace <YOUR_REMOTE_URL> with the repository’s complete HTTPS or SSH URL.

git remote add origin <YOUR_REMOTE_URL>

For example, a real command might use an address such as https://github.com/example/project.git or [email protected]:example/project.git. The name origin is a convention for the main remote, but Git lets you choose another name.

This command saves the remote in your local repository; it does not upload files. Use git remote -v again to confirm that Git saved the expected fetch and push URLs.

Git remote origin: verify URLs and push with git push -u origin <YOUR_BRANCH_NAME>

After adding the remote, verify both connection URLs:

git remote -v

Replace <YOUR_BRANCH_NAME> with the local branch you want to publish, such as main, then push it:

git push -u origin <YOUR_BRANCH_NAME>

The -u option sets the remote branch as the local branch’s upstream. After this first push, you can usually use git push and git pull without repeating the remote and branch names.

If Git reports that the branch does not exist, check your current branch name with:

git branch –show-current

Use that result as the value for <YOUR_BRANCH_NAME>. The remote repository must also be accessible with your configured credentials.

Fix the ‘remote origin already exists’ error with git remote set-url

If you run git remote add origin when origin is already configured, Git returns an error such as fatal: remote origin already exists. Adding the remote again is unnecessary. Inspect the existing configuration first:

git remote -v

If the URL is incorrect, replace the <YOUR_REMOTE_URL> placeholder with the correct repository URL and update the fetch URL:

git remote set-url origin <YOUR_REMOTE_URL>

Run git remote -v again to confirm the change. If the remote has a separate push URL, update that URL separately with:

git remote set-url –push origin <YOUR_REMOTE_URL>

Use set-url when origin should point to a different repository. Add another remote with a different name when you need to keep the existing origin and connect the repository to an additional destination.