Managing remote repositories

Adding a remote repository

To add a new remote, use the git remote add command in the terminal, in the directory where your repository is stored.

The git remote add command The command takes two arguments:

  • A remote name, for example, origin
  • A remote URL, for example, https://github.com/ OWNER/REPOSITORY.git

For example:

$ git remote add origin https://github.com/OWNER/REPOSITORY.git # Set a new remote $ git remote -v # Verify new remote > source https://github.com/OWNER/REPOSITORY.git (fetch) > source https://github.com/OWNER/REPOSITORY.git (push)

For more information about which URL to use, see “About remote repositories.”

Troubleshooting: remote source already exists

This error means that you tried to add a remote with a name that already exists. exists in your local repository.

$ git remote add origin https://github.com/octocat/Sp oon-Knife.git > fatal: Remote origin already exists.

To work around this, you can:

  • Use a different name for the new remote.
  • Rename the existing remote repository before adding the new remote. For more information, see “Rename a remote repository” below.
  • Delete the existing remote repository before adding the new remote. For more information, see “Deleting a remote repository” below.

Changing the URL of a remote repository

The git remote set-url command changes the URL of an existing remote repository.

The git remote set-url command takes two arguments:

  • An existing remote name. For example, origin or upstream are two common options.
  • A new URL for the remote control. For example:
    • If you are upgrading to use HTTPS, your URL might look like: https://github.com/OWNER/REPOSITORY.git
    • If you are upgrading to use SSH, your URL could look like: [email protected]:OWNER/REPOSITORY.git
See Also:  How to Make a Website Quickly: Your Easy 6-Step Guide

Change remote URLs from SSH to HTTPS

  1. Open TerminalTerminalGit Bash.
  2. Change the current working directory to your local project.
  3. List your existing remotes to get the name of the remote you want to change. $ git remote -v > origin [email protected]:OWNER/REPOSITORY.git (fetch) > origin [email protected]:OWNER/REPOSITORY.git (push)
  4. Change the URL of your control remote from SSH to HTTPS with the git remote set-url command. $ git remote set-url origin https://github.com/OWNER/REPOSITORY.git
  5. Verify that the remote URL has changed. $ git remote -v # Check new remote URL > source https://github.com/OWNER/REPOSITORY.git (fetch) > source https://github.com/OWNER/REPOSITORY.git (push)

The next time you use git fetch, git pull, or git push on the remote repository, you’ll be prompted for your GitHub username and password. When Git asks for your password, enter your personal access token. Alternatively, you can use a credential helper like Git Credential Manager. Password-based authentication for Git has been removed in favor of more secure authentication methods. For more information, see “Creating a personal access token”.

You can use a credential helper to have Git remember your GitHub username and personal access token each time you communicate. with GitHub.

Change remote URLs from HTTPS to SSH

  1. Open TerminalTerminalGit Bash.
  2. Change the current working directory to your local project.
  3. List your existing remotes to get the name of the remote you want to change. $ git remote -v > origin https://github.com/OWNER/REPOSITORY.git (search) > origin https://github.com/OWNER/REPOSITORY.git (push)
  4. Change your HTTPS-to-SSH remote control URL with the git remote set-url command. $ git remote set-url origin [email protected]:OWNER/REPOSITORY.git
  5. Verify that the remote URL has changed. $ git remote -v # Check new remote URL > origin [email protected]: OWNER/REPOSITORY.git (fetch) > origin [email protected]: OWNER/REPOSITORY.git (push)
See Also:  How To Broadcast Zwift From Any Device Onto Your Big Screen TV

Troubleshooting: Remote ‘[name]’ does not exist

This error means that the remote you tried to change does not exist:

$ git remote set-url sofake https: // github.com/octocat/Spoon-Knife > fatal: No such remote sofake

Check that you spelled the remote name correctly.

Renaming a remote repository

Use the git remote rename command to rename an existing remote.

The git remote rename command takes two arguments:

  • An existing remote name, for example , source
  • A new name for the remote, eg destination

Example of renaming a remote repository

These examples assume You are cloning using HTTPS, which is recommended.

$ git remote -v # See remotes exist tes > source https://github.com/OWNER/REPOSITORY.git (fetch) > source https://github.com/OWNER/REPOSITOR Y.git (push) $ git remote rename source target # Rename remote from ‘source’ to ‘target’ $ git remote -v # Check new remote name > target https://github.com/OWNER/REPOSITORY.git (find) > target https://github.com/OWNER/REPOSITORY.git (push)

Troubleshooting: Could not rename config section ‘remote.[old name]’ to ‘ remote.[new name]’

This error means that the old remote name you typed doesn’t exist.

You can check which remotes currently exist with the command git remote -v:

$ git remote -v # View existing remotes > source https://github.com/OWNER/REPOSITORY.git (lookup) > source https://github.com/OWNER/REPOSITORY.git (push)

Troubleshooting: Remote [new name] already exists

This error means that the remote name you want to use already exists. To resolve this, use a different remote name or rename the original remote.

Removing a remote repository

Use the git remote rm command to remove a remote URL from your repository.

See Also:  How to create a blog in PHP and MySQL database

The git remote rm command takes one argument:

  • A remote name, for example, target

Remove the remote URL from your repository only unlinks the local and remote repositories. It does not delete the remote repository.

Example deleting a remote repository

These examples assume you are cloning using HTTPS, which is recommended.

$ git remote -v # View current remotes > source https://github.com/OWNER/REPOSITORY.git (search) > source https://github.com/OWNER/REPOSITORY.git (push) > target https://github.com /FORKER/REPOSITORY.git (find) > target https://github.com/FORKER/REPOSITORY.git (push) $ git remote rm target # Remove remote $ git remote -v # Verify that it’s gone > source https: // github.com/OWNER/REPOSITORY.git (search) > origin https://github.com/OWNER/REPOSITORY.git (push)

Troubleshooting: Could not remove config section ‘remote. [name]’

This error means that the remote you tried to remove does not exist:

$ git remote rm sofake > error: Could not remove configuration section ‘remote.sofake’

Check that you have done it correctly you wrote the name bre of the remote control.

Further reading

  • “Working with Remote Controls” from the Pro G book

.

Leave a Reply

Your email address will not be published. Required fields are marked *