Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 including files which weren't tracked, which I would also like to avoid. I have changed the title of the question accordingly.

like image 440
Pedro d'Aquino Avatar asked Feb 02 '11 11:02

Pedro d'Aquino


People also ask

How do I commit just changed files?

Enter git add --all at the command line prompt in your local project directory to add the files or changes to the repository. Enter git status to see the changes to be committed. Enter git commit -m '<commit_message>' at the command line to commit new files/changes to the local repository.

How do I commit without adding files?

Without adding any files, the command git commit won't work. Git only looks to the staging area to find out what to commit. Staging, or adding, files, is possible through the command line, and also possible with most Git interfaces like GitHub Desktop by selecting the lines or files that you'd like to stage.

How do you git add all modified files except untracked?

The easiest way to add all files to your Git repository is to use the “git add” command followed by the “-A” option for “all”. In this case, the new (or untracked), deleted and modified files will be added to your Git staging area. We also say that they will be staged.


2 Answers

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

like image 184
tokarev Avatar answered Sep 29 '22 03:09

tokarev


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

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

like image 21
Pedro d'Aquino Avatar answered Sep 29 '22 05:09

Pedro d'Aquino