开发者

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

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 includ开发者_运维技巧ing files which weren't tracked, which I would also like to avoid. I have changed the title of the question accordingly.


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


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

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


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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜