Git Basics | git status
The git status command
git status is the command that shows the state of the working directory and the staged snapshot. It displays staged changes, unstaged changes, and files that are not tracked by Git. This status output does not include information about committed history. Use git log when you need to view committed history.
Usage
git status
Lists staged files, unstaged files, and untracked files.
Notes
git status is a relatively simple command. It only checks the result of running git add and git commit. The status information also shows details about staged and unstaged files. The sample output below shows the three main categories displayed when git status is run:
# On branch master
# Changes to be committed:
# (use "git reset HEAD ..." to unstage)
#
# modified: hello.py
#
# Changes not staged for commit:
# (use "git add ..." to update what will be committed)
# (use "git checkout -- ..." to discard changes in working directory)
#
# modified: main.py
#
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
# hello.pyc
Excluding files from output with .gitignore
There are usually two types of untracked files. The first is a file that has just been added to the project and has never been staged. The second is a compiled binary file with an extension such as .pyc, .obj, or .exe. It is useful for the former to appear in git status, but the latter can make it harder to understand the working directory state.
For this reason, Git lets you create a special file path named .gitignore to exclude files from display. Files to exclude should be written one per line, and the * character can be used as a wildcard. For example, add the following line to the .gitignore file at the project root to exclude compiled Python modules from git status output:
*.pyc
Example
Checking the repository state before committing changes is a good habit because it helps prevent unintended commits. This example shows the repository state before and after staging a snapshot and before and after committing it:
# Edit hello.py
git status
# hello.py is listed under "Changes not staged for commit"
git add hello.py
git status
# hello.py is listed under "Changes to be committed"
git commit
git status
# nothing to commit (working directory clean)
The first status output shows the file as unstaged. The result of running git add is reflected in the second git status, and the final status output shows that no files remain to be committed. In other words, the working directory state matches the result of the immediately previous commit. Some Git commands cannot be used unless the working directory is clean, such as git merge, to prevent accidental overwriting of changes.