How Does The Git Status Command Work?
Being the most popular version control software in the world, it is very important for a developer to understand git well. This is impossible to do without knowing and understanding git commands. One of those essential commands to know is git status.
git status displays the state of your repository. It shows changed files and whether or not they are tracked and staged.
In this article, I will go over, in detail, the git status command, how does it work, as well as explain when and how to use it.
Let’s get to it 😎.
The definition
git status is a git command that shows the status of the working tree. It shows changed files and whether or not each file is tracked and staged.
To run this command, you will need to open a terminal from the location of your git repository and type:
bashgit status
The output of this command will show the status of your directory and of your staging area.
Using this command will not show you any information about the commit history of your repository. You will need to use the git log command for that.
That's all good you may say... But can you show a real-life example?
Of course, I can.
An example using git status
Let's say we have a git repository that we haven't made any changes to.
When we type the git status command in our terminal, the output will show:
bash> git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
When an existing file is modified, but not staged.
Then, let's say we modify an existing file called appController.ts.
The output of that command will be:
bash> git status
On branch master
Your branch is up to date with 'origin/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: server/controllers/appController.ts
no changes added to commit (use "git add" and/or "git commit -a")
In our case, that output shows the modified file and shows us that we haven't staged the modified file.
Without staging, we will not be able to commit our changes to a remote server.
To stage a file, we need to use the git add command.
When an existing file is modified AND staged.
After we have staged the modified file, the output of that command will look like this:
bash> git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: server/controllers/appController.ts
When a new file is added.
Now let's reset and use another example. Let's say that we add a completely new file called personController.ts to our repository. Then we stage our changes.
The output of that command will look like this.
bash> git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: server/controllers/personController.ts
When a file is deleted.
Finally, let's reset and use one final example. Let's say that we delete a file called appController.ts from our repository. Then we stage our changes.
The output of that command will look like this.
bash> git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: server/controllers/appController.ts
What to do if git doesn't show my changes?
In some rare instances, git may not recognize your changes... In that case, typing the git status command will show that you have no changes.
To fix this problem you can either:
- Check the .gitignore file (git ignores all the paths from that file)
- Use git check-ignore -v path/to/file to check why is the file being ignored
- Try refreshing the git indexes with git update-index --really-refresh
- Try using the git rm command git rm --cached path/to/file then git reset path/to/file
One of those solutions will work.
Final thoughts
Here you have it...
Now you understand the git status command and will be able to use it when working on your next coding project.
For my part, most of the time, I use the git status command when I want to see if all the files that I want are being tracked.
I have written more tutorials on git on this website (if you are interested in learning more).
Don’t hesitate to ask your questions in the comments section. I always respond 😉.
Please share this article with your fellow developers, and thank you very much for reading it.