Git Status: Check the State of Your Repository
What is git status
?
The git status
command shows the current state of your working directory and staging area. It helps you see which files have been modified, staged, or untracked before committing changes.
How to Use git status
1. Basic Usage
Run the following command inside a Git repository:
git status
This will display information about:
- Untracked files (files not yet added to Git).
- Changes not staged for commit (modified but not added to staging).
- Changes staged for commit (files ready to be committed).
- Current branch and tracking status.
Example Scenarios
Scenario 1: No Changes (Clean Working Directory)
If you haven't made any changes, you'll see:
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
This means your working directory and staging area are clean.
Scenario 2: Untracked Files
If you create a new file (newfile.txt
), but haven't added it to Git, running git status
will show:
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
newfile.txt
nothing added to commit but untracked files present
To track this file, run:
git add newfile.txt
Scenario 3: Modified but Not Staged Files
If you modify a tracked file (index.html
), but haven't staged the changes, you'll see:
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
modified: index.html
To stage the file, run:
git add index.html
Scenario 4: Staged Files Ready for Commit
Once you stage files using git add
, running git status
will show:
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: index.html
Now, commit the changes:
git commit -m "Updated index.html"
Scenario 5: Checking Branch Status
If you're working on a feature branch and want to check if it's ahead or behind the remote repository:
On branch feature-branch
Your branch is ahead of 'origin/feature-branch' by 2 commits.
(use "git push" to publish your local commits)
This means you have two new commits that need to be pushed to the remote repository:
git push origin feature-branch
Conclusion
The git status
command is an essential tool to keep track of changes in your repository. Before committing your work, it helps you understand what files are modified, staged, or untracked.
Would you like to add more details or examples? 🚀