An Introduction to Git: Understanding the Fundamentals

Introduction

This article provides an overview of Git, a distributed version control system that allows local work and branching without needing centralized permissions. It details Git commands, categorizing them into high-level (user-friendly) and low-level (system-focused) commands, and explains key terms like 'repo,' 'commit,' 'index,' and 'remote.' The article also highlights important concepts such as the acyclic graph structure of Git and best practices like frequent commits and the use of squashing to simplify commit history.

What is GIT?

Git is a distributed version control system, VCS. Instead of the traditional centralized control system where even checking out a file required admin privileges, git allows any work to be locally and may diverge as much as you want.

Commands in GIT

In Git, commands are divided into high level ("Porcelain") and low level ("Plumbing") commands. High level commands are designed to be user-friendly and are what you use to interact with Git. Low level commands are more for Git itself and are designed to be used by other tools.

Key Terms

  1. repo: a git tracked project.

  2. working directory: the files you see in your computer.

  3. commit: a point in time representing the project in its entirety. The SHA that represents a commit, 40 a-f 0-9 characters, is calculated from the contents of change, author, time and more

  4. index: used interchangeably with "stage". The index is where you place changes you want to commit.

    From Github blog: The Git index is a critical data structure in Git. It servers as the staging area between the files you have on your filesystem and your commit history. when you run git add, the files from your working directory are hashed and stored as objects in index, leading them to be staged changes

  5. squash: to take several commits and turn it into one commit. Technically a squash would be taking N commits and turning it into N-1 or 1 commit, but typically its N commits to 1 commit.

  6. work tree | working tree | main working tree: the directory where you are working. The files you see in your computer.(Your git repo). Your working tree is setup by git init or git clone.

  7. untracked files: files that are not tracked by git. They are not in the index, and they are not in the commit history.

  8. indexed/staged: this is where the changes are to be committed. You must stage before you commit and you stage changes by using git add command.

  9. tracked files: files that are already tracked by git. A file could be tracked AND have staged changes (changes stored in the index) ready to be committed.

  10. remote: the same git repo on another computer or directory. You can accept changes from or potentially push changes too.

Key Facts About GIT

  • git is an acyclic graph.

  • in git, each commit is a node in the graph, and each pointer is the child to parent relationship. Meaning you can have more than one parent, but you cannot have a child that is also a parent(cycle in the graph).

  • if you delete untracked files, they are lost forever. commit early, commit often, you can always change history to make it one commit (Squashing)