How to commit only modified (and not new or deleted) files?
GitVersion ControlGit 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])
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
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
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.