Create a Remote Git Repository


I recently started using Git and it took me some time to find out how to just get the basics going – create a local repository and push it to a remote repository on my server. In this tutorial, we’ll cover how to do that real quick.

1. On your local machine, create the local repository. We’ll create one in our home directory:

cd ~
mkdir my_project

2. Create some project files inside the directory:

cd my_project
touch a.txt
touch b.txt

3. Now initialize the directory to become a git repository:

git init
# Add all files and directories to the repository
git add .
# Commit the new files to the repository with the log message "Initial commit"
git commit -m "Initial commit"

4. Now go to your server that will host the remote git repository:

We will be accessing the remote repository via SSH, so each user who wants to pull or push changes from and to the repository has to have an account on the server. It’s a good idea to create a new user/group “git” and add each user that will work with the repository to that group:

useradd git

Once the user is created, go to /etc/group and add each user to the git group:

vim /etc/group

Now we’ll create the remote repository on the server. You can choose your own location for it. In this tutorial, we’ll put it in /srv/git/.

mkdir -p /srv/git
cd /srv/git
git --bare init example.git

Above, we created a repository called example.git.

Make sure to set the ownership of the repository to the git user and give read/write permissions to the git group on that directory:

cd /srv
chown -R git:git git
chmod -R 775 git

Almost done.

5. Now go back to your local computer and add the local repository to the remote one:

git remote add origin ssh://username@your_server_domain.com/srv/git/example.git

Where, of course, you replace the username and your_server_domain.com with your respective values.

6. Finally, push your local directory to the remote one:

git push -u origin master

You’re done!

Now you can clone the remote repository to other computers and/or directories via:

git clone ssh://username@your_server_domain.com/srv/git/example.git
Bookmark the permalink.

Comments are closed