Git

Git Add Only Modified Changes and Ignore Untracked Files

On Git, multiple developers work together, and occasionally, they encounter situations where it is required to add, modify, or delete some files from the local repository. However, it is necessary to ignore untracked files and simultaneously commit staged files to the Git repository. In this situation, the “$ git add -u” command can be used to add modified changes and ignore untracked files.

This article will demonstrate how Git adds only modified changes and ignores untracked new files.

How Does Git Add Only Modified Changes and Ignore Untracked Files?

To add modified changes and ignore untracked files, first, move to the Git local repository. Next, create a file and move it to the staging index. After that, create another file and do not track it to the staging area. Lastly, add modified files to the staging area, skip untracked files with the help of the “$ git add -u” command, and commit the staged changes.

Let’s move ahead and implement the above-discussed scenario for better understanding!

Step 1: Open Git Bash Terminal

Launch the Git terminal using the Windows Start menu:

Step 2: Go to Git Repository

Use the following command and navigate to the required Git repository:

$ cd "C:\Git"

Step 3: Initialize Git Repository

Then, to initialize the current repository by running the following command:

$ git init

Step 4: Create a File

Now, create a new text file through the “touch” command:

$ touch myFile.txt

Step 5: Add File to the Staging Area

After that, add the untracked file to the Git staging area:

$ git add myFile.txt

Step 6: Create Another File

Similarly, generate another text file by using the same procedure:

$ touch myFile2.txt

Step 7: Verify the Git Status

To view the tracked and untracked files, check the Git status through the provided command:

$ git status

According to the below-listed output, we have one tracked file which is ready to commit and the other one is the untracked file:

Step 8: Track Modified File

To add only modified files to the staging area and skip new or untracked files, run the “git add” command with the “-u” option:

$ git add -u

Step 9: Commit Changes

Execute the below-provided command to commit the staged changes:

$ git commit -m "Add only modified files"

Step 10: Check the Git Status

To verify if the untracked file is added to the Git repository or not, check the Git status:

$ git status

As you can see, the untracked file is not added to the Git repository and exists in the working directory:

That’s all! We have compiled the easiest method of adding only modified changes and ignoring untracked files.

Conclusion

To add only modified changes and ignore untracked files, first, go to the Git local repository. Next, create a file and add it to the staging index. After that, create another file and do not add it to the staging area. Then, add modified files to the staging area, ignore unstaged files with the help of the “$ git add -u” command, and commit the staged changes. This article demonstrated the procedure of adding only modified changes and ignoring untracked files.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.