What is git?

Published: (January 19, 2026 at 12:10 PM EST)
5 min read
Source: Dev.to

Source: Dev.to

Why You Need Git

For many developers, a pendrive is just a place to store and retrieve old projects or files.
But when you have too many folders, redundant files, and a nearly‑full pendrive, you start deleting the oldest folders and keeping only the last 1‑2 versions of a project.

Later, after adding new features and removing old ones, you may realize that a piece of basic functionality you need is missing and you have no record of the older versions.

That’s where Git comes in as a saviour.

What Git Gives You

  • Every time you stage or push (later) a tested change, Git saves a snapshot inside the hidden .git directory.
  • If you mess up, you can backtrack to a previous version and keep your app running.
  • Git compresses only the changes (deltas) rather than storing the full code each time, so the repository stays small compared with simply copying whole folders onto a pendrive.
  • Snapshots are stored in a compressed format and include a full history of the project’s file structure.

Inside a Git Commit

When you commit, Git creates objects in .git/objects/:

FolderWhat it Holds
blobThe actual file contents (the changes).
treeThe directory structure for that commit.
commitMetadata (author, date, message) and the SHA‑1 hash that identifies the commit.

Each commit gets a unique SHA‑1 hash.
When you ask for a previous version, Git:

  1. Looks up the commit object (metadata).
  2. Reads the associated tree to reconstruct the directory layout.
  3. Retrieves the required blobs (file contents).
  4. Writes the reconstructed files into your working directory.

Simple Developer Workflow

1. Initialise a Repository

git init

Creates the .git folder and tells Git to start tracking the project.

2. Stage Files

git add          # add specific files
# or
git add .

Marks the files as ready to be committed.

3. Commit

git commit -m "Your commit message here"

Creates a snapshot with a unique hash. This is essential for later retrieving a specific feature or bug‑fix.

4. Push (optional)

git push origin <branch-name>

Uploads your commits to a remote repository (e.g., GitHub, GitLab) so the project is tracked off‑site.

Branches – Working on Multiple Lines of Development

A branch is simply a pointer to a commit.
Git creates a default master (or main) branch, but you should create additional branches for:

  • Feature development – work on new functionality without disturbing the stable code.
  • Testing – verify changes before merging them into the main line.
  • Collaboration – each developer can have their own branch, reducing the chance of conflicts.

Example:
If you and a friend are building a Todo app, you might have three branches:

  • dev‑alice – your work
  • dev‑bob – your friend’s work
  • test – integration testing before merging into main

For more details, see the official documentation.

Viewing History

  • Full log – shows all metadata and hashes

    git log
  • One‑line summary – shows only the short hash and commit message

    git log --oneline

Copy the hash of the commit you want to revisit (e.g., 01xyz2).

Checking Out an Older Version

Detached HEAD (temporary view)

git checkout 01xyz2

Shows the project as it was at that commit, but you are in a detached HEAD state (not on any branch).

Create a New Branch from that Commit

git checkout -b 01xyz2
  • Checks out the old version and creates a new branch pointing to it, so you stay on a proper branch instead of a detached HEAD.

Going Back Permanently

After inspecting the old code you have two options:

  1. Leave the repository as‑is – just use the old code for reference.

  2. Reset the current branch to that commit (dangerous – rewrites history).

    # Move the current branch pointer to the old commit
    git reset --hard 01xyz2

    Use --hard only if you are sure you want to discard all newer commits.

TL;DR

  • Git stores only the changes in a compressed format, keeping history lightweight.
  • Every commit is a snapshot identified by a SHA‑1 hash.
  • Use git init, git add, git commit, and git push to start tracking.
  • Branches let multiple developers work in parallel without stepping on each other’s toes.
  • git log and git checkout let you explore and revert to any previous version.

Happy coding! 🚀

Git reset options – a quick overview

CommandWhat it doesEffect on your work‑tree & index
git reset --soft <commit>Moves HEAD to the specified commit but leaves all changes you made after that commit staged (in the index).If you commit now, the new commit will contain all the changes that were present in the latest commit plus the ones you staged.
git reset --mixed <commit>
(or simply git reset <commit>)
Moves HEAD to the specified commit and unstages any changes that were staged after that point.Your working directory stays unchanged, but the changes are no longer staged. You can edit, stage, and commit them again as you wish.
git reset --hard <commit>Moves HEAD to the specified commit and discards all changes made after that commit, both staged and unstaged.All modifications that were not part of the commit are permanently removed from the working tree. Use with caution!

How to backtrack safely

  1. Identify the commit you want to return to (e.g., via git log or a visual tool).
  2. Choose the appropriate reset mode:
    • Use --soft if you want to keep everything staged for a new commit.
    • Use --mixed if you want to keep the changes but start with a clean index.
    • Use --hard only when you are sure you can discard the later work.
  3. Run the chosen command.

Collaboration tip

When working with others (e.g., on a shared “Todo” repository), avoid rewriting public history:

  • Never git reset --hard on commits that have already been pushed to a shared branch.
  • If you need to undo a public commit, consider using git revert instead; it creates a new commit that undoes the changes without rewriting history.

By understanding these three reset modes, you can confidently navigate between versions of your code while minimizing the risk of accidental data loss or merge conflicts.

Back to Blog

Related posts

Read more »

Understanding Git

!Cover image for Understanding Githttps://media2.dev.to/dynamic/image/width=1000,height=420,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.a...

Git for Beginners

markdown Introduction If you are learning programming or working with code, you will hear the word Git everywhere. Git can feel confusing at first, but once you...