An Intro to Git and GitHub

August 15, 2023 • Harsh Shinde

Git and GitHub have become popular standards in the software development process. Here's an introduction to how you can utilize both to write code and build applications.

Git and GitHub overview

What Is Git?

Git is a free and open source distributed version control system. What is version control? Essentially, it's a system that allows you to record changes to files over time, thus, you can view specific versions of those files later on.

Why Use Git?

Over time, Git has become an industry standard for development. Being able to snapshot your code at a specific time is incredibly helpful as your codebase grows and you have to reference previous versions of it.

How It Works

With Git, you record local changes to your code using a command-line tool, called the "Git Shell" (you can use Git in other command-line tools — I'll refer to Git Shell through the following sections). Command-line lets you enter commands to view, change, and manage files and folders in a simple terminal, instead of using a graphical user interface (GUI). If you have not used command-line before, don't worry, once you get started, it is incredibly straightforward.

Git workflow diagram

Basic Git Workflow

Essentially, when using Git, you make changes to your code files as you normally would during the development process. When you have completed a coding milestone, or want to snapshot certain changes, you add the files you changed to a staging area and then commit them to the version history of your project (repository) using Git. Below, you'll learn about the Git commands you use for those steps.

Git staging area visualization

Git Staging Area

Terminal Commands

While using Git on the command line, chances are you will also use some basic terminal commands while going through your project and system files / folders, including:

pwd
Check where you are in the current file system
ls
List files in the current directory (folder)
cd [directory-name]
Moves to the given directory name or path
mkdir [directory-name]
Makes a new directory with the given name

Creating Repositories

When you wish to utilize Git for a project, the first command you must do is git init, with the name of your project:

git init [project-name]

You run this command on the Git Shell command-line in the main directory (folder) of your project, which you can navigate to in the Shell using the commands listed above. Once you run this command, Git creates a hidden .git file inside the main directory of your project. This file tracks the version history of your project and is what turns the project into a Git repository, enabling you to run Git commands on it.

Making Changes

git add [file]
git add *
Add files to the staging area

Once you make changes to your files and choose to snapshot them to your project's version history, you have to add them to the staging area with git add, by file name, or by including all of the files in your current folder using git add *.

git commit -m "[message]"
Commit your changes with a descriptive message

To finally commit the changes you made to your files from the staging area to your repository's version history, you need to run git commit with a descriptive message of what changes you made.

git status
View the status of your working directory

If at any point, you wish to view a summary of the files you have changed and not yet committed, simply run git status in your project's repository on the Git Shell command-line.

Git workflow detailed diagram

Detailed Git Workflow

Working with GitHub

GitHub collaboration

GitHub Collaboration Flow

Overview

GitHub features overview

In essence, GitHub is a service that allows you to host your Git repositories online and collaborate with others on them. You can use GitHub through their web portal as well as the GitHub desktop GUI and the Git Shell.

As a service, GitHub is now used by 12 million developers and organizations, and has become a fairly popular standard for collaborating on projects and open-sourcing code.

How GitHub Works

GitHub workflow

With GitHub, you have the same local process of adding and committing files to an initialized Git repository on your computer. However, you can utilize GitHub to push your changes to GitHub's hosting service. This allows other people to similarly work on the same project, pull your changes to their computers, and push their own changes to GitHub.

Creating & Copying Repositories

GitHub fork process

With Git on your local computer, if you want to create a new repository, you must run git init. However, many times you may work on projects that are hosted on GitHub and have already been initialized.

One of the ways to copy a repository to your GitHub account is using fork, which is available on GitHub's website.

git clone [url]
Clone a repository to your local machine

Pushing & Pulling Changes

git push [repo] [branch]
Push your changes to GitHub
git pull
Pull latest changes from GitHub

Important Note about Merge Conflicts

It is important to remember, that while you may have a repository hosted on GitHub, the version history of your local copy can be different than the version history of your repository online.

If you try to pull or push changes to files that have already been changed by someone else, you can run into a merge conflict. You will need to resolve these conflicts manually.

For more information, see GitHub's guide on resolving merge conflicts.

Working with Branches

Git branching visualization

When using Git, you have the ability to view the version history of your project's development. However, sometimes you may choose to develop features, fix bugs, or experiment in ways where you want to separate your main project's code from another variant.

You can do this with branches, which are essentially parallel versions of your repository's main code— that code is developed on the "master" branch. You can create multiple branches for collaboration and other unique development to your code.

Learning More About Branches

To learn more about branches and merging changes, check out Git's official documentation on branching.

Getting Started

Getting started with Git and GitHub

To get started with Git and GitHub:

  1. Sign up for a GitHub account at github.com
  2. Download GitHub's desktop GUI at desktop.github.com
  3. Set up your credentials using the GitHub desktop application or Git Shell

GitHub Student Developer Pack

As a student, you can get access to the GitHub Student Developer Pack, which includes benefits from GitHub as well as other partners. Learn more at education.github.com/pack.

Microsoft Imagine

Microsoft Imagine provides students with professional developer and designer tools at no cost. Create an account to get started!

Additional Git features Git and GitHub ecosystem

Additional Resources