The Git Workflow
Understanding the Git workflow is crucial for using Git effectively. Let's break down how Git manages your files.
The Three States
Git has three main states for your files:
1. Modified
Files that have been changed but not staged:
bash
1# Check which files are modified
2git status
3
4# Output:
5Changes not staged for commit:
6 modified: index.html
7 modified: style.css2. Staged
Files that are marked to go into the next commit:
bash
1# Stage a file
2git add index.html
3
4# Now it's staged
5Changes to be committed:
6 modified: index.html3. Committed
Files that are safely stored in your local database:
bash
1# Commit staged files
2git commit -m "Updated homepage"
3
4# File is now committed
5[main a1b2c3d] Updated homepage
6 1 file changed, 10 insertions(+)Visual Workflow
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Working │ │ Staging │ │ Local │
│ Directory │────▶│ Area │────▶│ Repository │
│ │ │ (Index) │ │ │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │ │
│ git add │ git commit │
│ │ │
│ │ │
│◀──────────────────────┴───────────────────────│
│ git checkout │
Basic Workflow Commands
Step 1: Check Status
Always start by checking the status:
bash
1git statusStep 2: Stage Changes
Add files to the staging area:
bash
1# Stage a specific file
2git add filename.txt
3
4# Stage all changed files
5git add .
6
7# Stage all files of a type
8git add *.jsStep 3: Commit Changes
Save your staged changes:
bash
1git commit -m "Your descriptive message"Step 4: Push to Remote
Share your changes with others:
bash
1git push origin mainA Complete Example
Let's walk through a typical workflow:
bash
1# 1. Create a new file
2echo "Hello World" > hello.txt
3
4# 2. Check status
5git status
6# Output: Untracked files: hello.txt
7
8# 3. Stage the file
9git add hello.txt
10
11# 4. Check status again
12git status
13# Output: Changes to be committed: new file: hello.txt
14
15# 5. Commit the file
16git commit -m "Add hello.txt with greeting"
17# Output: [main a1b2c3d] Add hello.txt with greeting
18
19# 6. Push to remote
20git push origin main
21# Output: Successfully pushed to origin/mainThe .git Directory
When you initialize a Git repository, a .git folder is created:
project/
├── .git/ ← Git's database (hidden)
│ ├── objects/ ← Your commits and files
│ ├── refs/ ← Branches and tags
│ ├── HEAD ← Current branch pointer
│ └── config ← Repository settings
├── index.html
├── style.css
└── app.js
Workflow Best Practices
1. Commit Often
Make small, frequent commits:
bash
1# Good: Small, focused commits
2git commit -m "Add header component"
3git commit -m "Style header with CSS"
4git commit -m "Add navigation links"
5
6# Bad: Large, unfocused commits
7git commit -m "Add header, navigation, footer, and styling"2. Write Good Commit Messages
bash
1# Good commit messages
2git commit -m "Fix login button not responding on mobile"
3git commit -m "Add password validation to signup form"
4
5# Bad commit messages
6git commit -m "fix"
7git commit -m "updates"3. Check Status Before Committing
bash
1# Always review what you're about to commit
2git status
3git diff --stagedSummary
The Git workflow consists of:
- Working Directory - Where you edit files
- Staging Area - Where you prepare commits
- Repository - Where commits are stored
Master this workflow, and you'll be comfortable with Git in no time!
