Chapter 4 - What is Git/GitHub?
Remember how we made you read a whole page explaining the internet in 3 levels of complexity? Get ready for your next 1 in 3 (or should I say Git ready).
Part 1: Explaining it to a Child
Git and GitHub are tools that help developers collaborate and safely store their code.
Developers need a place to safely store their code. Imagine you kept all of 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: Explaining it to an Adult
What is Git?
Git is a version control software that runs on your computer.
Git serves two primary purposes:
-
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 previous saved point, but you can go back to any point in time that you saved.
-
It allows you to add (called
push
in Git) and download (calledpull
in Git) 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 left it in level 3. Health is great, energy is high, and you have lives to spare. Life is good...
But before long you end up back in the same predicament and it seems like maybe the bad decisions that lead you to this point 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've 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, not to be confused with just Git, 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: Explaining it to a Future Developer
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:
- Create a new repository
- Copy the link
- In terminal,
cd
into the directory where you want your repo to live - Clone the repo using
git clone <repo name>
cd
into the repo- Add a file
- Stage the changes using
git add -A
- Commit the changes using
git commit -m "some message"
- 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:
-
Create a new branch.
-
Then, all changes to our repository (additions, modifications, or deletion) are made inside this branch.
-
We then commit our changes, and push them to the remote source (GitHub).
-
Once the branch has been pushed to the remote, we open a Pull Request.
-
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.
-
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.
-
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!
To create a new branch:
cd
into the repo directory- Create and switch to a new branch using
git checkout -b new-branch-name
- Make the changes to your repository that you desire
- Stage the changes using
git add -A
- Commit the changes using
git commit -m "some message"
- Push the changes using
git push origin new-branch-name
- Navigate to your GitHub repository in your browser
- On the
Pull requests
tab, create a new pull request
To switch between existing branches, you do not need the -b
flag in your git checkout
command.
To merge your changes into master, and update your local repository with those changes:
- On GitHub, merge the Pull Request you created.
- On your computer, switch back to your master branch using
git checkout master
- Pull the updates from the remote server with
git pull
Assignment (Required)
This assignment will largely include the content from this chapter as well as the previous chapter. This means that if you did not complete the previous chapter, go back and do so first!
Instructions
-
Start by creating a new repository in GitHub. You can call this repository, "GitHub Prework".
-
After creating the new repository, clone it down locally.
-
Using the terminal or GitBash, navigate to the local version of the repository.
-
Create a new branch named, "prework/new" and change into it so that you do not push changes directly into the master branch.
-
Create a new folder named, "assets". Within "assets", create the following files:
-
README.md
- index.html
-
index.js
-
Add, Commit, and Push changes to the GitHub repository. Remember, you are pushing changes to your newly created branch.
-
Navigate to the repository on GitHub and create a Pull Request and merge the changes into the Master branch.
Submission
To submit this assignment, you'll be submitting a link to your newly created GitHub repository in Bootcampspot! Be sure to submit this link under the assignment, "Pre-work: GitHub Repository" in Bootcamptspot.
If you have any issues submitting your prework, check out this video for step-by-step instructions on how to do so! Prework Submission Video