Git Status
The git status command shows the current state of your working directory and the staging area. It tells you which files are changed, which are staged and which are not tracked by Git. It is the command you will use most often, so run it as much as you like. It never changes anything.
Syntax
git status
git status --short
git status -sb
Status of a clean repository
$ mkdir demo
$ cd demo
$ git init -q
$ git status
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)
Status with untracked files
$ echo "<h1>Home</h1>" > index.html
$ echo "body { margin: 0; }" > style.css
$ git status
On branch main
No commits yet
Untracked files:
(use "git add <file>…" to include in what will be committed)
index.html
style.css
nothing added to commit but untracked files present (use "git add" to track)
The files are untracked. Git sees them, but is not tracking them. The message also tells you what to do next.
Status after staging and changing
$ git add index.html
$ git status
On branch main
No commits yet
Changes to be committed:
(use "git rm –cached <file>…" to unstage)
new file: index.html
Untracked files:
(use "git add <file>…" to include in what will be committed)
style.css
The file index.html is now under “Changes to be committed” (staged), and style.css is still untracked. Let us commit and change a file again.
$ git commit -m "Add index page"
[main (root-commit) 85826d3] Add index page
1 file changed, 1 insertion(+)
create mode 100644 index.html
$ echo "<p>Welcome</p>" >> index.html
$ git status
On branch main
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)
style.css
no changes added to commit (use "git add" and/or "git commit -a")
Now the file is under “Changes not staged for commit” because we edited it after the commit.
Short format
When the output is too long, use -s or --short. Each line shows two letters and the file name.
$ git status -s
M index.html
?? style.css
| Code | Meaning |
|---|---|
?? | Untracked file |
A | New file added to the staging area |
M | Modified, not staged |
M | Modified and staged |
MM | Staged, and modified again after staging |
D | Deleted and staged |
R | Renamed and staged |
The first letter is for the staging area and the second letter is for the working directory.
Show the branch name too
$ git status -sb
## main
M index.html
?? style.css
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |