git remote add

Remote

theme

Remote Repo (Source: amazonaws.com)

Software development activities usually involve many people or a team of programmers and of course, we will not keep the project repository separately. All teams involved in the development process will store the repository on their respective local computers but it will later be merged into the repository on the remote server.

The question is, where to store the remote repository?

The answer is, that it can be on an office server or you can also use services such as Github, Gitlab, Bitbucket, etc. However, Github is the most popular service for storing repositories remotely. Many open-source projects are stored there.

Github Distributed

Github Distributed (Source: sfdctechie.files.wordpress.com)

About remote repositories

Remote URL is Git’s way of saying “the place where our source code is stored”. Remote URL is another repository that is different from our local repository, namely the repository on the server. There are two types of remote URLs that we can add to the Git configuration:

  • HTTPS URL such as https://github.com/user/repo.git
  • SSH URL, such as [email protected]:user/repo.git

Git associates the remote URL with a name and defaults to origin. It is stored in file config in folder .git in root directory. inside the file it will look like this:

[remote "origin"]
url = https://github.com/user/repo.git
fetch = +refs/heads/*:refs/remotes/origin/*

Add remote repositories to local git

We can use the git remote add command to associate the name with the remote URL, for example:

git remote add origin <REMOTE_URL>

This means origin will be linked to REMOTE_URL. We can use the git remote set-url command to change the remote URL.

Managing list of remote repositories in the local git

Adding a remote repository

To add a new remote, use the git remote add command in the terminal in the Git repository. The git remote add command requires two arguments, namely:

  • Remote Name, for example, origin
  • Remote URL, for example, https://github.com/user/repo.git

Example:

[user@localhost]$ git remote add origin https://github.com/user/repo.git
# Set a new remote

[user@localhost]$ git remote -v
# Verify new remote
> origin https://github.com/user/repo.git (fetch)
> origin https://github.com/user/repo.git (push)

Changing a remote repository’s URL

The git remote set-url command will change the existing remote repository URL.

Tip: For information about the differences between HTTPS and SSH URLs, see “About remote repositories.”

  • Name of existing remote. For example, origin or upstream are two common choices.

  • New URL for remote, example:

    • If we change remote to HTTPS then the URL will look like this:

      https://github.com/USERNAME/REPOSITORY.git
    • If we change remote to SSH then the URL will look like this:

      [email protected]:USERNAME/REPOSITORY.git

Removing a remote repository

Use the git remote rm command to remove the remote URL from the repository. The git remote rm command takes one argument namely remote name, for example, destination

Note: Deleting the remote URL from the repository will break the connection between the local repository and the remote. It won’t delete the remote repository.

Example:

[user@localhost]$ git remote -v
# View current remotes
> origin https://github.com/OWNER/REPOSITORY.git (fetch)
> origin https://github.com/OWNER/REPOSITORY.git (push)
> destination https://github.com/FORKER/REPOSITORY.git (fetch)
> destination https://github.com/FORKER/REPOSITORY.git (push)

[user@localhost]$ git remote rm destination
# Remove remote
[user@localhost]$ git remote -v
# Verify it's gone
> origin https://github.com/OWNER/REPOSITORY.git (fetch)
> origin https://github.com/OWNER/REPOSITORY.git (push)

Example Multiple Remotes

Adding multiple remotes

We can add more than one remote repository to our project. Do you still remember the git remote add origin command above? The original syntax for git remote add is like this (the text after the # is a comment):

# Syntax to add a git remote
[user@localhost]$ git remote add REMOTE-NAME REMOTE-URL

For example, we will add several remotes to our repository with origin as the main remote:

# Add remote 1: GitHub.
[user@localhost]$ git remote add origin [email protected]:jigarius/toggl2redmine.git
# Add remote 2: BitBucket.
[user@localhost]$ git remote add upstream [email protected]:jigarius/toggl2redmine.git
# Add remote 3: Gitlab.
[user@localhost]$ git remote add aditira [email protected]:jigarius/toggl2redmine.git

In the example above, we added a remote repository of a project called Toggl 2 Redmine on GitHub. We can use the command above to add one or more remote repositories, but make sure each repository has a different name, namely origin, upstream and aditira in the example above.

  • Configure primary remote

    Although we can add multiple remotes, typically, each branch of a project can be configured to track one remote. We can set branch to track remote with the following example:

    # Change local branch.
    [user@localhost]$ git checkout BRANCH
    
    # Configure local branch to track a remote branch.
    [user@localhost]$ git branch -u origin/BRANCH

    Here, BRANCH is the name of the remote branch, which is usually the same as our local branch.

  • Change remote URL

    If we want to change the URL that is connected to the added remote, just as explained above, we use the git remote set-url command, where the original syntax is:

    # The syntax is: git remote set-url REMOTE-NAME REMOTE-URL
    [user@localhost]$ git remote set-url upstream [email protected]:jigarius/toggl2redmine.git

List all remotes

To see all remotes, just use the following command:

[user@localhost]$ git remote -v
origin [email protected]:jigarius/toggl2redmine.git (fetch)
origin [email protected]:jigarius/toggl2redmine.git (push)
upstream [email protected]:jigarius/toggl2redmine.git (fetch)
upstream [email protected]:jigarius/toggl2redmine.git (push)
aditira [email protected]:jigarius/toggl2redmine.git (push)
aditira [email protected]:jigarius/toggl2redmine.git (push)

Remove a remote

If there is a remote repository that is no longer needed in local Git, we can delete it with the command:

# The syntax is: git remote remove REMOTE-NAME
[user@localhost]$ git remote remove upstream
Back to top