Skip to main content

Command Palette

Search for a command to run...

Git for Beginners: Basics and Essential Commands

Learn what is Git and why it is indispensable in modern ecosystems

Published
4 min read
Git for Beginners: Basics and Essential Commands
K

I am a problem solver and a full-stack developer. I love to write and share my knowledge. My wish is to learn and help others learn at the same time. Hoping to make a difference in the tech world.

What is Git

Git is a distributed version control system.

  • Version Control System (VCS) - A tracker which tracks changes and stores them somewhere. Git tracks changes and stores them inside the .git folder.

  • The .git folder has many other roles as well besides just tracking

  • It tracks changes made only on a local system. To track changes globally we need a VCS Remote which would host Git and allow collaboration. e.g. GitHub, Gitlab, Bitbucket, etc.

Basically, Git is nothing but a code tracker that tracks all modifications like changes, updates, creations, deletions and stores all of that in a hidden local directory, without the need of any manual tracking. However, it does not support any collaboration feature and for that we would need a VCS Remote which would host Git in a public domain and allow to usage, i.e. a Single Source of Truth setup.

Why Git is used

  • Tracking - Helps to automate tracking of changes in your project repo.

  • Rollback - Git’s internal design uses linked list architecture, allowing to go back versions if required.

  • Efficient - Git automatically tracks updates without any need of manual intervention, making the process seamless.

Git Basics and Core Terminologies

Now that we know what and why of Git, let’s delve a bit about how. Going forward these are some core jargons that you should know:

  • repository - your current working directory

  • commit - saved changes that have been tracked also known as a snapshot

  • branch - the current working tree you are on, a collection of commits made by u. The default branch as of today is main

  • HEAD - an indicator of the current branch. Analogy - think about the head pointer in a linked list.

  • Staging area - Any file whose changes have been tracked but not saved are said to reside in the staging area.

  • Untracked (U) - Changes have not been tracked yet

  • Modified (M) - Changes have been tracked but more changes have been done after that leading to a mismatch between the last tracked file and the current file.

  • refs - a directory inside .git containing a reference to the HEAD

  • commit_id - a link containing reference to previous commit (just like the address part of a LinkedList)

  • SHA - commit_ids are generally hashed in these algorithms

Common Git Commands

  • git init - Initializes git in your current directory (internally - .git repo is generated)

  • git add <filename> - Tracks changes made and sends your file to the staging area

    • git add . - Stages all files in the current directory
  • git commit -m “message” - Saves the tracked changes

  • git log - Shows history of all previous commits

  • git status - Shows if there is any modification or not

  • git revert <commit_id> - Makes a new commit which does the opposite of the previous commit

  • git reset --hard <commit_id> - Pointer moves to previous commit and latest commit is deleted making previous commit the latest (Use with caution - use git revert if needed)

    • --hard - DO NOT FORGET to add this flag or you might accidentally delete important commits
  • git branch - Shows current branch

Advanced Essential Commands

  • git commit -am “message” - stages and commits all files in the working directory in one go

  • git log --oneline - Shows history in one-line, better readability than original command

  • git cat-file -p <commit_id> - Shows details of that commit. It generally has 3 sections - own commit_id, previous commit’s commit_id, author, etc. Generally used when we need to see the content of a particular commit.

Developer Workflow

This is the whole process through which changes are tracked by Git (simplified)

Local Repository Structure Overview (High Level View)

my-project/
├── .git/              ← the repository (Git’s brain)
│   ├── HEAD
│   ├── config
│   ├── index
│   ├── objects/
│   ├── refs/
│   │   ├── heads/
│   │   └── tags/
│   └── logs/
└── src/
    └── main.c

Commit History Flow

Every commit is basically like nodes in a linked list which contains the address of the previous commit. The commit_id is hashed using SHA algorithms, and every branch is like a separate linked list.

Git is like any other software, but its unique use case makes it an indispensable tool in modern ecosystems. Knowing how to use Git is considered a fundamental skill that any developer should know as you will use it 99% of the time.