2 Control staging changes before committing #
3 git add #
- What it does : Stages entire files.
- Use case : When you're confident that you want to stage all changes in a file.
git add file
3 git add -p (or --patch) #
- What it does: Allows you to interactively stage parts (hunks) of files.
- Use case: When you want to commit only some of the changes in a file (e.g., splitting a large file change into multiple meaningful commits).
git add -p
Go through each change (hunk) and ask:
Stage this hunk [y,n,q,a,d,s,e,?]?
Choose to:
y: stage this hunkn: do not stage this hunks: split hunk into smaller pieces (if possible)e: manually edit the hunk before staginga: stage this hunk and all remaining onesq: quit?: help
3 When use which? #
| Situation | git add |
git add -p |
|---|---|---|
| Staging whole files w/o needing fine control | ✓ | ❌ |
| Stage only specific lines or sections of a file | ❌ | ✓ |
| Crafting clean, focused commits | ❌ | ✓ |
3 Example #
Suppose you've made multiple unrelated changes in app.py:
- Fixed a bug in one function
- Added a print statement for debugging
- Refactored another part
Using git add app.py will stage everything, mixing them into one commit.
Using git add -p lets you select only the bug fix, leaving the rest for separate commits.
.
2 How "big" should a commit be? #
Make commits a clean, complete step not big nor small - just logically focused.
Sample commit history for a small feature implementation:
Eg a feature: that allows users to update their name, bio, ...
3 Example Commit History #
feat(profile): create basic user profile page layout
# Add empty HTML structure and basic styling for user profile page. No logic yet.
feat(profile): fetch and display current user data
# Hooks up backend to frontend: loads user’s preofile datusing API
feat(profile): add form for editing profile info
# Add input fields and submit button. Includes controlled form components
feat(profile): implement saveProfile API call
# Add API integration to send profile updates to server
fix(profile): prevent form submission with invalid data
# Add validation: name required, bio max 160 chars, avatar must be image URL
refactor(profile): extract ProfileForm into separate component
# Cleans up logic by moving form into its own reusable component
test(profile): add tests for profile update logic
# Add unit tests for API integration and form validation
style(profile): improve spacing and avatar preview style
# Minor visual tweaks. Purely cosmetic changes
3 Why this works #
Each commit:
- Has a clear purpose (add, fix, refactor, test, style, etc.)
- Can be read and understood independently
- Could potentially be reverted on its own if needed
- Helps reviewers follow your thought process
last updated:
