Git
Version Control
Code Management
Modified Files
Untracked Files

git add only modified changes and ignore untracked files

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

In the world of version control, git is an indispensable tool for managing code changes across various stages of project development. Among the numerous functionalities it offers, understanding how to selectively stage modifications—adding only those changes you want to include in your next commit while ignoring others—is crucial. This article will focus on using git add to stage only modified changes, leaving untracked files untouched.

Understanding the Working Tree, Index, and Untracked Files

Before diving into the specifics of git add, it's important to understand the three main concepts in Git:

  1. Working Tree: The working directory where you are currently working. It holds all the current changes in files.
  2. Index (Staging Area): The area that holds a snapshot of the content of files as they are going to be committed. Changes moved here are what will be included in the next commit.
  3. Untracked Files: These are files in your working directory that were not in the last snapshot and are not in the staging area.

When modifications occur in your working tree, they can either be:

  • Changes to files that were previously tracked (modified files)
  • Newly created files (untracked files)

Using git add to Stage Modified Changes

The command git add is pivotal in Git. It updates the index using current content found in the working tree, to prepare the content staged for the next commit.

To stage only modified and deleted files, ignoring any untracked files, you can use the following command:

bash
git add -u

Here, the -u option stands for "update". This tells Git to automatically stage already tracked files that have been modified or deleted, but it does not add any new files that have never been tracked before.

Example:

Consider a repository with the following files:

  1. README.md (tracked and modified)
  2. LICENSE (tracked and unmodified)
  3. new_feature.rb (untracked file)

Running git add -u will stage README.md if it's modified but it won't stage new_feature.rb.

Why Choose git add -u?

Choosing to add only modified changes can be crucial for several reasons:

  • Focused Commits: It allows you to make commits that are focused only on changes meant to address a specific issue or feature, rather than mixing them with unrelated changes.
  • Prevent Errors: It helps in preventing the accidental addition of files that are not meant to be tracked by the repository.

Table: Summary of git add Options

OptionFunctionality
git add .Adds all changes in the directory, including new files.
git add -uUpdates the index with only the modifications (ignores new files).
git add -AAdds all changes (modifications, new files, and deletions).

Best Practices for Using git add

When using git add, consider these practices:

  • Routinely use git status to check the state of your working tree and index.
  • Consider using git add -p for partial adds, which allows for even more granular control by letting you stage chunks of changes.

Conclusion

Understanding how to interact efficiently with Git's staging area can significantly boost your productivity and error management in software development. Using git add -u allows you to focus on your current changes without mixing them with new, possibly unreviewed files. This results in cleaner commits and a more organized development workflow, essential for projects of any scale.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.