Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to commit specific file in index?

Tags:

git

Example:

» git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   a
    modified:   b

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   a

If I have modified a file named a, and add parts of it to index, and there is a file named b also in index.

I want to commit only file a in index, neither include b nor a in work directory.

The way I use is to use git commit -p and enter n to not stage. or git commit -p file and choose the part I want.

But if the part in file a already add in index, is there a better way to direct commit file a only in index part?


Supplement:

I think maybe that is wrong use of git.

Only the changes to be commited in the next commit, should add in the index.

Obvious the file b is not prepared to be commited, so it thould in the working directory.

like image 533
Tanky Woo Avatar asked Oct 01 '22 08:10

Tanky Woo


1 Answers

git commit a would commit only a and leave b staged.

Here's an example from the man page :

$ edit hello.c hello.h
$ git add hello.c hello.h
$ edit Makefile
$ git commit Makefile

This makes a commit that records the modification to Makefile. The changes staged for hello.c and hello.h are not included in the resulting commit. However, their changes are not lost — they are still staged and merely held back.

like image 146
Eran Avatar answered Oct 05 '22 22:10

Eran