Link With Remote Repository – Git Remote
git remote
is the command used when you want to establish and check the linking status between a Remote Repository and your Local Repository. There are some derivative commands and options for this command. We'll explain five major usages on this page.
git remote add
: Establish a link with a Remote Repositorygit remote -v
: Check the status of the linked Remote Repositorygit remote rm
: Delete the Remote Repository linkgit remote set-url
: Change the Remote Repository URLgit remote rename
: Change the Remote Repository name
1. git remote add
Establishes a link between a Local Repository and a Remote Repository. As explained before, when you launch a project as the owner of the Remote Repository, you need to upload your project directory to the Remote Repository first. Before pushing the code, you need to establish a link between your Local Repository and the Remote Repository. The command to establish the link is git remote add
. You need to specify a Remote Repository name and Remote Repository URL as shown below. The Remote Repository name is typically origin. You can get the URL from the Remote Repository page on GitHub.
git remote add [Remote Repository name] [Remote Repository URL]
2. git remote -v
Checks registered Remote Repositories. After you create a link with a Remote Repository by running the git remote add
command or the git clone
command, you may want to check the status of the link. git remote -v
is the command used to check the status of the Remote Repository linked with your Local Repository. This command doesn't require any argument as shown below.
git remote -v
3. git remote rm
Deletes the Remote Repository link. When you want to deregister the link in the Remote Repository, the git remote rm
command is used. To run the command, you need to specify the Remote Repository name as shown below.
git remote rm [Remote Repository name]
4. git remote set-url
Changes the Remote Repository URL. When you want to change an SSH connection to an HTTPS connection or you simply want to change the URL of the Remote Repository, you can run the git remote set-url
command. When you run the command, you need to specify the existing Remote Repository name and new URL.
git remote set-url [existing Remote Repository name] [new URL]
5. git remote rename
Changes the Remote Repository name. The typical name of a Remote Repository is origin. However, there may be a situation, such as managing multiple Remote Repositories at the same time, when you want to use different names for the Remote Repositories. You can change the Remote Repository name by running the git remote rename
command.
git remote rename [existing Remote Repository name] [new Remote Repository name]
For a better understanding, please go through the following practice section.
Practice
Developer A (Project Owner Role)
Objective:
Check how the git remote commands work
1. Setup a practice project directory and file for this chapter
Directory and file structure
For this practice, we'll use the following directory and file. The directory and file will be used throughout the practices in this chapter.
- Practice project directory: git_remote_practice
- Practice file: git_remote_practice.html
The screenshot below is the target directory structure example based on Mac OS.
Create the practice project directory and file
Open the project's main directory (e.g., Dev_A_bloovee) with VS Code. You can use drag & drop to open the directory.
You can also use the command line to move the current directory to the main project directory.
cd ~/Dev_A_bloovee
After opening the project's main directory with VS Code, open a new terminal in the VS Code window and run the command below to create the directory and file.
mkdir git_remote_practice
cd git_remote_practice
touch git_remote_practice.html
Edit the practice HTML file
For the HTML file, make edits like those shown below. We'll use four branches. For practice purposes, create a coding section for each branch. For the first commit on the master branch, add <h1>M1LA</h1>
after <!-- Master Branch-->
.
M1LA indicates the following for practice purposes:
- M – Master Branch
- 1 – 1st commit on the branch
- L – Committed in the Local Repository
- A – Committed by Developer A
<!doctype html>
<html lang="en">
<body>
<!-- Master Branch-->
<h1>M1LA</h1>
<!-- /Master Branch-->
<!-- Branch A-->
N/A
<!-- /Branch A-->
<!-- Branch B-->
N/A
<!-- /Branch B-->
<!-- Branch C-->
N/A
<!-- /Branch C-->
</body>
Create a Local Repository and make the first commit
As we are working on a new directory, we need to create a new Local Repository. Also, let's make the first commit with the commit message "M1LA", and check the commit status.
git init
git add .
git commit -am "M1LA"
git log --oneline
You can see that the first commit was successfully made.
hint: Using 'master' as the name for the initial branch. This default branch name
:
hint: git branch -m <name>
Initialized empty Git repository in /Users/bloovee/Dev_A_bloovee/git_remote_practice/.git/
[master (root-commit) 4b30a1c] M1LA
1 file changed, 19 insertions(+)
create mode 100644 git_remote_practice.html
4b30a1c (HEAD -> master) M1LA
2. Create a Remote Repository
Next, create a new Remote Repository for this practice. Go to the GitHub website and log into your account. Click the + button on the top right and select New repository. You'll reach the page as shown below. Add the Repository name (use the same name as your project directory (git_remote_practice) to avoid confusion.
After you press the green button to create a new repository, you'll see the following Quick setup page.
First, we'll try to use the HTTPS connection. Copy the URL by clicking the button on the right.
Go back to the command line and run the git remote add
command with a Remote Repository name (typically, origin is used) and the URL copied from the GitHub website. Also, run the git remote -v
command to check the Remote Repository status.
git remote add origin https://github.com/bloovee/git_remote_practice.git
git remote -v
You can see that the Remote Repository was successfully registered in your Local Repository.
origin
https://github.com/bloovee/git_remote_practice.git
(fetch) origin
https://github.com/bloovee/git_remote_practice.git
(push)
3. Delete a Remote Repository link
To delete the Remote Repository link, run the git remote rm
command as shown below. Also, run the git remote -v
command to check the status.
git remote rm origin
git remote -v
There will be no response as the Remote Repository was already deleted.
For the next step, add the Remote Repository again.
git remote add origin https://github.com/bloovee/git_remote_practice.git
git remote -v
Confirm that the Remote Repository was correctly registered.
origin
https://github.com/bloovee/git_remote_practice.git
(fetch) origin
https://github.com/bloovee/git_remote_practice.git
(push)
4. Change the Remote Repository URL
In this practice, we try to change the Remote Repository URL from HTTPS to SSH. First, get the new URL from the Remote Repository. Click the SSH button and click the right button to copy the URL.
Run the git remote set-url
command with the copied URL. Also, run the git remote -v
command to confirm the status.
git remote set-url origin git@github.com:bloovee/git_remote_practice.git
git remote -v
You can see that the URL changed to SSH URL.
origin git@github.com:bloovee/git_remote_practice.git (fetch)
origin git@github.com:bloovee/git_remote_practice.git (push)
5. Change the Remote Repository name
Lastly, try to change the Remote Repository name. This time change the name from origin to origin_2 as shown below.
git remote rename origin origin_2
git remote -v
You can see that the name has changed to origin_2.
origin_2 git@github.com:bloovee/git_remote_practice.git (fetch)
origin_2 git@github.com:bloovee/git_remote_practice.git (push)
For the next practice, revert the name to origin.
git remote rename origin_2 origin
git remote -v
Confirm the name was changed back to origin.
origin git@github.com:bloovee/git_remote_practice.git (fetch)
origin git@github.com:bloovee/git_remote_practice.git (push)