• 20 hours
  • Easy

Free online content available in this course.



Got it!

Last updated on 10/27/22

Write code efficiently by using a local repository

Log in or subscribe for free to enjoy all this course has to offer!

Decentralized, remote, & local repositories

Code repositories can be set up on a server, in a file on your own computer, and online! There are two main types: centralized and decentralized.  A centralized repository typically has one copy of a file that everyone shares.  You've likely used these without realizing it. For example, your office or school may have a server where you keep your files, and anyone with authorization can access them when needed.

These types of repositories allow members of a team project to work on the same file one after another; however, there is still the issue of two team members using the same file at the same time.  Also, if your backups aren't up to date and something happens to that server, you'll lose all your files! ☠️

Git is based on using a decentralized or distributed system, which basically means that its components are shared across a bunch of linked computers. The same thing goes for its repositories! 😉

A Distributed System
A distributed system

In a decentralized system, you have one main repository, also called a remote repository, which contains code for a project.

Why are we using the word "remote" to describe it?

Code repositories are hosted on servers in a decentralized system. When developers want to work off of a remote repository, they create their own copy of that remote repository on their computers. When a copy of the remote repository is downloaded, it is called a local repository. And that's exactly the process we described in Chapter 1 - we just didn't use the fancy terminology.  😉

You'll remember, that since a repository is on their own computers, developers can work on any files they want for as long as they want, without affecting the code located in the main repository. So, now we have a decentralized system where local and remote documents can communicate back and forth with each other! Cool, huh? 🤓

Why use a local repository?

It’s easier to write, build, and test your code using a compiler on your computer. Once it's completed, you can save it on your own computer, then send it back to your project on the GitHub main repository. 

Why can't I just write on GitHub directly? 

When working directly on GitHub, you can make a few small changes to the files in your repository and commit them, but it's not a good practice when making bigger code changes on large projects.  Typically, large projects have a lot of people involved with a lot of moving parts. Also, the changes involve a lot of coding. GitHub isn’t built for continuously code writing - just storing it! 

Additionally, with a local repository, you can still use a version control system directly with your integrated development environment (IDE) or compiler. Since Git is a distributed system, you have all the same functionalities on your local repo as you do on GitHub.

This includes updating between your local repository and your remote repository. Let’s say you were working on a pretty big project with a specific feature that has taken you two weeks to complete!  Meanwhile, other team members have probably changed the code in that main repository on GitHub, right? So what do you do if there have been changes on the remote repository, especially if it's connected to the feature you are working on? 😧

Git says, "No problem!  We've got this covered."

Decentralized version control systems (DVCS) like Git allow you to update your local repository with any changes that were made to the remote repository while you were working. You can pull, or fetch, and update all the changes from the remote repository whenever you want with a simple command!  This way, you can ensure that your local repository is current on all the changes and using the latest version of the project in the main repository.

This is another reason why it's important to test new versions come from various local repositories and keep the different versions separate from the main repository. Since DVCS allows you to save your changes without making them a part of the main repository, nothing will get lost, and you have time to test other local versions before integrating them into the main codebase.

Install Git locally

To get started on your local repository, you will have to install the Git versioning software.  Let’s get started with it so you can contribute to an open source project on GitHub!

First go to the Official Git Software page. You will see that there are installation packages for three operating systems.  Choose the option for your computer and run the installation. It will download and install a command line interface that you can also use to work on your local repository.

Git downloads
Git downloads

After you download Git, you can run through the installation using default settings. Once your installation is complete, it's time to configure the Git software on your computer to start working on your first local repository!

Configure your repository

Open the command line interface (CLI) for Git by launching Git Bash. This command line interface is called bash, which is a Linux CLI language.  Since we are using a Git software installation, we will call it Git Bash.

Make sure that Git installed correctly by checking your version using the command  git --version  in the command line. 

Now, the first step is to configure your credentials on Git Bash. Start by setting up Git’s config file so it has your name, and email address to identify you on the repository.  For that, you will type in the following commands in Git Bash:

$ git config --global user.name "Wonder Woman"
$ git config --global user.email ww@dccomics.com

Remember to fill in your name with the email address that you used for your GitHub account  (You can always change your credentials later).

Now the fun stuff!  You've created your first repository on GitHub, but you haven't created one on your computer. So now, let's create your first local repository:

  1. Create a folder for your repositories.  I like to put a folder on my Desktop called gitprojects  so all my repos are easy to access.

  2. We will be working with several local repositories, so create another folder in the gitprojects folder called myrepo.

  3. In Git Bash, use the command line to make myrepo your working directory.

  4. When myrepo is in your working directory, it should look like the photo below on Git Bash:

The path for myrepo on the Desktop
The path for myrepo on the desktop

5. Convert that myrepo folder in your directory into a local repository with the simplest command:  

git init

Congrats!  You have created your first local repository. 🎉

But it's not over yet. You may have a local repository, but you have to be able to communicate back and forth with the remote repository using Git. Let's configure that next.

Connect to your GitHub account remotely using SSH

Now let’s make sure that you connect your credentials (your username and password) on Git Bash to your GitHub account.  Do that by setting up an SSH key using the RSA encryption algorithm.

A whuh key with a whuh…? 😨

Okay, let me explain! SSH keys (secure shell keys) are used to unlock your encrypted GitHub credentials.  GitHub likes to keep your username and password safe. So, instead of logging in with those, Git Bash will automatically log you into your remote repository in GitHub using an SSH key that is stored in your Git credential file!  It's way easier because you won't have to keep plugging in your username and password, and it's encrypted! It sounds complicated, but it's a really easy command! 🙂

Generate an SSH key pair on Git Bash!
Generate an SSH key pair on Git Bash!

Just copy and paste the command below with your email address instead of mine (there can be only one Wonder Woman)! 😎

ssh-keygen -t rsa -b 4096 -C "ww@dccomics.com"

Allow the default to save the key.  This will generate two SSH keys.  One SSH key will be saved on your computer, and you will need to save the other in your GitHub account to authenticate your GitHub account and upload your changes to the remote repository.   

First, copy the SSH key to your clipboard.  Since it's pretty long, make sure that you got it all, so you have two options to copy over your SSH key. The first is  pbcopy. It is the easiest command to copy your SSH key; however, its a bit more tricky on a Windows machine.  Here is the command to copy the SSH key to your clipboard using  pbcopy.

pbcopy < ~/.ssh/id_rsa.pub

2. If you try the first option, and Git Bash says command not found, that means it does not recognize the command. In that case, you can use the CLI text editor cat to display the SSH key, and then highlight and right-click to copy it: 

cat < ~/.ssh/id_rsa.pub

The whole thing will look something like this:

Use pbcopy or cat to copy your SSH key for GitHub

Now log into your GitHub account. In the upper-right corner of any page, click your profile photo, then click Settings. In the user settings sidebar, click SSH and GPG keys.  Now, click New SSH key. 

Click on New SSH key
Click on New SSH key

Paste your key into the text field, and click on Add SSH key.

Add your SSH key to Github!
Add your SSH key to GitHub!

Now that you’ve completed this, remotely connecting to your GitHub account from your computer will be easy! In the next chapter, we'll look at an open source repository and fix an issue using Git.

Let’s recap!

  • Git is a decentralized system, using remote and local repositories. 

  • Local repositories offer the same functionalities as a remote repository, and allow you to update between the two.

  • Use the Git Bash interface to configure your local repository.  Git Bash is essentially the Linux command line interface on your computer.

  • You can create a local repository by typing git init in the directory for your repository.

  • SSH keys allow you to authenticate to your GitHub account without putting your username and password in.

Example of certificate of achievement
Example of certificate of achievement