Maker.io main logo

Beginners Guide to Github

2016-06-02 | By Maker.io Staff

Beginners Guide to Github

If you’re an avid maker or hobbyist then at some point along your development, you would have seen references to GitHub such as fork, merge, clone, master, pull and commit. When I first started out coding I didn’t have a Scooby doo what any of this meant so I asked a software programmer to explain it to me and after this it left me even more confused. However, I still knew the importance of learning and understanding how it all worked and this could make my working methodology more efficient in this modern day. For me GitHub does one if not more of the things below:

  • Collaboration
  • Systematic collection of Code
  • Share open-source
  • Efficiency
  • Social Networking

Having started as a developer’s collaborative platform, GitHub is now the largest online storage space of collaborative works that exists in the world. Whether you’re interested in participating in this global mindset or in researching this massive file dump of human coding knowledge, you need to be here.

Simply by being a member, you can brush elbows with the likes of Google and Facebook. Before GitHub existed, major companies created their knowledge mainly in private. But when you access their GitHub accounts, you’re free to download, study, and build upon anything they add to the network encouraging an open-source community.

What you might not know is that there are plenty of reasons to use GitHub if you’re not a programmer. According to GitHub’s educational videos, any knowledge worker can benefit, with “knowledge worker” defined as most any profession that makes use of a computer.

If you’ve given up on understanding how to use GitHub, this article is for you.

The main perception about GitHub is that it’s a development tool for programmers. However, GitHub itself isn’t much more than a social network like Facebook or twitter. You build a profile, upload projects to share and connect with other users by “following” their accounts. And while many users store programs and code projects, there’s nothing preventing you from keeping text documents or other file types in your project folders to show off.

github-profile-account-fig1

Figure 1: GitHub Profile Account

You may already have other social media accounts, but here’s why you should be on GitHub anyway: it’s got the best Terms of Service agreement compared to others. If you check out the terms, you’ll see that GitHub ensures that you retain total ownership of any projects you upload to the site:

“We claim no intellectual property rights over the material you provide to the Service. Your profile and materials uploaded remain yours.”

You can actually use GitHub without knowing ANY programming what so ever. You don’t really need a tutorial to sign up and click around, but I do think that there’s merit to learning things the hard way first. After all, GitHub just happens to be one of the most effortless graphical interfaces for the Git programming language.

Brief History of Git

Software developer Linus Torvalds came up with Git, the software that runs at the heart of GitHub. Git is a version control software, which means it manages changes to a project without overwriting any part of that project.

Say you and a coworker are both updating pages on the same website. You make your changes, save them, and upload them back to the website. So far, so good. The problem comes when your coworker is working on the same page as you at the same time. One of you is about to have your work overwritten and erased, this was a very common problem especially if your work that you spent hours/days on has been overwritten.

A version control application like Git keeps this issue from happening. You and your coworker can each upload your revisions to the same page, and Git will save two copies. Later, you can merge your changes together without losing any work along the way. You can even revert to an earlier version at any time, because Git keeps a “snapshot” of every change ever made.

The problem with Git is that it’s so old that we have to use the command line in order to access it, typing in snippets of code like retro hackers. This can be a difficult and time consuming for modern computer users. That’s where GitHub comes in.

GitHub makes Git easier to use in two ways. First, if you download the GitHub software to your computer, it provides a GUI to help you manage your version-controlled projects locally. Second, creating an account on GitHub.com brings your projects to the Web and also ties in social network features.

You can browse other GitHub users’ projects, and even download copies for yourself to use for your own local projects. Other users can do the same with your public projects, and even spot errors and suggest fixes. Either way, no data is lost because Git saves a “snapshot” of every change.

While it’s possible to use GitHub without learning Git, there’s a big difference between using and understanding. Before I figured out Git I could use GitHub, but I didn’t really understand why. In this tutorial, we’re going to learn to use Git on the command line.

Common References to Git

Below is a list of common phrases when people talk about Git, some you have probably heard of and some not. It is important to understand which each one does to learn more about how you can use Git.

Command Line: The computer program we use to input Git commands, usually this is referred to as a terminal. On a Windows PC, it’s a non-native program that you download when you download Git for the first time. In both instances, you type text-based commands into the screen.

Repository: Also known as a “repo”, it is a file or folder where you store your projects. It can be a local folder on your computer, or it can be cloud storage space on GitHub. You can keep code files, text files, image files, you name it, inside a repository.

Version Control: With Git it keeps “snapshots” of every point in time in the project’s history, so you can never lose or overwrite it. It’s almost the same as when you create a backup of your computer, you create a version of that time.

Commit: This is the command that gives Git its power. When you commit, you are taking a “snapshot” of your repository at that point in time, giving you a checkpoint to which you can restore your project to any previous state.

Branch: How do multiple people work on a project at the same time without Git getting them confused? Usually, they “branch off” of the main project with their own versions full of changes they themselves have made. After they’re done, it’s time to “merge” that branch back with the “master,” the main directory of the project.

Git Commands

Git was originally designed with large projects in mind, there are a lot of Git commands that were created. However, to use the basics of Git you’ll only need to know a few commands. They all begin with the word “git.”

git init: Initializes a new Git repository. Until you run this command inside a repository or directory, it’s just a regular folder. Only after you input this does it accept further Git commands.

git config: Short for “configure,” this is most useful when you’re setting up Git for the first time.

git help: Most useful command for when you get stuck

Type this into the command line to bring up the most common git commands. You can also be more specific and type “git help init” or another term to figure out how to use and configure a specific git command.

git status: Check the status of your repository

See which files are inside it, which changes still need to be committed, and which branch of the repository you’re currently working on.

git add: This does not add new files to your repository. Instead, it brings new files to Git’s attention. After you add files, they’re included in Git’s “snapshots” of the repository.

git commit: Git’s most important command. After you make any sort of change, you input this in order to take a “snapshot” of the repository. Usually it goes git commit -m “Message here.” The -m indicates that the following section of the command should be read as a message.

git branch: This command will let you build a new branch, or timeline of commits, of changes and file additions that are completely your own.

git checkout: This is a navigational command that lets you move to the repository you want to check. You can use this command as git checkout master to look at the master branch.

git merge: When you’re done working on a branch, you can merge your changes back to the master branch, which is visible to all other users.

git push: If you’re working on your local computer and want your commits to be visible online on GitHub as well, you “push” the changes up to GitHub with this command.

git pull: If you’re working on your local computer and want the most up-to-date version of your repository to work with, you “pull” the changes down from GitHub with this command.

How to Setup GitHub

Beginners Guide to Github Figure 2

Figure 2: GitHub Home Page

First you will need to sign up for an account on GitHub.com with your details. Keep the email address you used handy; we’ll be needing it again soon.

If you want to work on your project on your local computer, you need to have Git installed. In fact, GitHub won’t work on your local computer if you don’t install Git. Install Git for Windows, Mac or Linux as needed, Figure 3.

Beginners Guide to Github Figure 3

Figure 3: Download and install Git for your operating system

Now let’s open up a command line prompt or terminal window. On Windows that means starting the Git Bash app you just installed and on OS X it’s just a matter of opening up a regular Terminal window. Type in the following code:
git config --global user.name "Your Name Here"

Replace “Your Name Here” with your own name in quotations. It can be your legal name, your online handle or anything else. It just needs to know who to credit commits and future projects.

Next tell git your email address and make sure it’s the same email you used when you signed up for a GitHub.com account. Type in the following:
git config --global user.email "your_email@youremail.com"

That’s all you need to do to get started using Git on your computer. However, since you did set up a GitHub.com account, it’s likely you don’t just want to manage your project locally, but also online. If you want, you can also set up Git so it doesn’t ask you to log in to your GitHub.com account every time you want to talk to it.

Creating a Repository

Now that everything is set up, it’s time to create a place to store your project. Both Git and GitHub refer to this as a repository or “repo” for short, a storage space where you can access your project, its files, and all the versions of its files that Git saves.

Go to GitHub.com and click the tiny book icon next to your username. Give your repository a short, memorable name, remember you might have to type this name in Git at some point so keeping it short and sweet is useful. You can at this point make it public if you wish, nothing gained at this point.

github-profile-account-fig4

Figure 4: Creating a New Repository

Don’t worry about clicking the checkbox next to “Initialize this repository with a README.” A Readme file is usually a text file that explains a bit about the project. Click the green “Create Repository” button and this will create the repo for your project.

Creating Your Local Repository

Now we have created a repository for where you project files will live however chances are this is not where you will be working on the project. Most of your work is going to be done on your computer. So now we need to make a copy of that repository we just made as a local directory on your computer.

In the command line on your computer type:
mkdir ~/MyProject

This will create a new folder in the top level hierarchy of your computer, which is usually the user’s folder. The command is not a Git command but it is short for make directory, you must also make sure the directory you are creating is exactly the same as the repository you created earlier.

Next, type:
cd ~/MyProject

cd stands for change directory, and is a navigational command. We just made a directory, and now we want to switch over to that directory and go inside it. Once we type this command, we are transported inside the directory.

Now we’re finally using a Git command. For your next line, type:
git init

You know you’re using a Git command because it always begins with git. When we type this command in, it tells the computer to recognize this directory as a local Git repository. If you open up the folder, it won’t look any different because this new Git directory is a hidden file inside the dedicated repository. Your computer now realizes this directory is Git-ready, and you can start inputting Git commands. Now we have setup both an online and a local repo for your project.

Now that these steps have been completed, let’s add the first part of your project by making your first commit to GitHub.

On your next line, type:
touch Readme.txt

This, again, is not a Git command. It’s another standard command prompt. touch means “create.” Whatever you write after that is the name of the thing created. If you go to your folder using Finder or the Start menu, you’ll see an empty Readme.txt file is now inside.

You can clearly see your new Readme file. Type: git status

This command will confirm that the file has been created in the local repository and should spit out some lines confirming this. The Readme.txt file now gets listed as an untracked file, which means that Git is ignoring it for now. For Git to recognize the file is there, type
git add Readme.txt

Now that you have added your first file, it’s time to take a “snapshot” of the project so far and “commit” to it, type:
git commit -m “Add Readme.txt”

Connect Your Local Repository To Your GitHub Repository

Having a local repository as well as a online repository is the best of both worlds. You can tinker all you like without even being connected to the Internet and at the same time showcase your finished work on GitHub.

This setup also makes it easy to have multiple users working on the same project. Each of you can work alone on your own computers, but upload or “push” your changes up to the GitHub repository when they’re ready.

First, you need to tell Git that an online repository actually exists somewhere online. We do this by adding it to Git’s knowledge. Just like Git didn’t acknowledge our files until we used the git add command, it won’t acknowledge your remote repo yet either.

Assume that you have a GitHub repo called “MyProject” located at https://github.com/myusername/myproject.git. Of course, myusername should be replaced with whatever your GitHub username is, and myproject should be replaced with the actual title you named your first GitHub repository.
git remote add origin https://github.com/username/myproject.git

The first part is familiar; we’ve used git add already with files. We’ve added word origin onto it to indicate a new place from which files will originate. remote is a descriptor of origin, to indicate the origin is not on the computer, but somewhere online.

Git now knows there’s a remote repository and it’s where you want your local repository changes to go. To confirm, type this to check:
git remote -v

This command gives you a list of all the remote origins your local repository knows about. It’s listed twice, which means it is available to push information to, and to fetch information from.

Now we want to upload, or “push,” your changes up to the GitHub online repo. Just type:
git push

If I wanted to be more specific, I could have typed git push origin master, to specify that I meant the master branch of my repository. This would only be suitable if there was multiple branches.

Log into GitHub again. You’ll notice that GitHub is now tracking how many commits you’ve made. If you’ve just been following this tutorial, that should be exactly one. Click on your repository, and it will have an identical Readme.txt file as we earlier built into your local repository.

Summary

Hopefully now you should have the basics of how Git and GitHub works. You should be able to create a new repository both locally and online and commit your working files, whilst sharing your projects with others and encouraging collaboration between users.

For makers you can use Git amongst a number of development boards which are Linux based such as Raspberry Pi, BeagleBone, Intel Galileo and many more. It is useful for replicating someone else’s project they have created on your device and then trying to make it better or adapt it for your own project needs.

TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum