Sling Academy
Home/DevOps/The ‘git status’ command: How to check the status of your Git repository

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

Last updated: January 27, 2024

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.

Next Article: How to add a file to the staging area in Git

Previous Article: The ‘git init’ command: How to create a new Git repository

Series: Git & GitHub Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide