git remote add explained [Easy Examples]

 

How git remote add works

Git remote add is a git command that enables developers to work on a central remote repo by creating remote duplicates. Remote add command serves as a means through which collaborators for a project can independently make commits for a shared project. The remote add function in git also allows fetching changes made from the remote server to local. When using git, you must learn how to switch between the central remote to your own. That way, you can maintain all the status updates and make contributions through the push and fetch git methods.

In this tutorial about git remote add, we shall learn how to add a new remote repository, view remotes, and erase remotes in an active project.

Advertisement

 

Setting up the lab environment

To practice how git remote add function works, you will prepare a lab environment to run the experiments. I will be using git version 2.32.0.windows.2 and windows 10 pro as my local workstation to run experiments for this tutorial.

To begin, let’s clone a remote repository remote-add-demo a collaborated project and clone to the local server as follows;

$ git clone https://github.com/Josephine-Techie/remote-add-demo.git

Sample Output:
git remote add explained PROPERLY [Easy Examples]

 

git remote add vs git remote set-url

Running git remote add will create a new remote repository while git remote set-url will modify an existing remote repository URL. Let’s use an example to further illustrate how the two commands comparably works.

We shall start with the git remote add command in the example below.

To view the remotes in local project remote-add-demo using the master/main branch  we shall run git remote -v command:

Advertisement
$ git remote -v
myremote        https://github.com/Josephine-Techie/remote-add-demo.git (fetch)
myremote        https://github.com/Josephine-Techie/remote-add-demo.git (push)

Next, we will apply git remote add <remote> <url> command as shown below:

$ git remote add origin https://github.com/Josephine-Techie/remote-add-demo.git

$ git remote -v
myremote        https://github.com/Josephine-Techie/remote-add-demo.git (fetch)
myremote        https://github.com/Josephine-Techie/remote-add-demo.git (push)
origin  https://github.com/Josephine-Techie/remote-add-demo.git (fetch)
origin  https://github.com/Josephine-Techie/remote-add-demo.git (push)

You will notice that a new remote origin has been added to the list of remotes. Now let’s run git remote set-url to see what happen.

In this example instead using origin , we will  set up a new remote username by running git remote set-url command as follows:

$ git remote set-url newuser https://github.com/Josephine-Techie/remote-add-demo.git
error: No such remote 'newuser'

The output shows an error because the git remote set-url command doesn’t create a new remote like what the git remote add function does hence the error. Git remote set-url when run it tends to locate the remote name specified among the existing ones and assigns it a new url.

 

Create "remote repo" from the "central remote repo"

To create a remote repository that you can use to test your codes or committing changes away from the central remote project use the git remote add command.

We shall practice how to add a remote repository in the following example:

 

Step-1: List available remote repositories

First, let’s display the current remotes in the active repository remote-add-demo by running the git remote –v command. –v stands for verbose that enables outputting all remotes with their URL’s.

$ git remote -v
origin  https://github.com/Josephine-Techie/remote-add-demo.git (fetch)
origin  https://github.com/Josephine-Techie/remote-add-demo.git (push)

We only have origin as remote in the current repository.

Advertisement

 

Step-2: Use git remote add to create a new remote repository

Now, we are going to add a new remote myremote to help make and track changes that we make before merging into a central repository.

Let’s use the git remote add <remote-name> <url> function responsible for creating new remotes as follows;

$ git remote add myremote https://github.com/Josephine-Techie/remote-add-demo.git

To view the new remote myremote we shall run the git remote –v command as demonstrated below:

$ git remote -v

Sample Output:
git remote add explained PROPERLY [Easy Examples]

 

Step-3: Fetch all the refs and remote tracking information

While working independently from your remote branch you can fetch updates from the upstream to your local repository using the git fetch command.

You can fetch updates from upstream to your remote myremote in this case by running git fetch <remote> commands as shown:

$ git fetch myremote

Sample Output:
git remote add explained PROPERLY [Easy Examples]

 

How to push changes to a remote repository

Git allows you to push changes any time from your local server to the remotes found on your project. Use the git push <remote> <branch> command as demonstrated in the example below to push your changes to remote:

Using the active project remote-add-demo we shall check out a new branch mybranch and push the changes upstream to the remote myremote:

$ git push myremote mybranch

Sample Output:
git remote add explained PROPERLY [Easy Examples]

According to the output a new remote branch mybrnach was generated and changes pushed to myremote remote.

Let’s run git log --oneline command to view the commit history for the remote myremote.

$ git log --oneline
c2c6661 (HEAD -> mybranch, origin/master, origin/HEAD, myremote/mybranch, myremote/master, master) Initial commit

The case is different if you wish to push to the origin master. To push to origin master/main you will use git push origin master.

Let’s demonstrate how that works by checking out to master in the active repository remote-add-demo.

$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

$ git add .

$ git commit -m "myfile.css"
[master 1152c84] myfile.css
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 myfile.css

$ git push origin master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 281 bytes | 281.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/Josephine-Techie/remote-add-demo.git
   c2c6661..1152c84  master -> master

NOTE:

You may experience an error or rejection if you and another team member ran the cloning at the same time. That is especially if the teammate pushes the changes earlier than you. In such instances, you will need to fetch upstream changes to merge into yours before pushing again.

In case you want more details about a specific remote run the git remote show <remote> command. To put it into practice, let's apply the git remote show command on remote myremote in the local project remote-add-demo.

$ git remote show myremote
* remote myremote
  Fetch URL: https://github.com/Josephine-Techie/remote-add-demo.git
  Push  URL: https://github.com/Josephine-Techie/remote-add-demo.git
  HEAD branch: master
  Remote branches:
    Josephine-Techie-featb tracked
    master                 tracked
    mybranch               tracked
  Local refs configured for 'git push':
    master   pushes to master   (up to date)
    mybranch pushes to mybranch (up to date)

The above output details the commits history of the remote myremote. Myremote remote has two remote branches master and mybranch tracking their local counterparts master and mybranch.

 

How to rename your remote repository

To rename your remote you will use the git remote rename <old-remote> <new remote> command.

We shall run git remote –v command to views all the remotes in the active repository remote-add-demo as a start.

$ git remote -v

Sample Output:
git remote add explained PROPERLY [Easy Examples]

Next, we shall apply the  git remote rename <old-remote> <new remote> command to rename remote jremote to remote m_remote.

$ git remote rename jremote m_remote

To view the renamed remote m_remote we will run git remote –v command as follows:

$ git remote -v

Sample Output:
git remote add explained PROPERLY [Easy Examples]

Note that renaming a remote also affects the tracking remote of the renamed.

 

How to delete remote repository

To erase a remote you will use the git remote remove<remote-name> or git remote rm <remote-name> command.

First, let's display the remotes as shown below:

$ git remote -v
m_remote        https://github.com/Josephine-Techie/git-tester.git (fetch)
m_remote        https://github.com/Josephine-Techie/git-tester.git (push)
myremote        https://github.com/Josephine-Techie/remote-add-demo.git (fetch)
myremote        https://github.com/Josephine-Techie/remote-add-demo.git (push)
origin  https://github.com/Josephine-Techie/remote-add-demo.git (fetch)
origin  https://github.com/Josephine-Techie/remote-add-demo.git (push)

In this example, we will remove remote m_remote from the active repo remote-add-demo as follows:

$ git remote remove m_remote


$ git remote -v
myremote        https://github.com/Josephine-Techie/remote-add-demo.git (fetch)
myremote        https://github.com/Josephine-Techie/remote-add-demo.git (push)
origin  https://github.com/Josephine-Techie/remote-add-demo.git (fetch)
origin  https://github.com/Josephine-Techie/remote-add-demo.git (push)

Remote m_remote has been erased from the list of remotes as displayed in the above output. NOTE that this operation erases the tracking remote as well from the remote repository.

 

Summary

We have covered the following topics regarding the git remote add function:

  • Using git remote add to create a remote from the central remote
  • How to fetch a new remote
  • How to push changes to your remote
  • How to rename your remotes
  • How to erase your remotes

 

Further Reading

Git-remote-add
Manage git remote repositories

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment