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.
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.
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
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:
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:
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
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 *.
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.
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.
Detailed Git Workflow
Working with GitHub
GitHub Collaboration Flow
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
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
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.
Pushing & Pulling Changes
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
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
To get started with Git and GitHub:
- Sign up for a GitHub account at github.com
- Download GitHub's desktop GUI at desktop.github.com
- 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 Resources
- git-scm.com - The main website and documentation for Git
- help.github.com - GitHub's help documentation