Module 6: What is Git/GitHub?

This isn't going to be your typical classroom where you sit and listen to boring lectures all day. You'll be learning in new and exciting ways. In this prework, we may employ some learning techniques that are foreign to you, but trust us that these are proven educational strategies that will transform the way you learn forever.

In this chapter we're going to use a tool that we call 1 in 3. We'll explain 1 concept in 3 ways. While it may feel redundant, each section becomes progressively more technical. It's a great way to get a broad understanding of a new field or concept.

This chapter is all about developing a deeper understanding of Git for version control.

Why spend time on this? We need our students understand the basics of how Git works in order to work collaboratively with colleagues and communicate effectively across technical teams at their organizations. This is a high-level overview aimed at introducing common vocabulary and concepts relevant to the technology.

Part 1: Basic Explanation

Git and GitHub are tools that help people collaborate and safely store their code.

People need a place to safely store their code. Imagine you kept all your projects on your computer and your computer was stolen. You could lose months of work. Just like Dropbox is used to back up documents, GitHub is an online space to store code. GitHub also allows you to share code with other people and work together on projects.

Git is a software application that runs on your machine and allows you to add things to GitHub and download things from GitHub.

Part 2: Foundational Knowledge

What is Git?

Git is a version control software that runs on your computer.

Git serves two primary purposes:

  1. It tracks changes to any files in your project. If you have a working application and you accidentally break it, you can roll it back to a previous point. Unlike Microsoft Word or similar programs, not only can you go back to the previously saved point, but you can go back to any point in time that you saved.

  2. It allows you to add (push) and download (pull) code from your online code repository on GitHub. This functionality is similar to the Dropbox client that syncs your local files with Dropbox.

Real World Example:

Imagine you're playing a video game and there's a key you can press to save at any time you want. Perhaps you saved twice in level 1, once in level 2, and once in level 3. The game is going well, but suddenly it takes a turn for the worst. Your health is waning, your energy is low, and you're down to your last life. Not to worry, you can go back to any of your save points!

The save point from level 3 seems like the obvious choice because you were the furthest along in the game at that point. You pop back to the level 3 saved version and bam! Your game is just as you it in level 3. Health is great, energy is high, and you have lives to spare. Life is good...

But before too long you end up back in the same predicament and it seems like maybe the bad decisions that lead actually occurred long before level 3. You pop back to your second saved version on level 1 and whiz through the game with ease!

This video game example is a scenario that happens in software development frequently. Suppose you are building an event management software for a local event planner. They ask you to add a calendar widget so clients can see available dates. You're working on this over a few days, it's going great, but you code yourself into a corner and break the entire application trying to figure it out. Not to worry! If you used git and saved (pushed) frequently enough, you can roll back to exactly the point where you took a wrong turn.

What is GitHub?

GitHub is a web-based storage solution for developers. It's like the developer's Dropbox or Google Drive. You can store code for safe keeping and share it with others. But it's so much more than that! It's also a version control system.

Suppose you are managing a team of 5 developers and they are all working on a project. Quality is dropping and more bugs are slipping into production. You find the offending files.

Want to know who wrote them?

GitHub can tell you that.

Want to know who reviewed them and said they were okay?

GitHub has that info too.

How about rolling back to a previous version when things were more stable?

Easy Peesy.

GitHub also helps developers collaborate. Let's take a look at the example below.

Real World Example:

Janet and Tasha are working together on a login feature. The design files call for two shades of green and a shade of yellow to be used on the site. Janet is working on the login box and adds a green background. Tasha is working on the same feature and adds a purple background. They aren't using GitHub and Janet uploads her files first, followed closely by Tasha. What color is the background? It's purple. This is definitely not right, but whoever pushes last wins in the wild world of the GitHub-less.

Let's take a look at the same scenario with GitHub:

Janet pushes her code and it's merged into the production code. Tasha pushes her code, but GitHub blocks it from being merged. There's a conflict! Someone on the team has to review the code, check the conflict, and choose which code is correct. Their teammate Alysha reviews the code, notices that the purple doesn't match the design files and tells GitHub to use the green background before merging the rest of Tasha's code.

Tada! Cloud storage, version control, and source code management all in one!

Part 3: Technical Explanation

What is Git?

Git is the most widely used version control system. Version control is software that helps a team manage changes to its source code. Git was created originally in 2005 to manage the Linux kernel. Nowadays it is used across the world for an unfathomably huge number of software projects.

Git tracks your changes to the source code via commits, and syncs with a remote source, like GitHub or Bitbucket, via push (to send your commits to the remote) or pull (to retrieve commits from the remote) commands.

What is GitHub?

GitHub is an online space to work collaboratively on projects and keep your code safe.

Most software requires teams of developers to work simultaneously in a safe way. GitHub is a tool that allows us to do that. It is a powerful tool on which all developers rely heavily.

Virtually every software company stores its code in GitHub or a similar service.

How do I use Git and GitHub?

This handy video can walk you through the below steps:

  1. Create a new repository
  2. Copy the link
  3. In terminal, cd into the directory where you want your repo to live
  4. Clone the repo using git clone <repo name>
  5. cd into the repo
  6. Add a file
  7. Stage the changes in your current working directory using git add .
  8. Commit the changes using git commit -m "some message"
  9. Push the changes using git push origin master

This will push your changes directly into the master branch. The master is the default branch of your repository, and as its name suggests, is the master source for your project. As you might have guessed, we do not want to push directly to our master branch, and in fact, when working on a team, you likely will not have access to do so.

To get around this, we use a workflow called Feature Branching. The main idea behind feature branching is that all feature development should take place in a dedicated branch rather than pushing directly to the master branch.

Here's how you can work in this kind of environment:

  1. Create a new branch.

  2. Then, all changes to our repository (additions, modifications, or deletion) are made inside this branch.

  3. We then commit our changes, and push them to the remote source (GitHub).

  4. Once the branch has been pushed to the remote, we open a Pull Request.

  5. Once the Pull Request has been made, there will very often be automated tests that run to ensure that your modifications to not break the working application.

  6. Pull Requests also initiate a discussion about the changes you've committed, and generally have to be approved by someone else on the team. That person (or entire team) will review your changes to make sure that everything is in order.

  7. Once any concerns have been addressed and the Pull Request has been approved, the modified code is merged into the master branch.

A great benefit of this process is that, once merged, Pull Requests act as a record of changes to the codebase, just like a save file!

Feature Branches

To create a new branch:

  1. cd into the repo directory
  2. Create and switch to a new branch using git checkout -b new-branch-name
  3. Make the changes to your repository that you desire
  4. Stage the changes using git add -A
  5. Commit the changes using git commit -m "some message"
  6. Push the changes using git push origin new-branch-name
  7. Navigate to your GitHub repository in your browser
  8. On the Pull requests tab, create a new pull request

To switch between existing branches, use git checkout branch-name. Note that you do not need the -b flag in your git checkout command this time because we are not creating a new branch.

To merge your changes into master, and update your local repository with those changes:

  1. On GitHub, merge the Pull Request you created.
  2. On your computer, switch back to your master branch using git checkout master
  3. Pull the updates from the remote server with git pull