Getting Started with Git and GitHub Desktop
In this article, we will learn how to better manage our code using a version control system. We will cover:
- What a version control system is and why it is important.
- The basics of Git, one of the most popular version control system for coders.
- The difference between Git and GitHub.
- How to interact with Git using GitHub Desktop.
Let’s get started!
As a programmer, you’ll often find yourself working with many different types of files as well as collaborating with others on projects.
For example, imagine you and your team are working on a machine learning algorithm aimed at teaching a car to drive autonomously.
As you and your team collaborate on many different files, things can get pretty messy if you aren’t on the same page about the current state of the project. A version control system (VCS) can help prevent this issue.
A version control system helps manage your files and track the history of changes in your projects. In simple terms, you can think of it as a Google Doc – for coders.
A version control system makes it clear which changes to the project are made, who made those changes, and when those changes occurred. Version control also makes it easy to rewind to a previous version of your project if, for example, you discover a bug in your code and want to revert to a past version.
Git is a widely-used version control system used to manage code. Nearly 70% of developers use Git, according to a Stack Overflow survey.
Git allows you to save drafts of your code so that you can look back at previous versions and potentially undo complicated errors. A project managed with Git is called a repository.
A repository contains all of the files and folders associated with your project. It also includes the revision history of each file. The file history is a series of snapshots in time, known as commits. A commit tells Git that you made some changes which you want to record.
When you make a commit in Git you will see “commit to main.” This is referring to the default branch, which can be thought of as the production-ready version of your project.
Note: In the past GitHub used the term master, but decided to rename the default branch to main. You can read more about this change here.
Interacting with Git
Many people get intimidated by Git at first because they imagine interacting with it might look something like this:
However, today there are ease-of-use interfaces that make it easy to interact with Git without using the command line.
Using such interfaces helps simplify your development workflow and allows you to focus on your coding instead of spending your time figuring out all of the intricacies of Git.
The interface we will be working with is called GitHub Desktop.
What is GitHub?
Git and GitHub are the same thing…right? Not exactly.
GitHub is a popular hosting service for Git repositories. GitHub allows you to store your local Git repositories in the cloud. With GitHub, you can back up your personal files, share your code, and collaborate with others.
In short, GitHub is a tool for working with Git. There are other services to host Git repositories, but GitHub is a trusted, free service used by organizations across the world, big and small.
To use GitHub and GitHub Desktop, you will need a GitHub account.
If you already have a GitHub account, continue to the next section (GitHub Desktop). Otherwise, follow these steps:
In your browser:
Open a new browser tab.
Navigate to github.com
Create an account.
After you sign up, you will receive a verification email. Be sure to verify your email address to GitHub by following the instructions in that email.
Once you have your GitHub account set up, you are ready to install GitHub Desktop.
In your browser:
Open a new browser tab.
Navigate to desktop.github.com
Download GitHub Desktop.
Once installed, open the GitHub Desktop application. You should see a screen that looks like this:
Click “Sign in to GitHub.com” and sign in using your GitHub username (or email associated with your GitHub account) and password. You should now see this:
Here you should enter the name and email for your Git account – this information is used to identify the commits you create. You can use your GitHub username for your name and the email associated with your GitHub account for your email. Click “Continue” once you complete this step.
Click “Finish” on the next screen.
You’re now ready to start using GitHub Desktop!
Create a Repository in GitHub Desktop
If you followed the steps correctly so far, you should see the following screen:
To create a new repository in GitHub Desktop, start by clicking “Create a New Repository on your Hard Drive…”
Let’s name our repository codecademy-git-test.
For the local path, try to choose an easily accessible location, such as your Desktop. Also, make sure to check “Initialize this repository with a README” since we will be modifying the README file in this tutorial. Your screen should look like this:
Once you’re ready, click “Create Repository.”
Congratulations! You just learned how to create a new Git repository using GitHub Desktop. You should now see this:
On the top bar, notice that our current repository, codecademy-git-test, is shown. Next to that, our current branch, the default branch, is shown. This repository is currently available only on our local machine. To publish it to GitHub, let’s click “Publish repository.”
Now when you navigate to your GitHub profile on github.com, you should see your newly created repository listed there:
Commit in GitHub Desktop
Now that you know how to create a repository in GitHub Desktop, let’s talk about commits. A commit tells Git that you made some changes to files in your repository and you want to record those changes.
To make our first commit, we first need to modify one of the files in our repository.
Navigate to the codecademy-git-test folder on your Desktop (or wherever you chose to locate it). You’ll notice that the only file contained in this folder is a README.md file.
Open this file with your preferred text or code editor and write a personalized message. For instance, we can write the following:
Once you have finished your edits, save the README file and return to the GitHub Desktop application. You’ll notice that GitHub Desktop has already recognized the changes made to your file.
To commit these changes, we should first write a commit message on the bottom left of the screen. In this example, “Update README.md” is an appropriate commit message.
When we’re ready to commit, we can simply click “Commit to main.”
Congratulations! You just learned how to perform a Git commit using GitHub Desktop.
Your commit is currently local to your computer. If you want the changes to the README file to show on GitHub, you must click “Push origin” on the next screen:
Now when you navigate to your codecademy-git-test repository on github.com, you should see your updated README file:
Fetch in GitHub Desktop
When you collaborate with other people on a repository, you will want to see what everyone is working on and update your files with the most up-to-date changes.
In Git, fetching is the process of updating your files with the most recent changes. Luckily, GitHub Desktop makes fetching changes easy.
Let’s suppose that someone made an update to our README file from the previous example. The README file now looks like this:
In order to fetch these changes in GitHub Desktop, we can simply click the “Fetch origin” button:
Next, you should click “Pull origin” so that the changes are reflected in the files on your local machine:
Now, navigate to the codecademy-git-test folder on your Desktop (or wherever you chose to locate it). If you open the README file, you will notice that the changes made to the file are correctly shown:
Cloning in GitHub Desktop
Cloning in Git is a way to download a Git project from an online repository to your own computer. Cloning a repository is easy to do with GitHub Desktop.
First, we need a repository to clone. For this tutorial, we’ll clone Codecademy’s datasets repository. You can find this repository here.
Click the “Code” button at the top right and then click “Open with GitHub Desktop” to clone and open the repository with GitHub Desktop:
Next, click “Choose…” to select a local path where you want to clone the repository. When you’re done, click “Clone” to clone the repository:
That’s it! The repository should now be cloned on your computer.
We covered quite a bit in this article. We learned:
- A version control system helps manage files and track changes in projects. It’s like a Google Doc – for coders.
- The difference between Git and GitHub:
- Git is a widely used version control system that lets you manage and keep track of your code.
- GitHub is a cloud-based hosting service that lets you manage your Git repositories. With GitHub, you can back up your personal files, share your code, and collaborate with others.
- Basic functions in Git include commit, fetch, and clone. We use commit to update our default branch, fetch to update our local repository with the updates from the default branch, and clone to save a respository to our local computer.
- We can interact with Git without the command line using the GitHub Desktop interface.
You now have a better grasp of how to how to store, update and share your code using Git and GitHub Desktop!