Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to commit one file at a time using Git?

Tags:

git

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
like image 777
Scud Avatar asked Jan 05 '10 01:01

Scud


People also ask

How do I commit a single file in git?

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 you add an individual file for a commit?

Save the file or files. Add only one file, or one part of the changed file: git add README.md. Commit the first set of changes: git commit -m "update the README to include links to contributing guide" Add another file, or another part of the changed file: git add CONTRIBUTING.md.


3 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.

like image 78
daf Avatar answered Oct 09 '22 10:10

daf


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

...
like image 42
Alex Reisner Avatar answered Oct 09 '22 10:10

Alex Reisner


git add -p described by daf's answer is nice, but for a more direct approach to picking and choosing, I'm really liking:

git add -e

It generates the appropriate patch, then loads it up in your preferred editor so that you can edit it as desired. When you save the file and exit the editor, and only the changes made by your edited version of the patch are added to the index.

If you accidentally close the editor without making changes, you'll probably want to use get reset HEAD and then start over.

like image 14
phils Avatar answered Oct 09 '22 11:10

phils