git add only modified changes and ignore untracked files

GitCommitGitignoreStaging

Git Problem Overview


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")

Git Solutions


Solution 1 - Git

Ideally your .gitignore should prevent the untracked (and ignored) files from being shown in status, added using git add etc. So I would ask you to correct your .gitignore

You can do git add -u so that it will stage the modified and deleted files.

You can also do git commit -a to commit only the modified and deleted files.

Note that if you have Git of version before 2.0 and used git add ., then you would need to use git add -u . (See "Difference of “git add -A” and “git add .").

Solution 2 - Git

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

Solution 3 - Git

To stage modified and deleted files

git add -u

Solution 4 - Git

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

Solution 5 - Git

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

Solution 6 - Git

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

Solution 7 - Git

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

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

Solution 8 - Git

> I want to put the modified files in staging so I can commit them

TL;DR : START

Use interactive mode (git add -i), select update (type 2 or u in What now> prompt), select all(type * in Update>> prompt), exit interactive mode(type 7 or q in What now> prompt) .

TL;DR : END

For the more patient:---

STEP 1

You could use the interactive mode.

git add --interactive 

or the short-hand form,

git add -i

This will give a list of all modified files along with their status, plus a bunch of command options you could use, like:

           staged     unstaged path
  1:    unchanged       +17/-0 package-lock.json
  2:    unchanged        +2/-0 package.json
  3:    unchanged       +2/-28 src/App.js
  4:    unchanged        +7/-6 src/App.test.js

*** Commands ***
  1: status       2: update       3: revert       4: add untracked
  5: patch        6: diff         7: quit         8: help
What now>

STEP 2

For your case, type u or 2 against What now>,

What now>u

You get below,

           staged     unstaged path
  1:    unchanged       +17/-0 package-lock.json
  2:    unchanged        +2/-0 package.json
  3:    unchanged       +2/-28 src/App.js
  4:    unchanged        +7/-6 src/App.test.js
Update>> *

STEP 3

In the Update>> prompt, type * to add all the files of the above list to the staging area(you could well add individual files too by providing the comma separated numbers),

Update>> *

At this point of time, all of your modified files have been added to the staging area and you can verify the status by typing 1 or s in the What now> prompt(or just exit the interactive mode by typing q or 7 and run the usual git status command),

What now> 1

which will show you the status,

           staged     unstaged path
  1:       +17/-0      nothing package-lock.json
  2:        +2/-0      nothing package.json
  3:       +2/-28      nothing src/App.js
  4:        +7/-6      nothing src/App.test.js

We can see here that the unstaged column displays the text "nothing" for all the files of the list.

For the second part of question regarding ignoring the untracked files, I believe this SO answer will help a lot.

Solution 9 - Git

one way to ignore is using git clean -xffd, which will clean the untracked files. then you can use git add -A

Solution 10 - Git

git commit -am "your message here" this worked for me

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSteveView Question on Stackoverflow
Solution 1 - GitmanojldsView Answer on Stackoverflow
Solution 2 - Gituser877329View Answer on Stackoverflow
Solution 3 - GitDayamoyView Answer on Stackoverflow
Solution 4 - GitSaurabhView Answer on Stackoverflow
Solution 5 - GitMike QView Answer on Stackoverflow
Solution 6 - GitKarl BielefeldtView Answer on Stackoverflow
Solution 7 - GitErik EnglundView Answer on Stackoverflow
Solution 8 - GitAsif Kamran MalickView Answer on Stackoverflow
Solution 9 - GitNithya View Answer on Stackoverflow
Solution 10 - GitConcepticioView Answer on Stackoverflow