2

I am having a problem in git repository on selected files commit.

As in SCM or SVN if I have modified 4 files.But want to commit only two then I can easily commit the selected files only.

But in git when using cmd: git commit -a.It is going to commit all modified files. If use cmd like git commit filename filename.It is not working.

So any help how to commit only selected files in git.

2 Answers 2

4

Don't use git commit -a if you don't want to commit everything - since -a is "commit all".

Instead, use git add <filename> to add the files you want to commit, and the just git commit.

For example, if you have three files foo, bar, and baz, and you want to only commit the changes to foo and bar, then do:

git add foo
git add bar
git commit

(You could also do git add foo bar to add both files in a single command.)

Sign up to request clarification or add additional context in comments.

5 Comments

you can even drive this further by using -p instead of adding files manually. You can then add individual hunks (i.e. parts of patches) selectively and even manipulate them before adding. Useful to really split up monster commits logically.
Yeah, but -p can be a little complex for people new to Git. :)
You can add the files only Once using add.Am I correct?I have added 4 files today as A,B,C,D.And commit all.And on next day modify A and C and want to commit only A.Then ?
You need to do git add each time you change a file. (Even if you've just added a file and then modify it again, you still need to git add it; otherwise git will commit the previously added version.)
@iPhoneDev No, git add is not just once per file. You can repeatedly git add a file whenever you need to stage changes to it.
1

Now it is working.

From the git doc:

git commit ... <file> ...​
When files are given on the command line, the command commits the contents of the named files, without recording the changes already staged. The contents of these files are also staged for the next commit on top of what have been staged before.

To commit only some (added!) files do this:

git commit sample_file1.txt sample2.php file3.cs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.