What states can a file have in Git?
Show answer — try answering out loud first
A file can be untracked (Git isn't tracking it yet), modified (it changed relative to the last commit but isn't staged), staged (prepared for the next commit with git add), or committed (already saved in the repository, with no pending changes). A file moves through these states as you edit it and commit it.
Think of a file's life cycle:
- Untracked: the file is new; Git sees it but isn't versioning it. It appears when you've just created it and never added it.
- Modified: Git already knew the file, but you edited it since the last commit. The change exists in the working directory, but isn't staged yet.
- Staged: you ran
git addand the file (or its change) is ready to go into the next commit. - Committed: the file is saved in the repository and matches the last commit; it has no pending changes. It's usually called "tracked and unmodified."
git status tells you the state of each file, with colors: red for what isn't staged, green for what is.
# You create a new file -> it becomes UNTRACKED
echo "hola" > nota.txt
git status
# "Untracked files: nota.txt"
# You add it -> it becomes STAGED
git add nota.txt
git status
# "Changes to be committed: new file: nota.txt"
# You commit it -> it becomes COMMITTED
git commit -m "Agrega nota"
# You edit it again -> it becomes MODIFIED
echo "otra linea" >> nota.txt
git status
# "Changes not staged for commit: modified: nota.txt"
Confusing untracked with modified. An untracked file is completely new (Git has never saved it); a modified file is one Git already knew about and that changed. It's also common not to realize that a file can be both "staged" (for one change) and "modified" (for another new change) if you edit it after running git add.
"A file can be untracked, when Git isn't tracking it yet; modified, when I edited it since the last commit but haven't staged it; staged, when I already ran
git addand it's ready for the next commit; and committed, when it's already saved in the repository with no pending changes. Withgit statusI can see the state of each file at any time."
You run git add archivo.txt and then edit it again without running another add. What state is it in?
See answer
It's in two states at once. The version you staged with git add is still staged, but the new changes you made afterward are modified (unstaged). If you run git commit, only the version that was staged is saved; the later changes stay pending until a new git add.