DEV Community

loading...
Cover image for Git and GitHub Essentials - #3 Working with Remotes

Git and GitHub Essentials - #3 Working with Remotes

envoy_ profile image Envoy-VC ・2 min read

📌Section 1.1: Setting a Remote Branch

git remote add origin /link/to/git/repo
Enter fullscreen mode Exit fullscreen mode

📌 Section 1.2: Deleting a Remote Branch

To delete a remote branch in Git:

git push [remote-name] --delete [branch-name]
Enter fullscreen mode Exit fullscreen mode

📌Section 1.3: Changing Git Remote URL

Check existing remote

git remote -v
# origin https://github.com/username/repo.git (fetch)
# origin https://github.com/usernam/repo.git (push)
Enter fullscreen mode Exit fullscreen mode

Changing repository URL

git remote set-url origin https://github.com/username/repo2.git
# Change the 'origin' remote's URL
Enter fullscreen mode Exit fullscreen mode

Verify new remote URL

git remote -v
# origin https://github.com/username/repo2.git (fetch)
# origin https://github.com/username/repo2.git (push)
Enter fullscreen mode Exit fullscreen mode

📌Section 1.4: List Existing Remotes

List all the existing remotes associated with this repository:

git remote
Enter fullscreen mode Exit fullscreen mode

List all the existing remotes associated with this repository in detail including the fetch and push URLs:

git remote --verbose
Enter fullscreen mode Exit fullscreen mode

or simply

git remote -v
Enter fullscreen mode Exit fullscreen mode

📌Section 1.5: Getting Started

Syntax for pushing to a remote branch

git push <remote_name> <branch_name>
Enter fullscreen mode Exit fullscreen mode

Example

git push origin master
Enter fullscreen mode Exit fullscreen mode

📌Section 1.6: Renaming a Remote

To rename remote, use command git remote rename
The git remote rename command takes two arguments:

  • An existing remote name, for example : origin
  • A new name for the remote, for example : destination Get existing remote name
git remote
# origin
Enter fullscreen mode Exit fullscreen mode

Check existing remote with URL

git remote -v
# origin https://github.com/username/repo.git (fetch)
# origin https://github.com/usernam/repo.git (push)
Enter fullscreen mode Exit fullscreen mode

Rename remote

git remote rename origin destination
# Change remote name from 'origin' to 'destination'
Enter fullscreen mode Exit fullscreen mode

Verify new name

git remote -v
# destination https://github.com/username/repo.git (fetch)
# destination https://github.com/usernam/repo.git (push)
Enter fullscreen mode Exit fullscreen mode

=== Posible Errors ===

  1. Could not rename config section 'remote.[old name]' to 'remote.[new name]' This error means that the remote you tried the old remote name (origin) doesn't exist.
  2. Remote [new name] already exists. Error message is self explanatory

📌Section 1.7: Show information about a Specific Remote

Output some information about a known remote: origin

git remote show origin
Enter fullscreen mode Exit fullscreen mode

Print just the remote's URL:

git config --get remote.origin.url
Enter fullscreen mode Exit fullscreen mode

📌Section 1.8: Set the URL for a Specific Remote

You can change the url of an existing remote by the command

git remote set-url remote-name url
Enter fullscreen mode Exit fullscreen mode

📌Section 1.9: Get the URL for a Specific Remote

You can obtain the url for an existing remote by using the command

git remote get-url <name>
Enter fullscreen mode Exit fullscreen mode

By default, this will be

git remote get-url origin
Enter fullscreen mode Exit fullscreen mode

📌Section 1.10: Changing a Remote Repository

To change the URL of the repository you want your remote to point to, you can use the set-url option, like so:

git remote set-url <remote_name> <remote_repository_url>
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide