Skip to main content

Intro to Git

What is version control, and why should you care?

A Version Control System (VCS) records changes made to files over time so that you can recall specific versions later. It also makes working in teams easier because it enables developers to submit changes to be merged into the codebase.

More specifically, a VCS allows you to:

  • Revert files to a previous state
  • Review changes made over time
  • Collaborate on a set of files with others
  • Create separate branches of the codebase to develop new features without impacting the main, or production branch. This process can be done non-linearly

In SEI, we'll be using Git - the world's most popular version control system. Linus Torvolds created Git in 2005 to help with the development of his main project at the time - Linux.

Git is a version control system designed for tracking changes in source code. It allows developers to collaborate efficiently, manage code history, create and merge branches, and ensure data integrity through cryptographic hashing. Git operates independently but can be integrated with collaboration platforms for additional features.

Git Vocabulary

Git Repository

A Git repository (aka Git repo, or just repo) is essentially a copy of your project, but that copy holds information about the who, what, and when of your project. This information helps developers use Git to collaborate, track changes through time, or even revert to previous versions of the code when necessary.

Git repositories can be stored locally on a developer’s machine or hosted on remote servers such as GitHub.

In short - a Git repo allows us to find all past versions of the code, see who made changes as the project evolved, and when those changes were made.

Git Branch

A Git branch (or just branch) is an independent line of development within a Git repository, allowing developers to work on different features, bug fixes, or experiments without affecting the main codebase.

Each branch represents a separate snapshot of the project's files, diverging from a common point in the repository's history. In short, branches allow developers to contribute to the same set of code and not step on one another's toes. As a result, branches are an essential part of collaboration.

In this course, we will use the main branch as our central source of truth, and the default branch name. As of 2020, this is the default naming convention for Git and GitHub. You may see other organizations use the now outdated master branch as their default branch name instead - as time goes on, this will be less and less common.

Remotes

A remote is a reference to a repository hosted on an external server, allowing synchronization and collaboration between a local Git repository and its counterpart located elsewhere.

In this course remotes typically take the form of a URL that will point to a repository on GitHub, but other code-sharing platforms like GitLab and BitBucket exist. When collaborating, these external repositories provide a centralized location for sharing, updating, and accessing a codebase among multiple team members. We’ll discuss remotes in more detail when we cover GitHub itself.

Local Commands

git init

What it does

When run, it initializes a new Git repository by creating a hidden .git directory in the current working directory.

When and why we use it

Use the git init command when you start a brand new project. Initializing a local repository allows you to track file changes within the selected directory.


caution

IMPORTANT: Do not create a repo within an existing repo! If you find your computer very sluggish, it might be because you have "nested" repos. It's not uncommon for students to accidentally make their home folder (~) a repo - so start there if you suspect something is wrong.

To remove a local git repository from the current working directory, you can (carefully) enter the command rm -rf .git


git add .

What it does

This command adds all files/folders in the current directory to a staging area, where they will be added to the next commit.

When and why we use it

Use this command after making changes to your code that you want to potentially commit to the local repository. To track changes to files, you must add them to the staging area, where the changes will be committed.

The staging area is an intermediate area where changes to files are stored before being committed to the repository.

The . in git add . will add all of the files in the directory that we are currently working with. We are also able to add one file at a time, or multiples, by simply writing the file names out when we add:

git add index.html
git add index.html style.css js/app.js

Later on, as we cover GitHub and collaborative coding, it will become more clear why not adding every single file at once can be beneficial. For now, git add . will work great!

While this does overly simplify the Git workflow and only gets us so far, while you’re learning it helps to think of your code as a rocket 🚀 - running the git add . command puts the entire rocket on the launch pad.


git commit -m "commit message"

What it does

This command allows you to provide a checkpoint where you can save the recent changes you've made to your code. It also allows you to enter a message describing the changes that have been made to your code for future reference.

Commit messages should be in the present tense and should describe the changes that the code is making. For example, if a commit fixes a typo in your code, the commit message should be "fix typo", not "fixed typo".

When and why we use it

Use this command after adding changes to the staging area to commit those changes. Commits provide a list detailing changes that have been made to your code. This allows you (or anyone viewing your repository) to see what code changes have been made over time. It also allows you to revert to previous versions of your code if you end up breaking something and can't figure out how to fix it.

Continuing the rocket analogy 🚀 - committing your code is noting down the current state of the rocket that you've just put in the launch pad. Maybe you added another seat, maybe you managed to shave off some of the hull weight - this commit will help keep track of whatever changed since the previous iteration of the rocket.

git status

What it does

It tells us what is happening in a local repository at any given moment - Is the branch up to date? Are there changes we can commit to it? What is the state of those files?

When and why we use it

Use this command to help understand the current status of our Git repo.

If you’re ever having trouble with git commands, git status is usually a good way to start troubleshooting!