Search code examples
gitgit-commit

git commit only modified files as atomic commit, then commit only deleted files as separate atomic commit


I have a mix of deleted, new and modified files in my git staging area, i.e. they are already all added to the staging area and are ready to be finally committed to the repository.

How do I commit file by 'status' instead of using a pathspec?

Example, how do I commit (from the staging area) only the files that have been deleted, but leave new and modified files alone, in staging?

The reason is because I want to create separate atomic commits with separate comments that are more exact/applicable to the files being committed.


Solution

  • If possible, I would:

    • reset the index (no more staged files)
    • add them by status, then commit

    You can add by status (as shown here) with:

    git add --all $(git diff --diff-filter=D --name-only)
    

    D is for deleted. Use other filters, like A for added or M for modified.