Using the Git Status Command (with examples)
The git status
command is used to show the changes to files in a Git repository. It lists the changed, added, and deleted files compared to the currently checked-out commit. In this article, we will explore different use cases of the git status
command using code examples.
Use Case 1: Show Changed Files Not Yet Added for Commit
Code:
git status
Motivation: This use case is helpful when you want to see the changes you have made to your files that are not yet staged for commit. It gives you an overview of the modifications you have made since your last commit.
Explanation:
By simply running git status
without any additional options or arguments, you can see the changes you have made in your working directory. The output will show the modified files, untracked files, and any other relevant information.
Example Output:
On branch master
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: index.html
Untracked files:
(use "git add <file>..." to include in what will be committed)
newfile.txt
no changes added to commit (use "git add" and/or "git commit -a")
Use Case 2: Give Output in Short Format
Code:
git status --short
Motivation:
Sometimes you want a more concise and readable output from the git status
command. By using the --short
option, you can see the changes in a simplified format, which can be useful when you need a quick overview.
Explanation:
The --short
option is used to display the output in a shorter and more focused format. It provides a concise summary of the changes, showing only the status codes and file paths.
Example Output:
M index.html
?? newfile.txt
Use Case 3: Show Branch and Tracking Info
Code:
git status --branch
Motivation: When working with multiple branches in Git, it is essential to know which branch you are currently on and whether your branch is tracking a remote branch. This use case provides you with the branch and tracking information along with the status of your files.
Explanation:
The --branch
option is used to display the branch and tracking information in the output of git status
. It shows the current branch, whether it is ahead or behind the remote branch, and any upstream branch being tracked.
Example Output:
On branch feature/branch-name
Your branch is ahead of 'origin/branch-name' by 3 commits.
(use "git push" to publish your local commits)
Use Case 4: Show Output in Short Format with Branch Info
Code:
git status --short --branch
Motivation:
Combining the --short
and --branch
options gives you a neat and summarized view of the branch with its tracking information, along with the status of your files. This can be useful when you want a concise yet comprehensive overview.
Explanation:
By combining the --short
and --branch
options, you can get a single command that displays both the shorter format and the branch information. This allows you to quickly see the status of your files and the branch you are currently on.
Example Output:
## feature/branch-name...origin/branch-name [ahead 3]
M index.html
?? newfile.txt
Use Case 5: Show Number of Stashed Entries
Code:
git status --show-stash
Motivation:
When working with stashes in Git, it can be helpful to know how many entries are currently stashed away. This use case provides you with the count of stashed entries in the output of git status
.
Explanation:
The --show-stash
option is used to display the count of stashed entries in the output of git status
. It gives you an indication of how many stashes you have made that are not yet applied.
Example Output:
On branch master
Your branch is up to date with 'origin/master'.
On branch feature/branch-name
Your branch is ahead of 'origin/branch-name' by 3 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: index.html
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: script.js
modified: styles.css
Untracked files:
(use "git add <file>..." to include in what will be committed)
newfile.txt
Stashes:
stash@{0}: On feature/branch-name: Version 1.0
stash@{1}: On feature/branch-name: Version 2.0
Use Case 6: Don’t Show Untracked Files in Output
Code:
git status --untracked-files=no
Motivation:
In some cases, you may not want to see the untracked files in the output of git status
. This use case allows you to exclude the untracked files from the displayed information.
Explanation:
The --untracked-files
option is used to control the display of untracked files in the output of git status
. By setting the value to no
, the untracked files will not be shown in the output.
Example Output:
On branch master
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: index.html
Conclusion
In this article, we explored various use cases of the git status
command with code examples. We covered how to show changed files not yet added for commit, give output in short format, show branch and tracking info, show output in short format with branch info, show the number of stashed entries, and exclude untracked files from the output. By understanding and utilizing these different options, you can effectively monitor the status of your Git repository and manage your changes.