git status Command


git status Command

The git status command is used to display the state of the working directory and the staging area. It helps you understand which changes are staged for the next commit, which are not staged, and if there are any untracked files in your project.


What Does git status Show?

When you run git status, Git provides a detailed summary of:

  1. Staged Changes: Files that have been added to the staging area using git add. These changes are ready to be committed.
  2. Unstaged Changes: Files that have been modified but not yet added to the staging area. These are changes in the working directory.
  3. Untracked Files: Files that are present in your working directory but are not being tracked by Git. These files have not been added to the repository.
  4. Branch Information: The current branch you are on and whether it is ahead or behind the remote branch (if applicable).
  5. Other Messages: Any additional information, such as whether there are conflicts that need to be resolved after a merge.

Syntax

git status

This command doesn’t take any arguments, and simply running it will display the status of your repository.


Example Output of git status

Let's go through a typical output of git status and explain each part.

On branch main Your branch is up to date with 'origin/main'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: file1.txt new file: file2.txt Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: file3.txt Untracked files: (use "git add <file>..." to include in what will be committed) file4.txt

Explanation of Each Section:

  1. Branch Information:

    On branch main Your branch is up to date with 'origin/main'.
    • This tells you that you're on the main branch and that your local branch is synchronized with the origin/main branch (i.e., the remote branch).
    • If your branch is ahead or behind the remote branch, Git will indicate this.
  2. Changes to Be Committed:

    Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: file1.txt new file: file2.txt
    • These files have been added to the staging area with git add and are ready to be committed.
    • file1.txt has been modified, and file2.txt is a newly created file.
    • Git provides a helpful tip here: you can unstage these files with git reset HEAD <file> if you change your mind.
  3. Changes Not Staged for Commit:

    Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: file3.txt
    • These files have been modified in the working directory but have not yet been staged. If you want these changes to be part of the next commit, you need to run git add.
    • If you want to discard these changes, you can run git restore <file> to revert them.
  4. Untracked Files:

    Untracked files: (use "git add <file>..." to include in what will be committed) file4.txt
    • These are files that exist in the working directory but are not tracked by Git. They haven’t been added to the repository.
    • You can add these files with git add if you want them to be tracked in the next commit.

Practical Use Cases for git status

  1. Check the Current State Before Committing: After making changes to your project, use git status to see which files are staged, unstaged, or untracked. This ensures you commit exactly what you intend to.

    git status
  2. Track Unstaged Changes: If you've modified some files but haven’t added them to the staging area, git status will show them under "Changes not staged for commit."

    git status
  3. Find Untracked Files: When you create new files in your working directory, Git won’t track them unless you explicitly tell it to with git add. Running git status will show untracked files so you can add them to the repository.

    git status
  4. Verify Branch and Commit State: git status displays which branch you’re on and whether your local branch is ahead or behind the remote branch. This is useful when you want to know if you need to push or pull changes.


Summary

  • Purpose: git status shows the current state of your working directory and the staging area.
  • Displays:
    • Staged changes (ready to be committed).
    • Unstaged changes (modified but not yet staged).
    • Untracked files (not yet tracked by Git).
    • Branch information and potential remote changes.
  • Usage: Use it frequently to understand what will be included in the next commit, what still needs to be staged, and whether there are untracked files you may want to add.

Running git status frequently is a good practice to keep track of your changes and avoid unintended commits.