git remote add
is a Git command that is used to add a new remote repository to your local Git repository. A remote repository is a Git repository that is hosted on a remote server and can be accessed by multiple developers. By adding a remote repository, you can collaborate with other developers and share your code with them.
To use git remote add
, you need to specify the name of the remote repository and its URL. The name is a local alias that you can use to refer to the remote repository, while the URL is the address of the remote repository.
Once you've added a remote repository, you can use Git commands like git push
and git pull
to interact with the remote repository. For example, you can push your changes to the remote repository using git push <remote-name> <branch-name>
, or pull changes from the remote repository using git pull <remote-name> <branch-name>
.
You can also specify additional options when using git remote add
, such as the branch that you want to track or the push URL for the remote repository. These options allow you to customize how your local repository interacts with the remote repository.
How git remote add works?
- Create a new repository on your Git hosting service. For example, if you are using GitHub, you can create a new repository by clicking on the "New repository" button on your dashboard.
- Copy the URL of the new repository. For example, if you are using GitHub, the URL will be in the format
https://github.com/username/repo.git
. - Open a terminal window and navigate to the directory that you want to initialize as a Git repository. For example, if you want to create a new Git repository in the directory
/home/user/myproject
, you would run the commandcd /home/user/myproject
. - Run the command
git init
to initialize the directory as a new Git repository. - Run the command
git remote add origin <url>
to add the new remote repository as the origin. Replace<url>
with the URL of the new repository that you copied earlier. - Run the command
git add .
to add all the files in the directory to the staging area. - Run the command
git commit -m "Initial commit"
to commit the changes. - Finally, run the command
git push -u origin master
to push the changes to the new remote repository on the master branch.
Setting up the lab environment
To practice how git remote add
function works, you will prepare a lab environment to run the experiments. I have created a repository on GitLab which I will be using in this tutorial
Here I have created two branch i.e. main
and test
branch.
Syntax to use git remote add command
The syntax for using git remote add
is as follows:
git remote add <name> <url>
Here, <name>
is the name of the remote repository that you want to add, and <url>
is the URL of the remote repository. The name can be any string that you choose, but it should be meaningful and easy to remember.
For example, if you want to add a remote repository named "origin
" with the URL "git@github.com:username/repo.git", you would use the following command:
git remote add origin git@github.com:username/repo.git
This command adds the remote repository named "origin" to your local repository's configuration and sets the URL to "git@github.com:username/repo.git".
You can also specify additional options when using git remote add
. For example, you can use the -t
option to specify the branch that you want to track on the remote repository:
git remote add origin git@github.com:username/repo.git -t master
This command adds the remote repository named "origin" to your local repository's configuration, sets the URL to "git@github.com:username/repo.git", and specifies that you want to track the "master
" branch on the remote repository.
Detailed steps to add remote repository
Pre-requisite - Initialize Git Repository
If you are starting on a fresh Linux or Windows server where git is not initialized and you attempt to add remote git server, then you will get:
fatal: Not a git repository (or any of the parent directories): .git
In such cases you first need to initialize a new Git repository in that directory using the git init
command.
Navigate to your directory where you plan to add the remote git repo. Assuming you plan to add repo inside /opt/deepak then:
cd /opt/deepak
Initialize a new Git repository in the directory using the git init
command:
git init
Step-1: Get Remote Git URL
Get the URL of the repository, in my case it is git@gitlab.com:golinuxcloud/git_examples.git. You can get this information under Clone button as shown below:
Step-2: Add Remote Repo
You can navigate to the root directory of the local Git repository using the cd
command in the terminal. Once you are in the correct directory, you can execute the git remote add
command with the correct syntax to add the remote repository. For example, to add a remote repository named "origin" with the URL "git@gitlab.com:golinuxcloud/git_examples.git", you can use the following command:
# git remote add git_examples git@gitlab.com:golinuxcloud/git_examples.git
Step-3: Verify Remote Repo
Verify that the remote repository has been added by using the git remote -v
command. This will display a list of all the remote repositories that have been added to the local Git repository.
# git remote -v
git_examples git@gitlab.com:golinuxcloud/git_examples.git (fetch)
git_examples git@gitlab.com:golinuxcloud/git_examples.git (push)
Step-4: Pull changes from branch
You can now use git pull along with your branch name to pull the changes from respective branch:
# git pull git_examples test
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From gitlab.com:golinuxcloud/git_examples
* branch test -> FETCH_HEAD
Updating a6b2526..308e153
Fast-forward
file1 | 1 +
1 file changed, 1 insertion(+)
create mode 100644 file1
git remote add vs git remote set-url
Both git remote add
and git remote set-url
are Git commands used to manage remote repositories. However, they are used for different purposes.
The git remote add
command is used to add a new remote repository to the local Git repository. This command creates a new remote reference in the local repository's configuration, pointing to the specified URL. If the remote reference already exists, it will be updated with the new URL.
The git remote set-url
command, on the other hand, is used to change the URL of an existing remote repository. This command updates the URL of the remote reference in the local repository's configuration, allowing you to switch the remote repository to a different URL.
Here is an example to illustrate the difference between the two commands:
In the previous section, we added a remote repository "git_examples" which was pointing to git@gitlab.com:golinuxcloud/git_examples.git
. Now if you want to change the URL of the "git_examples" remote repository to git@gitlab.com:golinuxcloud/new_git_examples.git
# git remote set-url git_examples git@gitlab.com:golinuxcloud/new_git_examples.git # git remote -v git_examples git@gitlab.com:golinuxcloud/new_git_examples.git (fetch) git_examples git@gitlab.com:golinuxcloud/new_git_examples.git (push)
This command updates the URL of the "git_examples
" remote reference in the local repository's configuration to the specified URL, allowing you to switch the remote repository to a different URL.
How to rename your remote repository
To rename your remote you will use the git remote rename <old-remote> <new remote>
command.
Run the command git remote -v
to view a list of all the remote repositories that are currently configured for your local repository.
# git remote -v
git_examples git@gitlab.com:golinuxcloud/new_git_examples.git (fetch)
git_examples git@gitlab.com:golinuxcloud/new_git_examples.git (push)
Identify the remote repository that you want to rename and note down its current name.
Run the command git remote rename <old-name> <new-name>
to rename the remote repository. Replace <old-name>
with the current name of the remote repository and <new-name>
with the new name that you want to assign to the repository.
# git remote rename git_examples new_git_examples
Verify that the remote repository has been renamed by running the command git remote -v again. The new name should now be displayed instead of the old name.
# git remote -v
new_git_examples git@gitlab.com:golinuxcloud/new_git_examples.git (fetch)
new_git_examples git@gitlab.com:golinuxcloud/new_git_examples.git (push)
If you have any local branches that were tracking the old name of the remote repository, you will need to update them to track the new name.
To do this, run the command git branch -vv
to view a list of all the local branches and their tracking information. Identify any branches that are tracking the old name of the remote repository and run the command git branch --set-upstream-to=<new-name>/<branch-name>
to update the branch to track the new name of the remote repository.
Replace <branch-name>
with the name of the branch that you want to update.
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
In summary, git remote add
is a Git command that is used to add a new remote repository to your local Git repository. This command is commonly used when you want to collaborate with others by sharing your code on a Git hosting service like GitHub or GitLab.
The basic syntax for the git remote add
command is as follows: git remote add <name> <url>
. The <name>
parameter specifies a name for the remote repository, and the <url>
parameter specifies the URL of the remote repository.
You can also use git remote add
to add additional remote repositories to your local Git repository, or to change the URL of an existing remote repository.
To rename a remote repository, you can use the git remote rename
command. This command is useful if you want to change the name of a remote repository that you are currently working with.
Further Reading
Manage git remote repositories