Mastering basic Git commands is essential for working efficiently with version control.
In this chapter, you will learn how to stage files, create commits, inspect repository status, and explore commit history.
Understanding the Git Workflow
Git operations typically follow this three-step workflow:
- Modify files in your Working Directory
- Stage the changes using git add
- Save the changes using git commit
These commands form the foundation of all Git usage.
Checking the Status of Your Repository
The git status command shows what is happening inside your project:
git statusExample output:
On branch main
Changes not staged for commit:
modified: index.htmlStaging Files with git add
Stage a single file:
git add index.htmlStage multiple files:
git add file1.js file2.cssStage all modified files:
git add .Unstage a file (remove it from staging area):
git restore --staged index.htmlCreating Commits with git commit
A commit is a snapshot of your project.
Create a simple commit:
git commit -m "Add homepage layout"Commit with a detailed message:
git commit -m "Fix login bug" -m "Resolved issue with token validation and improved error handling"Amend the previous commit:
git commit --amendViewing Commit History with git log
Show full commit history:
git logTypical output:
commit a3f1c9d2b1e4
Author: Your Name <your.email@example.com>
Date: Tue Jan 12 14:22:01 2025
Add header componentCompact view:
git log --onelineGraph view:
git log --oneline --graph --allSkipping the Staging Area (Optional)
Commit tracked changes without staging:
git commit -am "Update styles"Notes:
- Only works for tracked files
- New files still require git add
Removing Files with git rm
Delete a tracked file:
git rm oldfile.jsRemove file from Git but keep it locally:
git rm --cached settings.jsonSummary
In this chapter, you learned:
- How to check repository status
- How to stage and unstage files
- How to create commits
- How to view commit history
- How to modify or skip the staging step
- How to remove files using Git
These commands are used every day by developers and form the backbone of all Git workflows.
Next, we will explore branching in Git and learn how to create, switch, and merge branches effectively.