Staging
Before making any commit, you must explicitly tell Git which files to include—whether they're new untracked files, modified existing files, or deleted files. This deliberate process is called staging and relies on the powerful add command. This selective approach isn't bureaucratic overhead; it's intelligent version control design. Consider a common scenario: you're simultaneously refactoring a complex component and fixing an unrelated bug. The bug fix is ready for production, but the refactoring needs more work. Git's staging area lets you commit the bug fix immediately while keeping the incomplete refactoring changes in your working directory. We add files to a staging area, then commit only what has been explicitly staged. Even file deletions must be tracked in Git's history—after all, removing the wrong file can be just as consequential as adding buggy code.
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.
Check Status
Understanding your repository's current state is fundamental to effective Git workflow. Let's examine what's happening in your working directory.
1. In your terminal (Terminal, Git Bash, or Windows Command Prompt), navigate to the folder that contains your Git repository. 2. Enter this command:
git status
3. You'll see your current branch (likely main for repositories created in recent years, or master for older ones) and the status of all files: untracked files that Git doesn't know about, modified files that have changed since your last commit, and deleted files that have been removed from your working directory. We'll explore branching strategies in detail later, but for now, focus on understanding your file states.
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
Stage Files to Prepare for Commit
Now that you understand your repository's current state, let's prepare your changes for commit. Staging gives you granular control over exactly what gets included in your commit history.
1. Enter one of the following commands, depending on your specific needs:
- Stage all files: git add .
- Stage a specific file: git add example.html (replace example.html with your actual file name)
- Stage an entire folder: git add myfolder (replace myfolder with your folder path)
Professional considerations:
- When file names or paths contain spaces, wrap them in quotes: git add "my file.html"
- You can stage multiple items by repeating these commands or listing multiple files: git add file1.html file2.css
- Use git add -p for interactive staging, allowing you to stage specific portions of files—invaluable when you've made multiple logical changes to a single file
2. Verify your staging actions by checking the status again:
git status
3. You should now see files listed under "Changes to be committed"—these are your staged changes, ready for the next commit.
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 |
Unstage a File
Mistakes happen, and Git provides clean ways to correct them. If you accidentally stage something that shouldn't be in this commit, you can unstage it without losing your changes.
git reset HEAD example.html (replace example.html with your specific file or folder)
This command moves the file back to the "Changes not staged for commit" section, giving you the opportunity to stage it properly later or include it in a different commit entirely.
Use 'git reset HEAD filename' to unstage accidentally staged files. This removes files from staging area while keeping your working directory changes intact.
Deleting Files
File deletion in Git requires the same intentional approach as file addition. When you delete files using your operating system's file manager or command line, Git detects these deletions and shows them as "deleted" in git status. You must then stage these deletions using git add to include them in your next commit.
For a more streamlined approach, Git provides the rm command, which both deletes files from your working directory and stages the deletion in a single operation:
- git rm example.html to remove a file and stage the deletion
- git rm -r myfolder to recursively remove a folder and all its contents, staging everything for deletion
This approach ensures your working directory and staging area remain synchronized, reducing the chance of inconsistencies in your commit history.
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 |
Commit Files
With your changes properly staged, you're ready to create a permanent snapshot in your project's history. Every commit should represent a logical, complete unit of work.
1. Create your commit with a descriptive message:
git commit -m "Message that describes what this change accomplishes"
Professional tip: Write commit messages in the imperative mood—"Fix user authentication bug" rather than "Fixed user authentication bug." This convention aligns with Git's own automatically generated commit messages and creates consistency across your project history. Think of each commit message as completing the sentence "If applied, this commit will..." When working in team environments, this approach makes code reviews and project archaeology significantly more effective.
2. Confirm your commit was successful by checking the repository status:
git status
3. A clean repository with no pending changes will display: nothing to commit, working tree clean.
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
Fixing Your Last Commit Message
Even experienced developers occasionally write unclear or incorrect commit messages. Git's amend feature lets you correct your most recent commit message without creating additional history clutter.
git commit --amend -m "Put your corrected message here"
Important: Only amend commits that haven't been pushed to shared repositories. Amending commits that others have already pulled will create conflicts and complicate your team's workflow.
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.
View a List of Commits
Understanding your project's evolution requires examining its commit history. Git provides several commands for viewing commits, each offering different levels of detail depending on your current needs.
- For a concise overview of recent commits, use:
- git log --oneline
- For detailed commit information including author, date, and full messages:
- git log
NOTE: When viewing lengthy commit histories, use the Down/Up Arrow keys to navigate and press Q to return to your command prompt.
- For comprehensive information including which specific files changed in each commit:
- git log --stat
NOTE: This detailed view is particularly useful during code reviews or when tracking down when specific changes were introduced. Use Down/Up Arrow keys to scroll and Q to quit.
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.
Go Beyond Git
Mastering Git is just one component of modern software development expertise. Our comprehensive curriculum prepares professionals for today's demanding technical landscape through hands-on projects and expert instruction.