[git] git add only modified changes and ignore untracked files

I ran "git status" and listed below are some files that were modified/or under the heading "changes not staged for commit". It also listed some untracked files that I want to ignore (I have a ".gitignore" file in these directories).

I want to put the modified files in staging so I can commit them. When I ran "git add .", it added the modified files AND the files I want to ignore to staging.

How do I add only the modified files and ignore the untracked files if presented with the git status below.

Also, are my ".gitignore" files working properly?

$ git status
# On branch addLocation
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   someProject/path/domain/viewer/LocationDO.java
#       modified:   someProject/path/service/ld/LdService.java
#       modified:   someProject/path/service/ld/LdServiceImpl.java
#       modified:   someProject/path/web/jsf/viewer/LocationFormAction.java
#       modified:   someProject/war/WEB-INF/classes/message/viewer/viewer.properties
#       modified:   someProject/war/page/viewer/searchForm.xhtml
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       .metadata/
#       someProject/build/
no changes added to commit (use "git add" and/or "git commit -a")

This question is related to git commit gitignore staging

The answer is


To stage modified and deleted files

git add -u


This worked for me:

#!/bin/bash

git add `git status | grep modified | sed 's/\(.*modified:\s*\)//'`

Or even better:

$ git ls-files --modified | xargs git add

I happened to try this so I could see the list of files first:

git status | grep "modified:" | awk '{print "git add  " $2}' > file.sh

cat ./file.sh

execute:

chmod a+x file.sh
./file.sh 

Edit: (see comments) This could be achieved in one step:

git status | grep "modified:" | awk '{print $2}' | xargs git add && git status

git commit -a -m "message"

-a : Includes all currently changed/deleted files in this commit. Keep in mind, however, that untracked (new) files are not included.

-m : Sets the commit's message


You didn't say what's currently your .gitignore, but a .gitignore with the following contents in your root directory should do the trick.

.metadata
build

Not sure if this is a feature or a bug but this worked for us:

git commit '' -m "Message"

Note the empty file list ''. Git interprets this to commit all modified tracked files, even if they are not staged, and ignore untracked files.


Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to commit

git: updates were rejected because the remote contains work that you do not have locally How to add multiple files to Git at the same time Why Git is not allowing me to commit even after configuration? Git commit in terminal opens VIM, but can't get back to terminal How to configure Git post commit hook GIT commit as different user without email / or only email Editing the git commit message in GitHub How to amend a commit without changing commit message (reusing the previous one)? Temporarily switch working copy to a specific Git commit git - Your branch is ahead of 'origin/master' by 1 commit

Examples related to gitignore

Gitignore not working How to add files/folders to .gitignore in IntelliJ IDEA? Apply .gitignore on an existing repository already tracking large number of files Ignore .classpath and .project from Git .gitignore all the .DS_Store files in every folder and subfolder Correctly ignore all files recursively under a specific folder except for a specific file type What should be in my .gitignore for an Android Studio project? Commit empty folder structure (with git) How to remove files that are listed in the .gitignore but still on the repository? What to gitignore from the .idea folder?

Examples related to staging

git add only modified changes and ignore untracked files How do I remove a single file from the staging area (undo git add)?