HomeBytesheetsRoadmapsUI TemplatesUI ToolsStack Discussion
4 Topics
Chapter•35 Minutes

Git Fundamentals

Understand version control, Git's architecture, repositories, commits, the staging area, and the basic development workflow.

What Is Git?

Git is a distributed version control system designed to track changes in source code and other files. Unlike a simple backup system, Git records a history of changes so developers can inspect, compare, revert, branch, merge, and collaborate safely.

Git is distributed, which means a cloned repository contains the complete repository history rather than depending on a single central server for normal version-control operations.

A typical workflow is: modify files → inspect changes → stage changes → create a commit → share commits with a remote repository.

Git vs GitHub

Git is the version-control system. GitHub, GitLab, and Bitbucket are services that host Git repositories and provide collaboration features.

Basic Git Workflow
Create Your First Repository
mkdir git-demo
cd git-demo
git init
echo "Hello Git" > README.md
git add README.md
git commit -m "Initial commit"
💡 git init creates a Git repository. git add places a change into the staging area, and git commit permanently records the staged snapshot in the repository history.
Key Takeaways
  • Git tracks changes as a history of commits.
  • Git repositories contain their own local history.
  • The normal workflow moves changes from working directory to staging area and then into a commit.
Knowledge Check

What is Git primarily used for?

Working Directory, Staging Area, and Repository

Git conceptually separates your project into three important areas. The working directory contains the files you are currently editing. The staging area, also called the index, contains changes selected for the next commit. The repository contains committed snapshots.

This separation lets you create focused commits. For example, if you modify three files but only one change is ready, you can stage only that file and commit it.

Git's Three Areas
Stage Selectively
git status
git add src/auth.js
git status
git commit -m "Add authentication validation"
💡 Only src/auth.js is staged, so the commit can contain that logical change without automatically including unrelated modifications.
Key Takeaways
  • Staging is a preparation step, not the same as committing.
  • Use selective staging to keep commits focused and reviewable.
  • The repository contains committed snapshots rather than an editable working copy.
Knowledge Check

Which Git area contains changes selected for the next commit?

Initializing and Inspecting Repositories

Use git init when starting version control in an existing directory. Use git clone when obtaining an existing repository. The git status command is one of the most frequently used Git commands because it explains the current state of tracked, modified, staged, and untracked files.

Use git status Frequently

Running git status before staging, committing, switching branches, or cleaning files prevents many common Git mistakes.

Repository Inspection
git init
git status
git log --oneline
git branch
git remote -v
💡 These commands help establish whether the directory is a repository, what changes exist, what commits are present, which branches exist, and which remote repositories are configured.
Key Takeaways
  • git init creates a repository while git clone copies an existing repository.
  • git status is the primary command for understanding your current working state.
Knowledge Check

Which command creates a new Git repository in the current directory?

Commits and Repository History

A commit represents a recorded snapshot of the project's state. Each commit has an identifier, author information, timestamp, message, parent relationship, and references to the content tree.

Good commit messages describe the intent of the change. Prefer small, logical commits such as Fix token expiration validation instead of vague messages such as changes.

Inspect Commit History
git log
git log --oneline --decorate --graph --all
git show HEAD
git show HEAD~1
💡 git log displays history, while git show displays the details of a particular commit. HEAD~1 refers to the first parent of the current commit.
Key Takeaways
  • Commits form a directed history through parent relationships.
  • Useful commit messages communicate intent.
  • Graph views make branches and merges easier to understand.
Knowledge Check

What does HEAD normally identify?