Git and GitHub in a Nutshell: Definitive tutorial for beginners

Aug 28, 2020 - 15 min read
Jerry Ejonavi
editor-page-cover

Git and GitHub are integral to the software development workflow of many large teams. They also power some of the most popular open-source projects. It’s essential that you get familiar with Git and make GitHub an integral part of your workflow as a developer.

Today, we will provide a quick introduction to Git and GitHub. You will learn the basics of these tools and learn how to create a new repository on your local machine and on GitHub. You will also learn how to synchronize your local repository with the remote one on GitHub. Let’s get started!

Today, we will cover:


Master the most popular software development tools.

Learn the basics of version control as well as how GitHub can serve as a software development platform.

A Guide to Git & Version Control


What is Git?

Git is a distributed version control system designed to track changes in a project’s files. Git was released in 2005 by Linus Torvalds for the Linux kernel. Other kernel developers contributed to its development, and it has become the most popular choice for software teams. Git offers strong support for non-linear development and speed.

Version control systems are a software tool that manage changes to a computer program, document, or collection of files. They help a user or a group of users track changes made to a project over time.

Git works by saving a snapshot of the state of the project files at a certain time. It keeps the files that did get updated and references of the files that did change since the last snapshot. Git is essential to software development since most projects are developed by teams.

Git is the de facto standard for version control. More than 87% of developers use Git as a control system. Any developer needs at least a base knowledge of Git to be successful and relevant to the market.

Most companies, big and small, use Git as a repository, including:

  • Netflix
  • Reddit
  • Lyft
  • Shopify
  • Facebook
  • Atlasssian
  • Google
  • eBay

Git terminology

%0 node_1 Git Features node_2 Tag node_1->node_2 node_3 Commit node_1->node_3 node_1598559443727 URL node_1->node_1598559443727 node_1598559453941 Index node_1->node_1598559453941 node_4 HEAD node_1->node_4 node_1598559459173 Branch node_1->node_1598559459173

What are Git states?

Git has three primary states that it allows the project’s files to acquire. The three states are outlined below.

  • Modified: files that have been changed, but the changes have not yet been marked by Git. These changes won’t become part of the next snapshot.
  • Staged: the changes have been tracked by Git and will be marked as such in the next snapshot.
  • Committed: the changed files that have successfully become part of the latest snapshot.

A repository is a directory or folder where your project lives, and it can either be local (on your computer) or hosted online. It contains the entire codebase and its revision history. Popular online hosting services for repositories are GitHub, GitLab, and Bitbucket. We will learn more about GitHub later.

What is Git commit?

Think of Git like a tool that manages a timeline. Commits are the units that timeline, like milestones or events. With the commit command, we are creating those individual events.

%0 node_1 Project Creation node_2 Commit #1 node_1->node_2 node_3 Commit #2 node_2->node_3

The commit command captures a snapshot of a project at a particular moment in development. Committed snapshots will not change on their own. Prior to the execution of git commit, we use the git add command to promote any changes that we will later store. Commit and add are the most popular commands.

What is Git checkout?

The git checkout command is a way of switching between branches or restores working tree files. Think of this as switching between different versions or features of a project. This command uses three entities: files, commits, and branches.

What is Git clone?

The git clone command is used to target a repository and copy it. You then have your own version of that repo and can start editing it without altering the original. In general, the original is held on a remote server, like GitHub.

What is Git pull?

The git pull command can be used to access and download files from a remote repository and update your local repository so it matches the updates to the original. Think of this like you’re merging your version and the original. The git pull command is made up of two commands: git fetch and then git merge.

What is Git tag?

Tags are a reference to a specific moment in a Git repo’s file history. Think of this like a branch that doesn’t change. Tagging allows you to mark important checkpoints in a project’s timeline (i.e. software version releases).


What is GitHub?

GitHub is a platform that can be used to host code online. Think of GitHub as a platform that stores the whole codebase in a remote repository. It comes with tools to collaborate on projects of any size.

By enabling the creation of remote repositories, GitHub allows developers to have a unified source of truth for their source code. This is what makes open source possible.

To create your repositories on GitHub or contribute to open source projects, you will need to create a personal account on GitHub.

If your codebase is in a remote repository on GitHub:

  • Every other contributor who wants to work on the project will know that they can get the code from the remote repository.
  • Any updates that they make and then want to share can push those changes to the remote repository instead of sending each contributor the updates one by one; GitHub serves as the project’s single source of truth.
  • Contributors and collaborators can make use of GitHub’s project management and automation tools to make development fast and efficient.

Get started with Git

The Git software is a powerful command-line tool that you can install on your machine, whether you use Windows, macOS, or Linux.


1. Installing for Linux

For a Debian-based Operating System such as Ubuntu, you can run the following command to install Git in a one-time operation:

sudo apt install git-all

If you are using a different operating system or Linix distribution, you can find installation instructions on this link.

Once you have git installed, you can then run git commands locally. To confirm that Git has been installed successfully, run this command in your terminal:

git --version

The output should be the current version of Git installed in the system.


2. First time configuration

The git config command allows you to set configuration variables that control how git looks and operates. The config command works on different levels:

  • Local-level: This means that all your credentials and configurations are only limited to your project’s directory. By default, git config writes to a local level when no configuration is passed.
  • Global-level: this configuration is specific to a user on the operating system; the configuration values live in the user’s home directory.
  • System-level: These configurations are placed in the system’s root path; it tracks all users and all repos on the operating system.

The first thing you configure after a successful installation is your email address and username; this will identify our contributions and changes in the project source code. To set up your user name and email, run the following in your command line:

git config --global user.email "educative_learner@example.com"

git config --global user.name "Educative Learner"

You can verify that the commands worked by running the following commands:

git config user.email
git config user.name

To see all the configurations currently saved on your working directory, run:

git config -l

If you run the previous command with a --global flag, your output will only contain configuration settings at the system level.


Keep the learning going.

Learn Git and version control without scrubbing through videos or documentation. You will learn more complex topics including Git commits, branches, merging, and rebasing. By the end, you will have a great new skill that you will use throughout your entire career.

A Guide to Git & Version Control



Using Git for a new project

Let’s see how we can set up a new project using Git.

1. Initializing the repository

First, create a new directory using the following command:

mkdir git_started

Next, you need to navigate to the newly created directory:

cd git_started

To initialize a git repository in this folder, which is now your working directory, run the following command:

git init

Running this command would initialize an empty Git repository in the current directory. A new subfolder named .git is created which contains several files and more subdirectories that Git will use to keep track of changes in the project.

Let’s create the first file that we will be tracking using Git. Ensure you are still in the git_started directory on your terminal, then run the following command:

touch README.md

This will create a file named README with the Markdown extension.

Note: Markdown is a lightweight markup language with plain-text-formatting syntax.

Most software used to browse Git repositories visually (by providing a GUI) look for a readme.md file and render the contents on the project’s homepage. You would typically include information about your project such as setup instructions in this file.

Open the file in your editor and add the following lines to it:

# git_started

This is your first Git Project. We will be working on this file as we learn more about Git.

Save and close this file. With the file saved, the state of your repository should have changed. You can check this status by running the following command:

git status

This command doesn’t change or update anything. Instead, it prints out which files are modified, staged, or untracked. An untracked file is one that hasn’t been added to the git index yet, while a file that has been changed since your last commit was made will be seen as modified by git.


2. Staging Commits

To start tracking files in your project directory, you will utilize the git add command. This command adds the file(s) you specify to a staging area. Files that have been staged will be added to the next commit.

A commit is a snapshot of the entire state that your project is in at that moment. Git keeps track of your project through a series, or chain, of commits. The most recent snapshot of your repository is referred to as the HEAD.

As soon as you create a new commit, it will directly link to the HEAD. However, since the latest commit is now the most recent one, it will be considered the HEAD instead, replacing the previous one.

Let’s commit our project’s readme file now. First, stage the file by running:

git add .

Note that this command adds all untracked and modified files to the staging area. If you want to add a specific file instead, specify the filename like this:

git add README.md

You can also remove files from staging by running the following command:

git rm --cached [filename] 

Next, you need to make a commit with an accompanying commit message. Commit messages are short descriptive messages describing what changes were made.

Let’s commit the file that’s currently staged by running the following command:

git commit -m "add readme"

This command saves a new commit with the message “add readme”. Now, when you run git status, you will get a message indicating that your working tree is clean (no modified or untracked files).


3. Ignoring Files

Sometimes there are untracked files that you would not like to add to your git index. These may be user-specific configuration files, like a .env file, or folders with large files, like the vendor folder for PHP projects or node_modules in JavaScript projects.

You can instruct git to ignore these files by adding glob patterns to a .gitignore file. This file will live in your project’s root directory and can contain any number of patterns. Any untracked file or folder that matches a pattern specified in the .gitignore file is ignored by git.

Let’s see what this looks like in practice. Create a file named .gitignore in your current working directory.

touch .gitignore

Now, open this file and add the following lines to it:

# Config files
.env
.env.local
/.vscode

# Large folders
/node_modules
/vendor

Each line in a .gitignore file specifies a pattern. Lines that begin with a # sign are comments. The patterns here are similar to what you would typically see in a PHP or JavaScript project.

The following are examples of patterns used in a .gitignore file:

  • *.env matches all files with a .env extension
  • /vendor matches the folder named vendor, as well as its subfolders and files
  • build_output.log would match the .log file named build_output.

Adding Git to an existing project

To add git to an already existing project, follow these steps in order:

  1. In your terminal make sure you are in the project directory,
  2. Initialize git using git init
  3. Create a .gitignore file and specify files and folders to be ignored (if this is not necessary, you can skip to step 4)
  4. Stage your files using git add . Commit your changes with an appropriate commit message, for example: git commit -m "first commit"

1. Working with remotes

Let’s create a remote repository on GitHub. Once you are logged in and are on the homepage, you will notice a button on the top left side titled ‘New’ (see figure below).

Once you click on the ‘New’ button, GitHub will redirect you to a different page where you will have to provide a name for the repository. You can also add a description of your repository, but this is optional.

GitHub allows you to make your repositories either public or private.

  • A public repository is accessible to anyone. Anyone is able to see the codebase and clone this repository to their local machine for use.
  • A private repository is only visible to people who you have chosen. No other person is able to view it.

Our project already has a README and .gitignore file, so we can leave the options to initialize with a README and .gitignore unchecked.


2. Pushing code to GitHub

Having created the repository, we now need to add the files from our local repository to the remote one on GitHub. To do this, we first have to link to the remote repository from our local project.

Git provides the remote command to help with this. The git remote command allows Git to track remote repositories and connects local repositories to those remote ones. For example, let’s say we want to add our remote repository to the local one. The command command format is:

git remote add <remote_name> <remote_url>

Therefore, the command we will run will be:

git remote add origin <url_to_remote_repository>

Note: The name origin is essentially a more human-readable way to link to the remote repository instead of always having to use the actual URL. origin is simply a conventional name, but we can use any other name as well.

Once we have added the remote repository URL to our local one, we will want to push or upload our local code and its revision history to the remote repository. This can be done using git push.

The git push command will update the remote repository code with all the updates that were made in the local repository.

Let’s make use of this command in our project. First, commit the .gitignore file we created earlier.

git add .

git commit -m "add .gitignore"

Now you can run the following command to push your code to the remote repository:

git push origin master

And there you have it. You have successfully pushed your code to a remote repository.


3. Updating the local repository

To update your local repository from a remote one, you need to run the git pull. This command fetches every change to the remote branch and automatically merges it to your local repository.

Every change made to your local repository must be committed before a merge can occur.

What to learn next

Congratulations on creating your first local and remote Git repository! You will need to have a good grasp of Git and GitHub as many teams utilize these tools in their product development workflows. There’s still a lot to learn. The next things to look into are:

  • Git logs
  • How to undo commits
  • Creating and manipulating branches
  • Resolve merge conflicts
  • Git rebase vs. merging
  • and more

To get started with these concepts and more, check out Educative’s course A Guide to Git & Version Control. This course takes you through Git basics to more advanced concepts. You will learn how to work with branches, how to utilize the git stash command, merging code and resolving merge conflicts among other concepts.

Cheers and happy learning!


Continue reading about Git and open source


WRITTEN BYJerry Ejonavi

Join a community of 500,000 monthly readers. A free, bi-monthly email with a roundup of Educative's top articles and coding tips.