Remote repositories are replicas of the Git repository at a central location for multiple people to access. This allows your developers to work on the same code base simultaneously and provides you with a backup of your code. There are various tools you can use to host your remote repositories. Notable ones include GitHub, Bitbucket, and Gerrit. You can install them on your on-premises or cloud servers or use a Software-as-a-Service (SaaS) platform to store them online. In this book, we are going to focus on GitHub.
GitHub is a web-based platform that helps developers collaborate on code. It is based on Git and allows you to host remote Git repositories. It was founded in 2008 and was acquired by Microsoft in 2018. It is one of the most popular open-source SaaS-based Git repository services and contains almost all open-source code available worldwide.
Before we can create our first remote repo, we must go to https://github.com/signup to create an account.
Once you’ve created an account, we can go ahead and create our first remote Git repository.
Creating a remote Git repository
Creating a remote Git Repository Name to Repository button.
repository is simple on GitHub. Go to https://github.com/new, set first-git-repo, keep the rest of the fields as-is, and click the Create
Once you’ve done that, GitHub will provide you with some steps that you can follow to connect with your remote repository. Before we go into any of that, we want to configure some authentication for our local Git command line to interact with the remote repository. Let’s take a look.
Setting up authentication with the remote Git repository
Some of the ways you can authenticate with your remote Git repository are as follows:
- HTTPS: In this mode, Git uses HTTPS to connect with the remote Git repository. We need to create an HTTPS token within our GitHub account and use this token as a password to authenticate with the remote repository. This process requires you to key in your token every time you authenticate with Git; therefore, it is not a convenient option.
- SSH: In this mode, Git uses the SSH protocol to connect with the remote Git repository. While using SSH, we do not need to use a password to authenticate; instead, we must add the public key of an SSH key pair we can generate from the Linux (or Windows if you’re using Git Bash) command line to the GitHub account. This process is more secure as well as convenient.
So, let’s set up SSH-based authentication with our remote Git repository.
First, we must generate the SSH key pair within our local system. Go to your Terminal and run the following command to generate an SSH key pair:
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
You will be prompted for other details. Keep pressing Enter until you reach the prompt again.
Once the key pair has been generated, copy the public key present in the ~/.ssh/id_rsa.pub file.
Then, go to https://github.com/settings/ssh/new, paste the public key in the Key field, and click the Add SSH Key button. We are now ready to connect with the remote Git repository. Now, let’s look at the configurations we must do on our local repository to connect with the remote repository.