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 is the version-control system. GitHub, GitLab, and Bitbucket are services that host Git repositories and provide collaboration features.
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.- 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.
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 status
git add src/auth.js
git status
git commit -m "Add authentication validation"src/auth.js is staged, so the commit can contain that logical change without automatically including unrelated modifications.- 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.
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.
Running git status before staging, committing, switching branches, or cleaning files prevents many common Git mistakes.
git init
git status
git log --oneline
git branch
git remote -vgit initcreates a repository whilegit clonecopies an existing repository.git statusis the primary command for understanding your current working state.
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.
git log
git log --oneline --decorate --graph --all
git show HEAD
git show HEAD~1git log displays history, while git show displays the details of a particular commit. HEAD~1 refers to the first parent of the current commit.- Commits form a directed history through parent relationships.
- Useful commit messages communicate intent.
- Graph views make branches and merges easier to understand.
What does HEAD normally identify?