878

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")
Steve
  • 11,831
  • 14
  • 51
  • 63
  • 2
    if you've added the .gitignore file AFTER you've tracked files then the .gitignore file will not ignore files which are already being tracked. that could be an issue. – BenKoshy Mar 16 '16 at 01:06

9 Answers9

1177

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

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 109
    point of interest, this (`add -u`) doesn't add __only__ `modified` files, it also "adds" `deleted` ones.. something I'm currently trying to prevent. – Zach Lysobey Jan 16 '13 at 21:44
  • 7
    To only add modified files, I usually go top the top directory of my repo and type `for fil in $(git diff --name-only --relative); do git add $fil; done`. If I were to use it a lot (I don't), I would just make an alias for this in my `~/.bashrc` file. This does, of course, only work in bash. – Krøllebølle Aug 20 '14 at 16:46
  • 11
    No definite answer, only "works-for-most-people" answers? I have to use another process to do this correctly? How is this not built into git add? It seems like such a common thing to want to do. – Samuel Mar 30 '15 at 21:48
  • 1
    You may interested in adding already added files list: `git diff --name-only --cached | xargs git add` – Kirby Jun 21 '16 at 10:11
  • 2
    *n.b.* the methods mentioned in these comments will fail if you have odd filenames (spaces, asterisks, what not). To properly escape any strangely named files, use: `IFS=$(echo -en "\n\b"); for file in $(git diff --name-only); do git add "$file"; done` – Orwellophile Aug 24 '16 at 02:52
  • 1
    How should exactly your gitignore hide untracked files? – Tomáš Zato Jan 16 '17 at 22:52
  • 8
    `git add -u` is short for `git add --update` and `git commit -a` is short for `git commit --all` – Hugo Oct 06 '17 at 07:26
  • What if I don't want to add untracked files. Just because I changed something locally doesn't mean I want to change it on remote. – Ben Alan May 03 '23 at 19:22
128

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
user877329
  • 6,717
  • 8
  • 46
  • 88
  • You don't need grouping (\(\)) if you are not going to reuse the group, and \s left whitespace in front of the filename for me. Not that it will affect the end result in this case, but for sake of setting an example here's the sed command I used: sed 's/.*modified: *//'. Verified on Mac OS X 10.9.5. – Samuel Mar 30 '15 at 22:09
  • @Samuel Grouping is nice when testing the expression. Then I can print the match inside square brackets to see that I am right. – user877329 Jul 25 '15 at 08:30
  • 1
    @Ярослав your solution adds modified and untracked files and is equal to `git add -u`, so it doesn't answer the question. – Nick Volynkin Jul 28 '15 at 19:16
  • 8
    --modified seems to include deleted as well as just modified – DMart Dec 09 '15 at 17:42
  • 2
    git diff-files -z --diff-filter=M --name-only | xargs -0 git add --dry-run seems to work well. – DMart Dec 09 '15 at 17:42
  • @user877329 - You can loose the `grep` before the `sed` by only printing lines that match: `git ... | sed -n 's/.*modified:\s*//p' _(and I found it educational to see the grouping, I didn't realise they were the same as vim's default regex)_.**Tested on OS X and Cygwin** – Orwellophile Aug 24 '16 at 02:59
  • @TomášZato It requires Bash, which is available for Windows, either through Cygwin, or GNUWin32. However, the expansion of the sedpipe may result in a longer command line than Windows can support (due to API limitations). Also, it should be noted that the command will fail if any filename contains whitespace, on any platform. – user877329 Jan 17 '17 at 12:25
  • I used to do this `git status -uno -s | cut -d ' ' -f 3 | xargs git add` :joy – Grijesh Chauhan Dec 17 '20 at 18:13
  • if the file name is `deleted` then it shouldnt work? – Chau Giang Nov 09 '22 at 07:11
  • @DMart - Caution: `git diff-files -z --diff-filter=M --name-only` puts NUL between file names; this may not play well what you pipe it to. – Rick James May 02 '23 at 14:51
48

To stage modified and deleted files:

git add -u

Where -u is short for --update.

Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119
Dayamoy
  • 560
  • 4
  • 7
19
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

Saurabh
  • 7,525
  • 4
  • 45
  • 46
9

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
Mike Q
  • 6,716
  • 5
  • 55
  • 62
  • 2
    This could be achieved in one step: `git status | grep modified | awk '{print $2}' | xargs git add && git status` – Choylton B. Higginbottom Jul 27 '17 at 23:55
  • Yeah I should I have provided that solution as well, thanks, I updated the post. – Mike Q Jul 28 '17 at 13:32
  • Best answer IMO. Allows for targeted addition, scoured the `git-add` man-page without anything that could achieve this in a simple manner like suggested in this answer. Cheers! – Meuko Oct 14 '22 at 07:26
5

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.

Asif Kamran Malick
  • 2,409
  • 3
  • 25
  • 47
4

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
Karl Bielefeldt
  • 47,314
  • 10
  • 60
  • 94
  • 4
    I was using my .gitignore all wrong. I had an empty .gitignore file in every directory that I wanted to ignore instead of having a single gitignore with contents in it. – Steve Aug 19 '11 at 18:43
  • 1
    @Steve: This would work if each .`gitognore` contained a start (ignore everything). But a single `.gitignore` in the top directory is usually much simpler to use and suffices. – maaartinus Jan 23 '14 at 08:01
1

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.

Erik Englund
  • 355
  • 2
  • 11
-4

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