Git Remote

 What is git remote?

Git remote URL is the link of web address where your code is stored. As a beginner, these terms might confuse, so here I am trying to explain them as simply as possible. 

You might have heard these term many times, 

git push origin master 

For a moment, let's forget about the command push [LINK - if you want to know about it], and focus on what is "origin" is. To put it simply, origin is a short alias set by a developer to refer to a remote URL

So basically, the command is telling Git that please push branch name "master" to remote url-value set in "origin". 

Git remote url is the URL git will use to send and receive your code from your repository. 

Simple enough right? Hope now origin won't confuse you anymore. 

How to add remote URL?

To add remote URL, you can use the following command 

 git remote add origin <REMOTE_URL> 

This means that add remote URL with the value <REMOTE_URL> and to give it an alias called "origin". So Now let's see an example. 



git remote add origin https://github.com/apprajapati9/apprajapati9.github.io

//Now origin always confuses because it is used everywhere so it seems like 
//origin is something you have to use all the time
//But that is not the case, You can give any alias you want like this

git remote add github-website https://github.com/apprajapati9/apprajapati9.github.io

After this command, you do not have to run command with origin, because we gave a different alias so,  instead of 

git push origin master

you can say 

  
git push github-website master
//assuming we are pushing the branch name called "master"

Example:



From above picture, I hope that you have already not only learned how to add new URL, but also howto check existed remote URL(s) added before. (Please click on the picture to view full size if cannot be read properly)

Important note:

You can only push to two types of URL addresses: 

  • An HTTPS URL like https://github.com/user/repo.git
  • An SSH URL, like git@github.com:user/repo.git

Git associates a remote URL with a name, and your default remote is usually called origin.

How to change url of exisiting remote? 

In order to change set URL for existing remote alias, you can run the following cammand

git remote set-url <EXISTING_ALIAS> <UPDATED_URL>

//for example

git remote set-url newRemoteURL https://github.com/apprajapati9

Let us see that in real time example.


How to change remote alias name?

Now if you want to change the alias itself, the identifier which you use instead of a url, then you can use the following command

  
git remote rename newRemoteURL updatedRemoteURL

Above command will change remote url identifier/alias called newRemoteURL to updatedRemoteURL, so after this command, you can use updatedRemoteURL alias in order to push your branch/code.


 Remove remote URL 

Run following command to remove existing remote url.

git remote rm updatedRemoteUrl

above command will remove the existing remote added, so in case you want to push code/branch to another URL, you must add another url or check previously added remote url.



I hope this tuturial has helped you clear some confusion over "origin" word as well as clear understanding of how to manage remote URLs. Once again, remote URL is the url which your git will use in order to push or pull code from your remote repository.

Post a Comment

0 Comments