Stage & Commit Files: git add, git commit, & git log
Master Git Version Control with Essential Commands
This guide covers the essential Git workflow commands for staging, committing, and tracking changes in your codebase. Perfect for developers new to version control or looking to solidify their Git fundamentals.
Git Workflow Components
Working Directory
Your local files where you make changes. Files here are either tracked or untracked by Git.
Staging Area
A preparation zone where you select which changes to include in your next commit.
Repository
The committed history of your project stored in the .git directory.
Checking Repository Status
Navigate to Repository
Use terminal to navigate to your Git repository folder using the cd command
Run Status Command
Execute 'git status' to see current branch and file statuses
Interpret Results
Review which files are untracked, modified, or deleted and ready for staging
Git Add Command Options
| Feature | Command | Purpose |
|---|---|---|
| git add . | Stage all files | Quick staging for all changes |
| git add filename | Stage specific file | Selective staging control |
| git add foldername | Stage entire folder | Organize by directory |
Use 'git reset HEAD filename' to unstage accidentally staged files. This removes files from staging area while keeping your working directory changes intact.
File Deletion Methods
| Feature | Method | Steps Required |
|---|---|---|
| Manual Delete + Git Add | Delete file manually | Two-step process |
| Git RM Command | Delete and stage together | One-step process |
Write commit messages in imperative mood like 'Make headings blue' rather than past tense like 'Made headings blue'. This tells collaborators what your code will do when merged.
Making a Commit
Execute Commit
Run 'git commit -m "Your descriptive message"' to commit staged changes
Verify Clean State
Check status again to confirm all changes are committed successfully
Confirm Clean Tree
Look for 'nothing to commit, working tree clean' message indicating success
The 'git commit --amend' command modifies the last commit. Only use this for commits that haven't been pushed to shared repositories, as it rewrites Git history.
Git Log Command Options
| Feature | Command | Information Displayed |
|---|---|---|
| git log --oneline | Simplified view | Commit hash and message |
| git log | Standard detail | Author, date, full message |
| git log --stat | Maximum detail | Files changed and statistics |
When viewing long commit histories, use Arrow keys to scroll up and down, and press 'Q' to quit the log view and return to your command prompt.
Key Takeaways