Skip to content

Understanding Git Basics and Terminology โ€‹

Introduction โ€‹

Git is a distributed version control system that tracks changes in source code during software development. Before diving into practical Git usage, it's essential to understand the fundamental concepts and terminology that form the foundation of how Git works.

This tutorial will cover the core concepts that every Git user should understand, providing you with a solid foundation for working with Git effectively.

What is Version Control? โ€‹

Version control is a system that records changes to files over time so you can recall specific versions later. It allows you to:

  • Track changes to your code
  • Collaborate with other developers
  • Revert to previous versions when needed
  • Understand what changed, when, and who made the changes
  • Maintain different versions of your project simultaneously

Git vs. Other Version Control Systems โ€‹

Centralized vs. Distributed โ€‹

Traditional version control systems (like CVS, Subversion) are centralized:

  • Single central server stores all versions
  • Developers check out files from the central repository
  • If the server goes down, collaboration stops

Git is distributed:

  • Every developer has a complete copy of the project history
  • Can work offline and sync changes later
  • No single point of failure
  • Multiple backup copies exist naturally

Core Git Concepts โ€‹

Repository (Repo) โ€‹

A repository is a storage space where your project lives. It contains:

  • All your project files
  • Complete history of changes
  • Branches and tags
  • Configuration settings

Types of repositories:

  • Local repository: On your computer
  • Remote repository: On a server (like GitHub, GitLab)

Working Directory โ€‹

The working directory is the folder on your computer where you're currently working on your project files. It's where you edit, create, and delete files.

Staging Area (Index) โ€‹

The staging area is a file that stores information about what will go into your next commit. It's like a preview of your next commit.

Think of it as a shopping cart:

  • You add items (changes) to your cart (staging area)
  • When ready, you checkout (commit) everything in your cart

Commit โ€‹

A commit is a snapshot of your project at a specific point in time. Each commit contains:

  • A unique identifier (hash)
  • Author information
  • Timestamp
  • Commit message describing the changes
  • Pointer to the previous commit

Branch โ€‹

A branch is a lightweight movable pointer to a specific commit. It allows you to:

  • Work on different features simultaneously
  • Experiment without affecting the main codebase
  • Collaborate with others on separate features

The default branch is usually called main or master.

HEAD is a pointer that refers to the current branch you're working on. It tells Git which commit you're currently looking at.

Git Workflow States โ€‹

Git files can exist in three main states:

1. Modified โ€‹

  • Files have been changed but not committed
  • Changes exist only in your working directory

2. Staged โ€‹

  • Files are marked to go into the next commit
  • Changes are in the staging area

3. Committed โ€‹

  • Files are safely stored in your local repository
  • Changes are part of the project history

The Three Areas of Git โ€‹

Understanding these three areas is crucial for Git mastery:

Working Directory โ†’ Staging Area โ†’ Repository
     (modify)         (stage)       (commit)

Working Directory โ€‹

  • Where you edit files
  • Contains one version of the project
  • Files can be modified, added, or deleted

Staging Area โ€‹

  • Stores information about what will go into the next commit
  • Also called the "index"
  • Allows you to craft exactly what goes into each commit

Repository โ€‹

  • Where Git stores metadata and object database
  • Contains all versions of your project
  • The .git folder in your project root

Essential Git Terminology โ€‹

Clone โ€‹

Creating a local copy of a remote repository on your computer.

Fork โ€‹

Creating a personal copy of someone else's repository on a hosting service like GitHub.

Pull โ€‹

Fetching changes from a remote repository and merging them into your current branch.

Push โ€‹

Uploading your local commits to a remote repository.

Merge โ€‹

Combining changes from different branches into a single branch.

Rebase โ€‹

Moving or combining commits from one branch to another, creating a linear history.

Tag โ€‹

A reference to a specific commit, usually used to mark release points.

Remote โ€‹

A version of your repository hosted on a server, used for collaboration.

Origin โ€‹

The default name for the remote repository you cloned from.

Upstream โ€‹

The original repository that you forked from (in fork-based workflows).

Git Object Types โ€‹

Git stores everything as objects in its database:

1. Blob (Binary Large Object) โ€‹

  • Stores file contents
  • Doesn't contain filename or directory structure

2. Tree โ€‹

  • Represents directories
  • Contains references to blobs and other trees
  • Stores filenames and permissions

3. Commit โ€‹

  • Points to a tree object
  • Contains metadata (author, timestamp, message)
  • References parent commit(s)

4. Tag โ€‹

  • Points to a commit
  • Contains additional metadata
  • Usually used for releases

Common Git Commands Overview โ€‹

Here are the most frequently used Git commands and their purposes:

Repository Operations โ€‹

  • git init - Initialize a new repository
  • git clone - Copy a repository from remote to local
  • git status - Check the status of your working directory

Basic Workflow โ€‹

  • git add - Stage changes for commit
  • git commit - Save changes to the repository
  • git push - Upload changes to remote repository
  • git pull - Download changes from remote repository

Branch Operations โ€‹

  • git branch - List, create, or delete branches
  • git checkout - Switch branches or restore files
  • git merge - Merge changes from one branch into another

Information Commands โ€‹

  • git log - View commit history
  • git diff - Show changes between commits, branches, etc.
  • git show - Display information about commits

Best Practices for Understanding Git โ€‹

1. Think in Snapshots โ€‹

Git doesn't store differences; it stores snapshots of your entire project at each commit.

2. Commits Are Cheap โ€‹

Don't be afraid to commit often. Small, focused commits are easier to understand and manage.

3. Use Meaningful Commit Messages โ€‹

Write clear, descriptive commit messages that explain what changed and why.

4. Understand the Three States โ€‹

Always be aware of which state your files are in: modified, staged, or committed.

5. Branch Early and Often โ€‹

Use branches for features, experiments, and bug fixes. They're lightweight and easy to work with.

Summary โ€‹

Understanding Git's core concepts and terminology is essential for effective version control. Key takeaways:

  • Git is distributed: Every copy is a complete repository
  • Three states: Modified, staged, committed
  • Three areas: Working directory, staging area, repository
  • Commits are snapshots: Not differences, but complete project states
  • Branches are pointers: Lightweight references to commits
  • HEAD tracks location: Shows where you are in the project history

With these fundamental concepts understood, you're ready to start using Git effectively. The next tutorial will guide you through creating your first Git repository and performing basic operations.

Next Steps โ€‹

Now that you understand Git basics and terminology, you can proceed to:

  1. Creating Your First Git Repository
  2. Basic Git Workflow: Add, Commit, Push
  3. Understanding Git Branches