git add only modified changes and ignore untracked files
Solution 1
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
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
To stage modified and deleted files
git add -u
Solution 4
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
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
Steve
Updated on July 28, 2022Comments
-
Steve almost 2 years
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")
-
BenKoshy over 8 yearsif 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.
-
-
Steve almost 13 yearsI 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.
-
Zach Lysobey over 11 yearspoint of interest, this (
add -u
) doesn't add onlymodified
files, it also "adds"deleted
ones.. something I'm currently trying to prevent. -
maaartinus over 10 years@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. -
Krøllebølle almost 10 yearsTo 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. -
Samuel about 9 yearsNo 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 about 9 yearsYou 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.
-
user877329 almost 9 years@Samuel Grouping is nice when testing the expression. Then I can print the match inside square brackets to see that I am right.
-
Nick Volynkin almost 9 years@Ярослав your solution adds modified and untracked files and is equal to
git add -u
, so it doesn't answer the question. -
DMart over 8 years--modified seems to include deleted as well as just modified
-
DMart over 8 yearsgit diff-files -z --diff-filter=M --name-only | xargs -0 git add --dry-run seems to work well.
-
Kirby about 8 yearsYou may interested in adding already added files list:
git diff --name-only --cached | xargs git add
-
Orwellophile almost 8 yearsn.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 almost 8 years@user877329 - You can loose the
grep
before thesed
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** -
Tomáš Zato over 7 yearsHow should exactly your gitignore hide untracked files?
-
user877329 over 7 years@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.
-
Choylton B. Higginbottom almost 7 yearsThis could be achieved in one step:
git status | grep modified | awk '{print $2}' | xargs git add && git status
-
Mike Q almost 7 yearsYeah I should I have provided that solution as well, thanks, I updated the post.
-
Hugo over 6 years
git add -u
is short forgit add --update
andgit commit -a
is short forgit commit --all
-
GhostCat about 5 yearsThis definitely does not work when using --amend.
-
Grijesh Chauhan over 3 yearsI used to do this
git status -uno -s | cut -d ' ' -f 3 | xargs git add
:joy -
Asef Hossini over 2 yearsOr simply
git commit -am "message"
. -
DannyS almost 2 yearsWell this is wrong advice! "git clean -xffd" will remove untracked directories and files completely, you can't call that "a way to ignore". What you really should do in this case is just git add -u to add only previously staged/modified/deleted files.