How to commit only modified (and not new or deleted) files?

GitVersion Control

Git Problem Overview


git status shows a bunch of files which were modified and some which were deleted. I want to first commit the modified files and then the deleted ones. I don't see any option in git add that enables me to do this. How can I do it?

EDIT: As pointed out, git add wouldn't have staged the deleted files anyway, so git add . would do. But it has the side-effect of including files which weren't tracked, which I would also like to avoid. I have changed the title of the question accordingly.

Git Solutions


Solution 1 - Git

The following command should do the trick:

git commit -a

or

git commit -am "commit message"

From the Pro Git book:

> Providing the -a option to the git commit command makes Git automatically stage every file that is already tracked before doing the commit

Solution 2 - Git

git diff --name-only --diff-filter=M | xargs git add

(based on [Charles Bailey's answer on a related question][1])

[1]: https://stackoverflow.com/questions/2121452/with-git-how-do-i-commit-a-load-of-deleted-files-in-one-go/2121476#2121476)

Solution 3 - Git

You could use:

git add -u

to stage already tracked files that have been modified since last commit.

From git-add man page:

> -u
> --update > > Only match against already tracked files in the index rather than the working tree. That means that it will never stage new files, but that it will stage modified new contents of tracked files and that it will remove files from the index if the corresponding files in the working tree have been removed. > If no is given, default to "."; in other words, update all tracked files in the current directory and its subdirectories.

Not sure when this feature has been added though.

Solution 4 - Git

I may be missing something but git add doesn't include deleted files, you have to use git rm to remove those:

mkdir git-test
cd git-test
git init
touch a
touch b
touch c
git add .
git commit -m "Initial"
echo "a" > a
echo "b" > b
rm c
git status

# On branch master
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   a
#       modified:   b
#       deleted:    c
#

git add .

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   a
#       modified:   b
#
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       deleted:    c
#

git commit -m "Changed"
git status

# On branch master
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       deleted:    c
#

git rm c
git commit -m "Deleted"

And git log shows three commits.

Solution 5 - Git

I would be careful with using git commit -a -- unless you have made sure that your .gitignore file has ALL files that you do NOT want added there

I have used git commit -a many times in areas where this was not the case, and I ended up having to clean up / delete temporary files etc. from git repository

Solution 6 - Git

git status On branch master Your branch is up to date with 'origin/master'.

Changes not staged for enter here commit: (use "git add ..enter here." to update what will be committed) (use "git restore …" to discard changes in working directory) modified: src/components/Header.js

no changes added to commit (use "git add" and/or "git commit -a")

If we try to add particular modified file only then using git add "Modified file" will throw an error $ git add Header.js fatal: path spec 'Header.js' did not match any files

So I used this and pushed Git add -u // To update the modified file.

git commit -m "Header.js".

git push

This worked fine 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
QuestionPedro d&#39;AquinoView Question on Stackoverflow
Solution 1 - GittokarevView Answer on Stackoverflow
Solution 2 - GitPedro d'AquinoView Answer on Stackoverflow
Solution 3 - Gitjln_wlflsbrgView Answer on Stackoverflow
Solution 4 - GitMakisView Answer on Stackoverflow
Solution 5 - GitserupView Answer on Stackoverflow
Solution 6 - GitVishnu Karthik MadupuriView Answer on Stackoverflow