Sitemap

A Comprehensive Introduction to Git & GitHub — Part 1/4: Core git features.

5 min readSep 18, 2024

--

Press enter or click to view image in full size
Git architecture

Introduction
Git is one of the most powerful tools for managing code changes, offering developers full control over their projects. However, understanding how to use Git effectively requires more than just a quick overview. This article aims to give you a deep understanding of Git’s core features, all of which operate locally on your machine. By mastering these basics, you’ll build a strong foundation to manage code, experiment with new ideas, and track your development history.

By the end of this article, you’ll understand:

  • How Git works and why it’s important.
  • How to install Git and set it up on your machine.
  • The fundamental Git operations like committing, branching, and merging, all done locally.

Let’s dive into the core of Git and build your foundation step by step!

What is Git?

Git is a distributed version control system that helps you track changes in your code. Unlike centralized systems, Git operates entirely on your local machine by default. It allows you to keep a history of your work, create isolated branches for new features, and combine changes in a controlled manner.

Everything you do with Git can be done locally — there’s no need for an internet connection or external server unless you want to collaborate with others (which will be covered in later articles).

The Architecture of Git

Before diving into the commands, it’s important to understand the architecture of Git and how it manages your project files. Git has three main areas where it tracks changes:

  • Working Directory: This is where your project files live. Any changes you make to your files happen in the working directory.
  • Staging Area (Index): Before committing changes, they need to be staged. The staging area holds a snapshot of the changes you intend to commit, allowing you to include only certain modifications.
  • Repository (Repo): Once changes are staged and committed, they are saved in the local repository. This is where Git stores the history of your project, keeping track of every commit you’ve made.

Understanding these areas will help you grasp how Git tracks your project files at every stage of development.

Installation & Setup

Before using Git, you need to install it on your system. Here’s how to get started based on your operating system:

Installing Git

  • Windows: Download Git from the official site and follow the installation instructions.
  • MacOS: Use Homebrew by running brew install git.
  • Linux: Install Git using your package manager, e.g., for Ubuntu:
sudo apt install git

Initial Setup

Once installed, you’ll want to configure Git with your identity, which is used to tag your commits:

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

This step ensures that your commits are linked to your identity.

Creating a Repository

To begin tracking changes in a project, you need to create a Git repository locally. This is done by running the following command in your project directory:

git init

This initializes an empty Git repository in the current folder. From now on, Git will start tracking changes made to the files in this directory.

Tracking Changes and Committing

With Git, you can track the changes in your project, commit those changes, and keep a record of your development history.

Checking the Status

Before committing any changes, it’s useful to check what has been modified. This is done with:

git status

Git will show you which files have been modified, which are staged for commit, and which are untracked (new files that aren’t yet being tracked by Git).

Staging Changes

Before committing, you need to stage changes. This tells Git which modifications should be included in the next commit. To stage a file, use:

git add <file-name>

To stage all changes in the current directory, use:

git add .

Committing Changes

After staging your changes, you can commit them. This means recording the changes in the repository’s history. Each commit should include a message describing the change:

git commit -m "Your descriptive commit message"

This action creates a local snapshot of your project at that moment.

Viewing Commit History

Git allows you to view the history of all commits, providing insights into what has been changed, when, and by whom. One of the most commonly used commands for this is git log.

Basic git log

To view the full commit history, you can use:

git log

This displays a detailed list of commits, showing the author, date, and commit message for each one.

Compact View with --oneline

If you want a more concise view of the commit history, you can use the --oneline option:

git log --oneline

This will show each commit in a single line, including the commit hash and the message.

View Commits for a Specific File

To see the commit history for a specific file, you can specify the file name:

git log <file-name>

This will filter the log to show only the commits that modified the given file.

With these options, you can adjust how much detail you want to see when reviewing your commit history.

Working with Branches

Branches allow you to work on new features or experiments without affecting the main part of your project. Git makes branching easy and flexible.

Creating a Branch

To create a new branch, run:

git branch <branch-name>

This creates a separate line of development in your project. You can switch between branches without losing work.

Switching Branches

To switch to an existing branch, use:

git switch <branch-name>

This will change your working directory to reflect the files and changes in that branch.

Viewing Branches

You can list all available branches in your project with:

git branch

Merging Branches

When you’re done working on a branch, you may want to merge it back into the main branch. Merging allows you to bring changes from one branch into another.

Performing a Merge

First, switch back to the main branch (often called main or master):

git switch  main

Then merge your feature branch into it:

git merge <branch-name>

Git will combine the changes from your branch into the main branch.

Handling Merge Conflicts

Sometimes, when Git tries to merge branches, it encounters conflicting changes that it can’t resolve automatically. This is called a merge conflict. When this happens, Git will show you the conflicting files, and you’ll need to manually edit them to resolve the conflict.

After resolving the conflicts, you need to stage the resolved files:

git add <file-name>

Then commit the merge:

git commit -m "Resolved merge conflicts"

Conclusion

Git is a powerful tool for version control, and mastering its core features will give you the confidence to manage your projects more effectively. In this article, we explored Git’s foundational concepts: initializing a repository, tracking changes, committing, and working with branches and merges — all performed locally on your machine. By understanding these basics, you’re well on your way to mastering version control.

In the next article, we’ll dive deeper into Git’s more advanced features, such as diffing, stashing, and undoing changes, to give you even more control over your code and workflow. Stay tuned as we build on this solid foundation!

--

--

Mina Samir
Mina Samir

Written by Mina Samir

Software Engineer, Interested in sharing knowledge in problem solving, system design, cloud based applications.

No responses yet