Using Git and GitHub can be a little intimidating at first. But worry no more; here are the tips, tricks, and analogies that will help you get started with Git and GitHub.
In this blog, you’ll learn the absolute essentials of Git and GitHub:
What is version control about, and how are Git and GitHub involved?
What is a “repo”?
What does “add” mean?
What is a “commit”?
Why do we “push”?
These are just the very essentials you need to get started. By the end of this blog you’ll have the tools you need to work on a solo project using Git and GitHub. You’ll also have the skills to get started on this Cloud Skills Challenge: “Get Learning with Git – Git skills and GitHub tools”.
For more concepts after you have these basics, I’ll be creating a new blog soon “Get Collaborative with Git” for how to use Git when working in teams. (For now, you can watch this video)
I’ll cover the points in the blog below, but you can also get them in video form here:
1. What is version control about?
You’ve probably heard about Git and GitHub when discussing version control. But why is version control so important?
In short, version control is the idea that we want to keep track of how our code is changing over time and have ways to get back to previous versions of our code. (ideal if a bug appears in your program and you want to figure out where it came from)
There are lots of different implementations of version control. Git is one implementation of version control and is the most popular and widely used option. It has features that allow you to have multiple people collaborating on the same code, ways to integrate conflicting changes different coders make as they work together, and many other handy things.
GitHub is a cloud-based platform that runs on the Git protocol. It is a way of using all the handy features of Git, whilst also backing up your code on the cloud and making it accessible to the rest of your team. There are LOTS of other features GitHub gives you (like options for testing your code, integrating with deployment systems, and more), but you don’t need to know about any of that to get started with.
2. What is a “Repo”?
Repo is short for Repository. This is essentially your project.
This project or repo will have all the files that you and your team create. It will also have the version history of all the changes that have been committed to the project along the way.
You can manage who has access to your repo, review changes your team has made, and set up automations for testing and deployments (once you get past the basics).
It’s easy to create a new repo on GitHub once you have an account. You can just click the “new” button or the “+” button on the home page, and the give your repo a name and add any other details.
Time for an Analogy
To answer questions 3, 4, and 5, an analogy will help us understand.
Git commands get a lot easier when we think about it like a shopping trip.
Let’s say we are shopping for a new outfit. In our example we:
SHOP First you are browsing the shop. You can put things into your basket whenever you like. You can also take them out.
This is our Code step. It's our normal coding process, it does not involve Git commands. You are just typing in code, removing code, and saving code locally on your computer (or web editor).
GO TO THE REGISTER When you’re shopping and you are happy with the items in your basket, you will go to the cash register and put your items on the conveyer belt to get scanned by the shop attendant. At this time, you still haven’t bought the items, you could take them off the conveyer belt at the last minute or ask the shop attendant to remove any scanned items before you make your purchase.
This is our Git add step. When you are happy with the code you have created, you’ll want to lock it in (and get it safely stored on the cloud in a minute). At this point you will “stage” your changes. To stage your changes you “add” them.
You can either do this in the VS Code Version Control extension by clicking the + icon next to the different files.
You can also do this from the command line with the following command:
git add <your_file_name>
You can include multiple files, but don’t have to add all of your changes at the same time. You might want to put them through in separate transactions (just like at the shops if you want items of different receipts. Eg: For personal expenses versus work expenses)
It's good to commit often and not to save all of your changes up for one big commit at the end. Once committed, we’ll be able to store code on the cloud with GitHub to keep it safe. It also allows team members to access your work so they can use it or add to it.
PURCHASE Once all your items have been scanned by the shop attendant and you’re happy with them all, you’re ready to make your purchase. You pay for your items, and you get a receipt with all of the details of your transaction. If you do change your mind on your purchase, don’t worry you have that receipt and can return them.
This is just like the Commit step when using Git. Once we have added all of the files we want in this commit we then commit our code to the repo. This creates a record (called “a commit”) of all the changes we have just made, making it part of the official log for the project. (it does this in the form of a “diff”, which stores a record of which lines of code were added, edited, or removed).
You can either do this in the VS Code Version Control extension by clicking the “Commit” button. Or you can also do this from the command line with the following command:
git commit -m “Write the note for your commit here”
We can undo this commit if we need, just like returning items to the shop. But this “returning” process gets a bit more tricky once we are working in a team, which we’ll discuss in the next step.
POST ON INSTAGRAM Our shopping trip might involve several shops and different purchases, so we might wait to post pics on Instagram until we’ve finished our whole shopping spree. We’ll post a picture once we’ve got all the items for our new outfit and post that all at once to show off.
But be careful, if you’re not happy with your outfit, things get more tricky once you’ve shared it with the world. Things on the public internet can be hard to take back!
This is the Push step of our Git process. Pushing our code gets it safely stored on the cloud, yay! When working alone, you can be free to push your code as often as you like (it’s good to push often so it’s safely backed up).
You can either do this in the VS Code Version Control extension by clicking the “Commit & Push” button or “Commit & Sync” button in the drop-down next to the Commit button. (By selecting the sync option, you also get any new changes other teammates have added to the repo synced to your computer)
You can also do this from the command line with the following command:
If you are working in a team and pushing to the same “branch” in your repo, be careful. It’s hard to undo changes once other people have already based their own code on your work.
To learn how to get the benefit of pushing your code to the cloud, while working well in a team, make sure you check out the next blog. Get Collaborative with Git. Here you’ll learn about “branches”, “merging”,“pull requests”, and more!
So, to answer our last 3 questions...
3. What does “add” mean?
“Add” means to get our changes ready to be added to the log by noting which files (or parts of files) are staged for the upcoming commit.
4. What is a “commit”?
“Commit” is both the action of committing the changes you have just staged, as well as a “Commit” being an item in the log with the changes that were added.
When you are ready to add your code to the log you “commit it”. When you want to review changes that happened at one point in the log you may look at a specific commit.
5. Why do we “Push”?
“Push” is when you put the commits that you have made locally on your computer onto the cloud version of your repository.
If you’ve liked learning this, make sure to check out my other Git blogs and videos: