2. Basic Git Commands: init, add, commit, status, log
Git Fundamentals: init, add, commit, status, log
The basic Git commands are the foundation of version control. Understanding these commands and their underlying mechanics is essential for every developer. This module covers the most frequently used Git commands and what happens when you run them. These are the commands you'll use daily, and understanding them deeply is the first step to Git mastery.
The key insight is that Git operates in three states: Working Directory (untracked files), Staging Area (index), and Repository (committed history). Files move through these states as you work. Understanding these states is essential for understanding Git. This module covers the commands that move files between these states. The staging area is unique to Git and is one of its most powerful features — it lets you build commits incrementally, staging only the changes you want to include.
You'll learn about git init (create a repository), git add (stage changes), git commit (save changes), git status (check state), and git log (view history). Each command is explained with what happens under the hood, common patterns, and best practices. By the end of this module, you'll be comfortable with the basic Git workflow and understand what's happening at each step.
The Three States of Git
Git tracks files in three states: Working Directory (modified), Staging Area (staged), and Repository (committed). Files start in the Working Directory — this is where you edit files. When you run git add, files move to the Staging Area — they're ready to be committed. When you run git commit, files move to the Repository — they're permanently saved. This three-state model is central to understanding Git. The Working Directory is your current set of files. The Staging Area is a snapshot of what will be in the next commit. The Repository is the permanent history of committed changes.
Understanding these states is crucial. The Working Directory contains the files you're currently working on. The Staging Area is a temporary snapshot before committing. The Repository is the permanent history. The git status command shows you which state your files are in. This is your compass for navigating Git. When you run git status, Git tells you exactly what's going on: which files are untracked, which are staged, and which are modified but not staged. Learning to read git status output is like learning to read a map — it shows you where you are and what you can do next.
When you initialize a repository, you create a .git directory that holds all of Git's objects. Initially, the repository is empty. As you add and commit files, Git creates blobs, trees, and commits, building the commit graph. The interactive visualizer above shows this process in real-time. When you stage a file, Git creates a blob object. When you commit, Git creates a tree (representing the directory structure) and a commit object that points to that tree. This is the foundation of Git's object model.
Git Status Output Explained
git status is your most important command. It tells you exactly what's happening in your repository. Understanding its output is essential for effective Git usage. The status output shows you which files are untracked, modified, staged, or deleted. It also shows you the state of your branch — whether you're ahead or behind the remote. It even gives you hints about what to do next. Learn to read git status output like a second language.
The status output has several sections. Untracked files shows files Git doesn't know about. These are new files in your working directory that haven't been added. Changes not staged for commit shows files you've modified but haven't added to the staging area. Changes to be committed' shows files in the staging area, ready to be committed. Understanding these sections makes it easy to know what to do next. If you see Your branch is ahead of origin/main, you have commits that haven't been pushed. If you see Your branch is up to date, you're synced with the remote.
- Untracked files: Files Git doesn't know about. Use git add to track them.
- Changes not staged: Files you've modified but haven't added. Use git add to stage.
- Changes to be committed: Files in the staging area. Use git commit to save.
- Your branch is ahead of origin/main: You have commits that haven't been pushed.
- Your branch is up to date: No changes to push or pull.
Step-by-Step: Basic Git Workflow
Follow these steps to practice the complete basic Git workflow. This is the workflow you'll use every day as a developer.
Step 1: Initialize a repositorymkdir project-democd project-demogit init
This creates a new directory and initializes an empty Git repository. The output should say Initialized empty Git repository.
Step 2: Create a fileecho <strong><code>My Project > index.htmlecho <strong><code>body { background: #f0f0f0; } > style.css
Create two files. You can use any text editor instead of echo.
Step 3: Check statusgit status
You'll see both files listed as Untracked files in red. Git knows they exist but isn't tracking them yet.
Step 4: Stage the filesgit add index.html style.css
This stages both files. Run git status again — both files are now in green under Changes to be committed.
Step 5: Commitgit commit -m <strong><code>Add initial HTML and CSS files
This creates your first commit. Run git log --oneline to see the commit hash and message.
Step 6: Make a changeecho "<h1>Welcome</h1>" >> index.html
Add more content to index.html. Run git status — you'll see index.html under Changes not staged for commit in red.
Step 7: Stage and commit the changegit add index.htmlgit commit -m <strong><code>Add welcome heading
This creates your second commit. Run git log to see both commits in your history.
Step 8: View your commit historygit log --oneline --graph
This shows a visual representation of your commit history. You'll see a simple line with your two commits.
Try It Yourself: Create a third file called app.js, add some content, stage it, and commit it. Then run git log --oneline --graph to see all three commits.
Knowledge Check
Ready to test your understanding of 2. Basic Git Commands: init, add, commit, status, log?