02 Git: Git Remotes and GitHub
Tags: git, github
Published: 2021-01-28 00:34:00

This post falls in the series after [01 Git: Basics](https://webdevjeet.me/git-basics/) in the [series](https://webdevjeet.me/tag/git-an-essential-tool-for-devs/). If you haven't read it or wanna brush up your Git skills, consider giving [it](https://webdevjeet.me/2020-12-git-basics/) a shot first 🚀.

In the above-mentioned post, we discussed basic Git concepts like initializing a Git repository(abbr. "repo"), creating a commit, and getting info about differences in files. All those were being done only on our machine only.

In this post, we're gonna take a look at how to manage remotes, which allow us to maintain a copy of our Git repository in a remote server. There are countless benefits of having a remote repository for Git, some are listed below.

*   Ability to have collaborators/contributors on our project from across the globe.
*   Perform automated actions like deploying a new version of our software, checking code-quality of our software, and much more.
*   Show your projects to the world with a simple `git push`.
*   Always have a copy of your code on a remote server and never lose it.

### Creating A Remote Repository With GitHub

> **GitHub** is a code hosting platform for version control and collaboration.

If you don't have an account on GitHub, it can be created [here](https://github.com/). Once it's done and you are already on the [homepage](https://github.com/), a new repository can be created with the option in dropdown named "+" in the top-right corner of the nav-bar.

<figure>![](https://dev-to-uploads.s3.amazonaws.com/i/o9j77anueg5qid9tc6rj.jpg)</figure>

Alternatively visiting [https://github.com/new](https://github.com/new) will take us to the create new repo page. Once you're on that page, you should see something similar to the following.

<figure>![https://github.com/new](https://dev-to-uploads.s3.amazonaws.com/i/g2bp0fri5dotl6qk5ig1.jpg)

<figcaption><a href="https://github.com/new" />https://github.com/new</a></figcaption>

</figure>

You can make the repository private/public according to your choice. For now, we're going to leave all the options in "_Initialize this repository with_" unchecked. After hitting the "create repository" button, we'll be taken to the repo page. There you can see configurations about pushing an existing repo from Command-Line-Interface(abbr. "CLI").

Three ways to push to the repository are mentioned here. For the scope of this post, we'll focus on the first two only.

<figure>![](https://dev-to-uploads.s3.amazonaws.com/i/hlcj8vy9k3ils52221uj.jpg)</figure>

### Pushing After Creating A New Repo via CLI

We had a thorough discussion about initializing a git repo via CLI and committing files in our previous post [01 Git: Basics](https://webdevjeet.me/2020-12-git-basics/). Once we're done with initializing the Git repo locally and committing at least once, we can focus on pushing our repository to GitHub. Now comes the part where we configure the GitHub as our `origin` remote in our local Git repo. We can have multiple remotes configured in our local repo and `origin` is the default remote. When we don't specify any remote while executing Git commands, it's going to operate with `origin` remote by default.

To list all the remotes we can run `git remote -v`. Right now as we won't have any remotes configured, it'll output nothing. Now we can add the origin remote by writing the following command.

```
git remote add origin https://github.com/sereneinserenade/first-repo.git
```

As mentioned earlier, to list the remotes, we can run `git remote -v`. This time, it'll show us the remote we added.

<figure>![](https://dev-to-uploads.s3.amazonaws.com/i/9qb8o8aw58jvff3al0pl.jpg)</figure>

On the 2<sup>nd</sup> line, we see that our push config is set to `https://github.com/sereneinserenade/first-repo.git`. Pushing to origin can be done with `git push origin master`. Notice that we've added remote with `HTTP`, thus Git will ask for username and password for GitHub when we will be pushing our repo.

As an alternative to that, we can use `ssh` and configure [SSH](https://en.wikipedia.org/wiki/SSH_(Secure_Shell)) key for GitHub and use it as an authentication method. A comprehensive guide on how to generate an ssh-key and add it to your GitHub account can be found [here](https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/connecting-to-github-with-ssh). You'll be required to [generate an ssh-key](https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent) first and then you can [add it to your GitHub](https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account) account. In the "[Adding a new SSH key to your GitHub account](https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account)" they use commands like `pbcopy` or `xclip`. We can simply do it by using `echo ~/.ssh/id_ed25519.pub` and then copying output to the clipboard manually with `ctrl/cmd + c`.

But before all that, configuring git is better since it allows other collaborators to identify the author of specific commits. Configuring Git requires the Name and Email of the Git User to be configured. Following would help in configuring.

```
git config --global user.name "<YOUR NAME HERE>"
git config --global user.email "<YOUR PUBLIC_EMAIL HERE>"
```

I already have an SSH key for GitHub configured on my machine, and I will be continuing with that. When we use `ssh`, we also need to configure our remote as `git@github.com:sereneinserenade/first-repo.git` as mentioned below.

<figure>![](https://dev-to-uploads.s3.amazonaws.com/i/50810xkme7oyloy27mfp.jpg)</figure>

We can finally push it by using `git push origin master` as mentioned earlier.

<figure>![](https://dev-to-uploads.s3.amazonaws.com/i/a3zuzlngeyaz754ix2yz.jpg)</figure>

Focusing on the last line of output, we see `master -> master` which basically means that we pushed the local branch `master` to the remote branch `master` or `origin/master`. We will be discussing Git Branches in its own post, for now, the only thing to keep in mind is that in a repo generated by `git init`, the default branch is `master` or `main` and we push it to remote respectively with their names, i.e if the output of `git branch` is `master`, we can push it with `git push origin master` and if it's `main`, we can push it by `git push origin main`.  

**That's it! We've pushed the repo and we can see in GitHub that our file** `**learnings.md**` **was also uploaded 🚀.**

<figure>![GitHub Repository Page After Pushing](https://dev-to-uploads.s3.amazonaws.com/i/wld36sy2fu1rg4rqfdpk.jpg)

<figcaption>GitHub Repository Page After Pushing</figcaption>

</figure>

### What We Discussed In This Iteration:

*   Knowing what are Git Remotes - done!
*   How to Create a New Repository on GitHub - done!
*   How to Configure SSH key for GitHub - done!
*   How to add Git Remotes to our local copy of repo - done!
*   Pushing our local repo to GitHub - done!

**Alrighty then 🚀, now you know how to work with Git Remotes and GitHub to keep your code online!!!**

Next post in this series is [03 Git: Pull - Getting The Remote Changes](https://webdevjeet.me/2021-01-git-pull-getting-the-remote-changes/) where we talk about how to do changes in GitHub UI and fetch them to our local machine.

---
This post is originally written [on my blog](https://webdevjeet.me/2020-12-git-remotes-and-github/).