Git: Add only modified / deleted files and ignore new files ( i.e. Untracked files ) using “git add -u”

In this article we will discuss how to add only modified & deleted files to the git and ignore all new files and folders.

Sometimes we encounter a situation where we add, modify, and delete some files locally in the project. But we want to update the git index by adding only modified & deleted files to the staging area and then commit only those files to repository. Basically, we want to ignore new files or folders i.e. un-tracked files.

Command to skip new files / folders while adding other files to git

$ git add -u <pathspec>

Or

$ git add --update <pathspec>

This command updates the index by adding the changes for those files only about which git already know i.e. changes in tacked files only. Therefore, it will add only modified & deleted files from the current project to the staging area and will skip all new files / folders.

<pathspec> represents the files to add content from. It can be,

  • A specific file e.g git add -u src/untility.cc
  • A specific folder (All files/ sub folders under this folder will be included) e.g git add -u src/
  • Current folder and All files/ sub folders under current folder e.g. git add -u .

If is not specified, then it will update the index of whole project only i.e.

$ git add -u

Will add modified & deleted files in whole project to the staging area and will skip new files / folders.

Let’s understand some examples,

Example of ignoring new files / folders while adding other files to git

We are in our project folder and let’s check the git status of project

$ git status

Output

On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   src/mainfile.cc
        deleted:    src/utility.cc

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        README.md
        test/

no changes added to commit (use "git add" and/or "git commit -a")

Git status shows that we have following changes waiting to be added to the staging area,

  • New file and folder (i.e. untracked files / folder)
  • A tracked file (build.xml) is modified i.e. src/mainfile.cc
  • A tracked file is deleted i.e. src/utility.cc

Now we want to add new modified and deleted only to the git staging area of the project and then commit them. Basically, we want to ignore all the new files & folders. For that we will execute this command,

$ git add -u

It updates the git index by adding changes only about modification & deletion of tracked files in the project to the staging area. It skips all new files in the project.

We can confirm this by checking the git status,

$ git status

Output:

On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   src/mainfile.cc
        deleted:    src/utility.cc

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        README.md
        test/

It shows that except new files all other changes like modified & deleted files are added to the staging area.
Now we can commit the staged changes i.e.

$ git commit -m "Adding only modified & deleted files."

Output:

[master 1de27aa] Adding only modified & deleted files.
 2 files changed, 3 insertions(+), 1 deletion(-)
 delete mode 100644 src/utility.cc

Now to confirm that files have been successfully commited or not, we can check the logs also,

$ git log

Output:

commit 1de27aa6fe2067ab8f41f0359cdb35d1dd679715 (HEAD -> master)
Author: Varun
Date:   Sat Jun 6 15:39:55 2020 +0530

    Adding only modified & deleted files.

New files are still not tracked by git after updating the index using “git add -u” command and we can confirm this by checking status

$ git status

Output:

On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        README.md
        test/

nothing added to commit but untracked files present (use "git add" to track)

So, this is how we can add only modified and deleted files from the project to git and skip new files.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top