How To: GitHub Basics

ReginaOfTech
10 min readJan 7, 2020

--

In April 2008, GitHub was born. The world of code was turned upside down with a revolutionary way of storing and sharing code. Founded by Tom Preston-Werner, Chris Wanstrath, P. J. Hyett, and Scott Chacon in San Francisco, California. It has grown to contain 100 million repositories with 28 million of those being public and being acquired by Microsoft for $7.5 billion US dollars in 2018.

https://giphy.com/gifs/statechamps-state-champs-3oz8xR9wKr8TaazlQc

It is commonly used by companies to store their code and it is beneficial to be comfortable with GitHub and its commands. Let’s dive in and understand the GitHub basics.

What is GitHub?

It is a hosting platform for software development version control. Git is the heart of GitHub, hence the name. It is a command-line tool that was originally written for Linux and then brought over to windows via GitBash. Git and GitBash are both project version managers that track, mark, and store changes in your project. It allows for multiple collaborators to be working on the same code and possibly on different features. If one of the collaborators pushes code that ends up adding a bug to the code, it can be reverted to the previous code easily.

What is a Repository and Why is it Important?

This is essentially where the code lives. When someone refers to a repository aka repo, think of a journal. It is specific to your code and each new entry is another version of the code that has been pushed/committed. You can flip through each page to see how your code has changed. If you encounter an error you can go back to the working version and “rip out” the pages of the buggy code. A repo is the home of your code.

Besides just making a controlled home for your code, it allows you to easily access your code and showcase your skills. There are two options with a repo: public and private. The public option allows anyone using a search engine to find your code. Private gives you the option to allow select eyes on your code via inviting them.

The public repo is a great option if you are using your code to grow your freelance business or allow a potential employer to see it. This is a great strategy if you are a new graduate or coming over to the tech sector from another. Showcase your skills and bring in a potential employer without much time commitment on their end. Piece of advice, do not copy and paste commonly used code and use that as your showpiece.

https://giphy.com/gifs/happy-endings-casey-wilson-penny-hartz-YjV9ujOqTs46I

Companies who are hiring may use AI programs to parse through resumes, but people still look at the code. Create your own without using the titanic data set or one that is commonly used. Find a data set that interests you and allows your imagination to flow free to show what you know. Make it interesting so that the reviewers want to keep their eyes in their heads or even put them back in.

How Do I Create a GitHub Repo?

Easy! First, you need an account. Go to https://github.com/ and fill out the sign-up form.

Once signed in, in the upper right corner, there are 3 symbols: bell, plus sign, and the user icon. Click on the plus sign then click on ‘New repository’ in the drop-down menu.

You will be brought to a page that looks like this and you will need to:

  1. Create a repository name
  2. Create an optional description (Recommended if it’s a public repo)
  3. Select if the repo is public or private
  4. Create a README with the repository (This is where you can explain why your code is useful and what it can be used for)
  5. Hit that button and create your repo!

How Do I Get the Code Up to GitHub?

You will need Git or GitBash. These are the command line consoles that interact with the underlying git layer that is what controls the pushing and pulling of the code from the repo. So, depending on which one you select, go to https://git-scm.com/downloads or https://gitforwindows.org/ and get git installed. I’ll be using the GitBash interface so I’ll be downloading from the second URL.

Click the Download button and the installation routine should auto-download. Once it is done, give that a click. After clicking yes for allowing GitBash access to your device, the installer should be up on your screen. Go through and set it up how you would like. I normally stick with the defaults but to each their own.

Git Flow

Now that the underlying Git layer is installed along with GitBash we are ready to push to the repo! Inside the GitBash console, you will need to change the working directory. This is where your scripts are living in your computer and any of its dependencies that it needs to function. This is a simple command and is:

cd [Path to Folder]

Simple! Just remember that cd means change directory. With that, the console line should change to where you need it. If it says that it could not find the folder use the other slash. If you used / then use \. I can never remember either which one it accepts, but it normally is the /.

Move to Staging Area

GitBash is now pointing to the working directory. The files that have changes are ready to be moved from the working directory to the staging area. Think of the staging area as a way for you to make sure that the proper files are being sent to the repo. Due to this type in the GitBash console:

git add .

To check what files are marked as changed use the line:

git status

Move to Local Repo

A couple more lines and then the code is in the repo! Along with having a cloud-based repo, we also have a local repo. This local repo is located on our device. This allows for any comments or last-minute changes to be made before it is sent to GitHub. Type:

git commit -m “Your message here”

The message that is between the “ “ will appear in the repository on the folders/files that you make changes to. This is another means of communication between the programmer and the collaborators on the project.

Move to GitHub

Next, we need to let Git know where the heck it is sending the code! If this is a brand new repo, the HTTPS that you need will be shown where the code should be. If there is code already in the repo, click on the green ‘Clone or download’ button and copy the URL in that window.

Bring the GitBash window back to the front and type:

git remote add origin [Repo HTTPS]

Once you are a pro and have multiple repositories you can view what you have activated with:

git remote -v

Git now has the repo as a target so let's push the code up to your repo. To do that type in:

git push -u origin master

You should see the console showing progress on copying the code to the repository. Once it is completed, refresh your GitHub repo and the code should be there.

Drag and Drop Method

If this is a brand new repo there is an option to drag and drop your files into GitHub. All you need to do is go to your blank repo, open file explorer, navigate to your files and bring them over. This is especially handy if there are only 2 files and you do not have the urge to work with a command console.

How Can I Pull from Someone Else’s Repo?

With one (well two) lines of code. Move the working directory that Git is pointing to where you would like the code to be stored.

cd [Working Directory]

Now you are ready to clone the code. In the desired repository, click on the ‘Clone or download’ green button and copy the HTTPS. Head back into Git and type:

git clone [Repo HTTPS]

This will download the repo to your computer into the working directory. Cloning a repo does not automatically make you a collaborator on that project. Some you will need to be added while others are open source and you can push to your heart's content.

If you are using code from another programmer’s repository be sure to give credit where credit is due. There is enough room at the top for everyone.

Source: https://giphy.com/gifs/help-coarse-each-other-Zap11yFJt57ckXJ2fo

How Can I Make It Easier For Someone To Give Me Credit For The Code?

There are several ways for someone to give credit for code. They can use the username, URL to the code, or a Digital Object Identifiers (DOI). If you are a researcher creating code then creating a DOI would be beneficial so that the repo can be included in a publication. Go to https://guides.github.com/activities/citable-code/ to learn how to create a DOI. It is an easy process and it is explained in a GitHub guide.

Is The Read Me Necessary?

The answer truly depends on who you ask. In my opinion, no it is not necessary. If you are throwing code to be stored and not planning on other people utilizing the code then you do not need one. If you are using the code as an open-source or a starter code for other people then yes include it.

The read me is essentially a guide to your code, what it can be used for, and how to grow upon it, along with credits and how to reach the programmers easily for questions.

Is GitHub Storing Code In The Artic?

https://giphy.com/gifs/daddyshome-yes-snow-3o7aCVwrXFrLmFSdLa

It is called the GitHub Archive Program. The goal is “preserving open-source software for future generations”. Similar to Norway’s Doomsday Seed Vault, but for code! Don’t believe me? Check out https://archiveprogram.github.com/. GitHub will take a snapshot of all active public repositories on 2/2/2020 and those will be stored for up to 1,000 years in their Arctic Code Vault.

There’s a little incentive to get the coding project done that you have been postponing! 😉

Other Useful Commands

Pulling From Your Repo

The point of having a repo is to have a place to store changing code. So having the ability to pull changes to your working directory is important.

To do that, change the working directory of GitBash to where your code lives on your computer.

cd [Working Directory]

Next, you will retrieve the changed code from the repo. This can be done in 2 ways. One that automatically takes the code from the repo to the working directory and the other that brings it to the local repo and then to the working directory.

The first option is the easiest but does not allow you to view what is being imported. The call is:

git pull [Remote Repo]

The second option has 2 calls:

git fetch [Remote Repo]

which copies the code from the repo into the local repository. Then the second call:

git merge

brings together the local repo and the working directory.

Fetch and merge equals git pull, but it does not allow you to see what is going to be brought into your code. That is truly just a preference on your part.

Login To GitHub via Git

To push to a repo, GitHub needs to know who to associate with the changes. Git will need to know who you are and if you have been permitted to commit to the repo.

When you do your first push, you will be asked to log in using your GitHub credentials. Once you do that, it will (normally) store that information so that you do not need to provide it every time. If you have multiple GitHub logins then this can get complex.

To see what credentials are being used in Git type in:

git config -global -list

This will bring up the email and name of who is being stored. To change this you can use:

git config -global user.name “Your Name”

and

git config -global user.email “Your email”

If that does not work, then you will need to sign back into GitHub through the console. To do that you will need to get rid of the token that is stored on your computer.

Open the Control Panel and click on User Accounts. You should see a window similar to this:

Click on Manage Windows Credentials and find the GitHub token. Click on the arrow to the right of the row and select remove at the bottom of the expanded window. When you push to the repo you should be requested to log back into GitHub.

Thank you for reading! If you are interested in some code to push to your first repository, check out How To Code A Fair Coin Flip In Python.

Until we learn again,

Originally published at https://reginaoftech.com on January 7, 2020.

--

--