GIT list of new/modified/deleted files
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Listing new, modified, and deleted files in Git sounds simple until you realize Git keeps multiple states at the same time. A file can be modified but unstaged, staged with a different change, or untracked and not yet part of history. The key to accurate output is choosing the exact comparison you want and using commands designed for that state instead of hoping one command means "everything."
Understand the Three Core States
Before running commands, map your question to one of these states:
- Working tree: files currently on disk.
- Index: what is staged for the next commit.
HEAD: the latest commit on the current branch.
If you skip this step, you get confusing results, especially in automation. For example, git diff compares working tree to index by default, so it does not include staged changes. git diff --cached compares index to HEAD, so it does not include unstaged edits.
A quick state review:
Use git status --short for a compact human overview. The two-column status markers show staged and unstaged state separately.
Get New, Modified, and Deleted Files Reliably
If you need all change types, use --name-status with the right comparison target.
Status codes you usually care about:
AaddedMmodifiedDdeletedRrenamedCcopied
When generating release checks, do not ignore renames by default. A rename may be represented as delete plus add if rename detection is disabled or thresholds are not met.
You can filter by type directly:
Include Untracked Files Correctly
git diff does not show untracked files. If your definition of new files includes untracked paths, use status output.
In porcelain output, lines beginning with ?? are untracked files. For scripts, this is safer than parsing human-readable git status output.
Example parse logic in shell:
This approach keeps source state explicit and works well in CI hooks.
If filenames can contain spaces or unusual characters, prefer null-delimited output modes where available and parse carefully. Path handling bugs are common in repository automation even when the Git command itself is correct.
Compare Against a Commit, Branch, or Tag
Sometimes you need a file list for deployment between two refs, not local edits.
Use merge-base comparison for feature branch reporting. It avoids noise from unrelated commits pulled into the branch after rebases or merges.
Practical Workflow Pattern
A stable workflow for teams is:
- Use
git status --shortfor quick local visibility. - Use
git diff --name-statusfor unstaged checks. - Use
git diff --cached --name-statusbefore commit. - Use ref-to-ref
git difffor release or deployment audits.
That pattern prevents accidental omissions and makes script behavior predictable.
Common Pitfalls
A frequent mistake is assuming one command can represent all states in one accurate list. Another is forgetting untracked files when checking what is new, because diff commands do not show them. Teams also break automation by parsing localized or human-formatted status output instead of porcelain output. Rename handling is another source of confusion when audits treat rename as delete plus add. Finally, running commands from nested paths can produce relative names that scripts mis-handle, so normalize paths when needed.
Summary
- Decide first whether you need working tree, index, or ref-to-ref comparison.
- Use
git diff --name-statusandgit diff --cached --name-statusfor precise state views. - Use
git status --porcelainto include untracked files safely. - Filter with
--diff-filterwhen policies depend on change type. - Prefer merge-base or explicit ref ranges for branch and release reporting.
- Keep parsing rules deterministic in scripts to avoid CI surprises.
Related reading
- Git list only untracked files also, custom commands
- git log --follow, the gitpython way
- Git log to get commits only for a specific branch
- git log to return only the commits made to the master branch?
- Git merge branch into master
- Git merge errors
- Git merge from someone else's fork
- Git merge hotfix branch into feature branch
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.