Git adding files to repo
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Adding files to a Git repository really means staging content for the next commit. git add does not push anything to a remote and it does not create the commit by itself. Once you understand the working tree, staging area, and commit flow, the command becomes much easier to use correctly.
Understand the Working Tree and Staging Area
Git has an extra layer between your files and your commit history: the index, usually called the staging area. When you edit a file, the change exists only in the working tree until you stage it.
After git add app.py, the current contents of app.py move into the staging area. They are not committed yet. They are only prepared for the next commit.
That distinction matters because you can keep editing the file after staging it. Git will then show both staged and unstaged changes for the same file.
Add New Files and Modified Files
The same git add command handles both new files and already tracked files.
If the file was untracked, Git starts tracking it. If it was already tracked, Git stages the new content. There is no separate “add new file” versus “update existing file” command.
Choose the Right Form of git add
Specific paths are the safest form.
You can also add broader scopes:
These variants are different:
- '
git add .stages changes below the current directory' - '
git add -Astages additions, modifications, and deletions across the repository' - '
git add -ustages modifications and deletions for already tracked files'
That difference becomes important when files were deleted or when you are working from a subdirectory instead of the repository root.
Use Patch Mode for Clean Commits
One of Git’s best features is patch mode.
This lets you stage selected hunks instead of entire files. It is ideal when one file contains both a bug fix and unrelated cleanup. Staging by hunk keeps commits focused and easier to review.
A good workflow is:
git diff --cached shows exactly what will go into the next commit.
Use .gitignore for Files That Should Never Be Added
Not everything in the working directory belongs in the repository. Build artifacts, local secrets, dependency folders, and logs usually belong in .gitignore.
.gitignore stops new matching files from being staged accidentally. It does not automatically untrack files that were already committed earlier, so that distinction matters too.
Undo a Staging Mistake Without Losing Work
If you staged the wrong file, remove it from the index while keeping the local edits.
Older tutorials often show git reset HEAD app.py. That still works, but git restore --staged is clearer because it describes exactly what is happening.
Common Pitfalls
- Thinking
git addcommits the file instead of merely staging it. - Using
git add .from a subdirectory and assuming the whole repository is staged. - Forgetting that a file edited again after staging now has both staged and unstaged changes.
- Committing generated files, secrets, or local environment data because
.gitignorewas missing or incomplete. - Avoiding
git add -pand ending up with noisy mixed-purpose commits.
Summary
- '
git addstages content for the next commit; it does not create the commit itself.' - The same command stages both new files and modified tracked files.
- '
git add -A,git add ., andgit add -ubehave differently, especially around deletions and current directory scope.' - Patch mode is the best tool for precise, reviewable commits.
- Use
.gitignoreandgit diff --cachedto keep the repository history clean.
Related reading
- git ahead/behind info between master and branch?
- Git alias with positional parameters
- Git and Mercurial - Compare and Contrast
- Git and nasty error cannot lock existing info/refs fatal
- git apply changes from one commit onto another branch
- git apply fails with patch does not apply error
- git archive fatal Operation not supported by protocol
- Git as mercurial client? Why no git-hg?
.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.