The ‘git status’ command: How to check the status of your Git repository

Updated: January 27, 2024 By: Guest Contributor Post a comment

Introduction

Understanding the state of your Git repository is a crucial aspect of using Git efficiently. The ‘git status’ command is your window into the workings of your repo at any given time. This tutorial is an in-depth exploration of the ‘git status’ command coupled with practical examples ranging from basic usage to advanced tricks. Whether you’re a newcomer or an experienced user, mastering git status will streamline your development workflow.

Getting Started: Basic Usage of ‘git status’

When first using ‘git status’, you’ll see one of the simplest and most frequently used commands in action. It provides immediate feedback about the current branch, changes not staged for commit, and files that are not being tracked by Git.

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
	modified:   README.md

Untracked files:
  (use "git add ..." to include in what will be committed)
	notes.txt

no changes added to commit (use "git add" and/or "git commit -a")

Checking the Status in Short Format

When working in a repository with many changes, the output of ‘git status’ can become overwhelming. Using ‘git status -s’ or ‘git status –short’ gives you a compact overview.

$ git status -s
 M README.md
?? notes.txt

The ‘M’ indicates modified files, and the ‘? ?’ indicates untracked files.

Understanding Branch Information

Branch information is essential and ‘git status’ does not hold back. It tells you about the branch you are on and how it relates to the remote counterpart.

$ git status
On branch feature-xyz
Your branch is ahead of 'origin/feature-xyz' by 2 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

Checking the Status of Specific Paths

‘git status’ can focus on specific paths to narrow down the output, which is particularly useful when dealing with large repositories.

$ git status README.md
On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
	modified:   README.md

Seeing the Differences

To understand how files changed, ‘git status’ can be combined with other commands, like ‘git diff’, for an even clearer picture.

$ git status -s
 M README.md
?? notes.txt

$ git diff

This will show the actual line differences in the files listed as modified.

Ignoring Files

Sometimes, you may want certain files to be left untracked. Creating a .gitignore file is how you tell Git to ignore certain files.

# Adding to .gitignore
*.log
build/

‘git status’ will respect these rules and will no longer list the ignored files as untracked.

Advanced Tricks with git status

For the power user, ‘git status’ offers advanced flags that can fine-tune the output or even affect how Git interacts with changes.

$ git status --ignored

This command will show you ignored files as well, which is great for debugging your .gitignore rules.

$ git status --branch

With the –branch flag, it displays the status along with branch information.

$ git status --show-stash

This will indicate if you have anything stashed away with ‘git stash’ which can be often forgotten.

Conclusion

‘git status’ is a versatile tool that adapts to your workflow. Understanding its nuances and combining it with other Git commands makes for a powerful process that informs you about the status of your work, helps you avoid common pitfalls, and ultimately helps you to focus on coding. Embracing ‘git status’ is a step towards Git mastery.