How to Properly Remove Multiple Deleted Files in a Git Repository

Dec 04, 2025 · Programming · 9 views · 7.8

Keywords: Git | file deletion | git add -u

Abstract: This article explains how to correctly remove deleted files from a remote Git repository after local deletion. The primary solution is using the git add -u command to stage all changes, followed by commit and push. It addresses the issue where git status shows deletions as unstaged, provides insights into how git add -u works, and helps developers manage Git repositories efficiently.

Problem Description

When using Git for version control, users may encounter a scenario where multiple files are deleted locally, but running the git status command marks these files as "Changes not staged for commit", with output showing "deleted:". For example, the status output might look like:

# On branch master
# Changes not staged for commit:
#   deleted:    file1.txt
#   deleted:    file2.txt

Users may have attempted to execute git commit and git push, but the remote repository (e.g., GitHub) still lists these deleted files, causing inconsistency. This occurs because the deletions are not staged in Git, so they are not included in subsequent commits.

Solution

Based on best practices, it is recommended to use the git add -u command to resolve this issue. git add -u is short for git add --update, and it automatically stages all modifications and deletions of tracked files in the working tree. The steps are as follows:

  1. Run git add -u in the command line. This command scans the working tree and stages all changes, including deletions, for tracked files.
  2. Then, use git commit -m "Describe changes" to commit the staged changes. The commit message should clearly indicate the deletion.
  3. Finally, run git push to push the changes to the remote repository, ensuring the deletion takes effect remotely.

Through this process, the deleted files are completely removed from the remote repository, synchronizing local and remote states.

Technical Details

The core function of git add -u is to update the Git index to match the current state of the working tree. It only handles tracked files and does nothing for new files (i.e., untracked files). This differs from git add . or git add -A, which add all changes, including new files and deletions.

In the user's case, since files have been physically deleted, git add -u efficiently stages these deletions without needing to specify file paths individually, as with git rm <file>. This simplifies batch processing and reduces the risk of errors.

To further illustrate, here is a code example demonstrating the use of git add -u:

# Assume files file1.txt and file2.txt have been deleted in the current directory
git add -u
git status  # Verify deletions are staged
git commit -m "Remove deleted files"
git push

In this example, git add -u automatically detects and stages all deletions, making subsequent operations smooth.

Conclusion

Mastering the use of git add -u not only solves specific file deletion issues but also enhances the efficiency of Git workflows. Through this explanation, developers should understand its principles and apply it flexibly in real projects to ensure consistency and reliability in version control.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.