Jaime Montoya
Jaime Montoya

Reputation: 7701

What should the "<url>" value be at "git remote add <shortname> <url>"?

I was reading the information at https://git-scm.com/docs/git-remote, and the syntax to add "a remote named <name> for the repository at <url>" is:

git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=<fetch|push>] <name> <url>

I am trying to understand the value I need to use for <url>. What do I need to do on my server? Assuming that in my remote server, I want to place the code at:

/home/myusername/public_html/mysourcecode

From what I understand, in this case at /home/myusername/public_html/mysourcecode, I should have a "public.git" folder, and in that folder, I should have files such as:

enter image description here

My two questions are:

1) What do I need to do from the server in order to generate the /home/myusername/public_html/mysourcecode/public.git/ with the content such as it is shown in the image above? Is it just putting my source code at /home/myusername/public_html/mysourcecode/ and then using git init?

2) Once I have /home/myusername/public_html/mysourcecode/public.git/ with the corresponding content, what would be my <url> that I can use in the code below when I add my remote repository?

    git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=<fetch|push>] <name> <url>

I was thinking about using something such as:

git remote add mysourcecode ssh://[email protected]/~myusername/public_html/mysourcecode/public.git

However, in that case my value for <url> would be ssh://[email protected]/~myusername/public_html/mysourcecode/public.git, and I typically see something such as:

git remote add origin https://github.com/folder/something.git

I would like to know what is the value for <url> that I am supposed to use.

Important: I am not using GitHub at all. I have my own server and I only use Git, not GitHub, so I am hosting everything on my own server. Typically I find online help that is specific to GitHub and it does not assume that I am hosting code on my own remote server.

Thank you.

Upvotes: 0

Views: 246

Answers (1)

Polygnome
Polygnome

Reputation: 7795

This is covered in the Pro Git book, under "Chapter 4: Git on the Server", online available at "https://git-scm.com/book/en/v1/Git-on-the-Server".

In order to use a remote repository , this repository must first exist. That means you need to have Git installed on the server.

Next, the repository on the server does not need to have a working directory, it only needs the git metadata. Such a repository is called a bare repository. You can create a bare repository from your old repository locally by using

$ git clone --bare mysourcecode mysourcecode.git
Cloning into bare repository 'mysourcecode.git'...
done.

You want to use Git over SSH as it would seem, so use SSH to copy this bare repository to the server, where it will be served by Git later:

$ scp -r mysourcecode.git [email protected]:/~myusername/git

I advise against using your public_html directory.

Now you can clone this project locally by using

$ git clone [email protected]:/~myusername/git/my_project.git

Or you add it as remote to the existing repository with

$ git remote add origin [email protected]:/~myusername/git/my_project.git

Note that you can skip the ssh:// and use a colon (:) between domain and path. Furthermore, the directory needs to be readable for the SSH user.


With regards to the URL with https://, this is an alternate way. You can use Git with different protocols - SSH, HTTPS and the Git protocol. All have their strengths and weaknesses.

The above mentioned book covers this topic extensively in Chapter 4, look there for more details, especially for the pros and cons of each protocol.

Upvotes: 1

Related Questions