How to commit one file at a time using Git?

Viewed 27153

Say there are several files modified and I just want one files committed each time. How to do that? Given an example with status looks like below. Thanks!!

Ex:

> git status
#   modified: file01.txt
#   modified: file02.txt
#   modified: file03.txt
#   modified: file04.txt
#   modified: file05.txt
5 Answers

Use git add -p followed by git commit. git add -p asks you for each change whether to stage it for committing, which is super convenient.

You can also use -p with git reset and git checkout.

There are a few ways, but the simplest is probably:

git commit file01.txt
git commit file02.txt
...

Any paths you list after the commit command will be committed regardless of whether they're staged to be committed. Alternatively, you can stage each one, and then commit:

git add file01.txt
git commit

git add file02.txt
git commit

...
git add file01.txt
git stash
git commit -m'commit message'
git stash pop

repeat.

Related