DEV Community

Ava Parker
Ava Parker

Posted on

Master Git in 30 Minutes: Unlock Essential Terms and Commands for Efficient Collaboration

Mastering Git Essentials

“Understanding the technical jargon used in software development is crucial before diving into advanced commands.”

If you agree, then you're in the right place.

Learning the Git workflow and its core concepts will significantly boost your productivity when managing your code with the Git version control system on a daily basis.

Unlocking the Power of Git

Git, like other Version Control Systems (VCSs), tracks different versions of the same data or file type.

What sets Git apart from other VCSs is its distributed nature, also known as a Distributed Version Control System (DVCS), where every developer machine serves as both a “node” and a “hub”.

Git ensures data integrity and treats every single filesystem alteration as data.

The fundamental architecture of Git is based on branches, with your initial project originating from a “master” branch. This facilitates efficient feature development, effortless bug fixes, and code reviews, ultimately leading to frequent synchronous work merges among peers.

The philosophy behind Git, developed by Linus Torvalds, revolves around “speed”, “seamless branching and merging”, “data integrity”, and “large-scale collaborations”.

Git stores your entire project history locally, allowing you to quickly access older versions of your data.

For more information on mastering Git, visit computerstechnicians.com.

Understanding the Git Workflow: The Three States of Git Files

git workflow - the 3 states of git files

Unlike many VCSs that employ a two-tier architecture — a repository and a working copy — Git operates on a three-tier model, comprising a working directory, staging area, and local repository.

This three-layered approach allows you to save your work-in-progress at different intervals and versions.

The three stages of Git store different (or identical) states of the same code (object) in each layer.

A Git project, when first initialized (or cloned), creates a ‘working directory’ on your local machine, allowing you to start editing your source code.

At this initial stage, you have the freedom to modify files and directories in your source code, making additions, deletions, moves, renames, or copies using your preferred editing tools. (Personally, I'm a fan of 'vim' – the most efficient and powerful text editor).

Once you're satisfied with your edits, you prepare your changes for the next step using the 'git add' command, which advances to the subsequent stage – an 'Index' or 'Staging area'. This Staging area provides a preview of what will be committed next, giving you a chance to review your changes.

However, if you make further edits in your working directory while your initial set of code is already staged (in the Staging area), the Working directory and Staging area will have disparate snapshots (versions) of your project, which can lead to inconsistencies.

To synchronize both stages, all you need to do is stage (git add) the latest Working directory changes you've made, ensuring that both areas are in sync.

Besides adding code changes, if you wish to reverse any of the staged changes in the index, git offers distinct commands to do so, providing flexibility in your workflow.

If there are no changes required at this stage, you proceed and commit (‘git commit’) your changes, and the code moves and gets stored in the local git repository, creating a permanent snapshot of your project.

A commit ensures that the same snapshot of your project exists in all three stages and that all three stages of git are in sync with each other, providing a unified view of your project.

The ‘git status’ command displays the working tree status and indicates in which stage your files are, giving you a clear overview of your project's state.

Essential Git Concepts and Commands

Although git is a VCS, it has its own unique filesystem architecture to manage source code, setting it apart from other version control systems.

Some popular VCSs you might have already used are CVS (Concurrent Version Control System), SVN (Apache SubVersioN), ClearCase, and BitKeeper, to name a few, but it's essential to understand git's distinct approach.

There is a general tendency to draw parallels between some of git's terms and those of earlier VCSs, but it's crucial to avoid getting carried away by how you used these terms in the past.

Repository

This is a local hub on your machine where the entire snapshot of your project is stored, providing a comprehensive history of your project's evolution. Every minor change is stored and retrievable, allowing you to jump to any older state (time-travel in history) of your code.

Working Directory

This is a local working copy of the project's latest code, where you can experiment and make changes.

Index (Staging area, Cache)

An Index is the snapshot of your next commit, serving as an intermediate layer between the working directory (where the code is developed) and the local repository (where the code resides).

This layer offers a swift preview of the project snapshot you're about to save. You can still refine (add, modify, or delete) your code in the Index.

You can also revert to a previous version (state) of a project from this point.

Commit

A commit embodies the latest snapshot (state) of a project.

git commit

Each commit has a unique identifier. All commit logs are stored in the local repository.

A commit is a git object that stores the following attributes: commit ID, author name, authored date, and a commit message (header and body).

git hash or the ‘SHA-1’

The unique commit ID is referred to as a ‘git hash’ or ‘SHA-1’. Every filesystem alteration (add, delete, edit, move, copy, rename, file permissions, etc.) is treated as a file and its contents are converted into a unique SHA-1 code.

Here is a sample commit ID: 8db083e7df7c9241e640b66c89c6f02649ac885a

They are often referred to by the first 7 unique digits, such as 8db083e

You never need to memorize the entire hash ID. git has a convenient way of handling these commit (hash) IDs using references such as branches and tags.

Branching Out

A branch is a parallel, independent line of development.

A branch allows you to work on the same piece of code in your isolated workspace.

Every branch has its own copy of the project history and develops on its own code. They are easily and often merged with each other.

Master Branch

A master is the primary default local branch when the project is first created as a git project.

HEAD of the Game

A HEAD is the snapshot of the latest commit on every branch.

It is a short name or pointer reference to an SHA-1 of a commit ID on each branch.

A HEAD always points to the branch’s latest commit (code) and automatically moves forward with each commit to point to the latest commit ID.

Extra: There is a concept of a ‘detached HEAD’ when the branch points to an older commit and not the latest one.

So you see, you now have the reference ‘HEAD’ for your latest commit ID and do not have to memorize the commit ID of your most recent work.

Checkout Time

This command switches over to the specified branch and displays the current project state as it is in the branch.

Moreover, it restores the previous working tree files to their original state.

Repository Duplication

A duplicated repository serves as a functional duplicate of a remote repository, mirroring its entire structure and content.

The 'git clone' command fetches the remote repository and sets up a working directory on your local machine, effectively creating a local replica.

Additionally, this command establishes a remote handler or pointer reference, linking the local repository to the remote repository, thereby facilitating seamless communication between the two.

Remote Repository Illustration

The screenshot depicts a local repository — “learn_branching” — that tracks a remote repository with the URL “https://github.com/divyabhushan/learn_branching.git”, with “origin” serving as the designation for the remote handler to the remote repository.

Final Thoughts

Mastering these fundamental concepts will undoubtedly provide you with a deeper understanding of leveraging git in the most efficient manner possible. Naturally, there are numerous additional concepts in git waiting to be explored.

Consult the git manual pages for a comprehensive list of available commands by running “git help -a”. To delve into a specific command, use “git help .

Top comments (0)