DEV Community

Cover image for 02 Git: Git Remotes and GitHub
Jeet Mandaliya
Jeet Mandaliya

Posted on

02 Git: Git Remotes and GitHub

This post falls in the series after 01 Git: Basics in the series. If you haven't read it or wanna brush up your Git skills, consider giving it 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. Once it's done and you are already on the homepage, a new repository can be created with the option in dropdown named "+" in the top-right corner of the nav-bar.

Alternatively visiting 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.

https://github.com/new

https://github.com/new

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.

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. 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
Enter fullscreen mode Exit fullscreen mode

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

On the 2nd 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 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. You'll be required to generate an ssh-key first and then you can add it to your GitHub account. In the "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>"
Enter fullscreen mode Exit fullscreen mode

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.

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

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 🚀.

GitHub Repository Page After Pushing

GitHub Repository Page After Pushing

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 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.

Top comments (0)