6

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.

gturri
  • 13,807
  • 9
  • 40
  • 57
Tanky Woo
  • 4,906
  • 9
  • 44
  • 75
  • If I understand you correctly, you already have the changes you want to commit on the index? If so a simple `git commit` is sufficient. I will post a more specific answer if you want me to. – Sascha Wolf Jul 15 '14 at 10:09
  • 1
    `git commit` will commit all the file in index, but I only want to commit file a in index, not include file b(in index), and not include file a in working directory. – Tanky Woo Jul 15 '14 at 11:39

2 Answers2

5

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.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • 4
    Only commit specific file `in index`, `git commit file` will commit the changes both in `index` and `working directory`. – Tanky Woo Jul 15 '14 at 11:42
0

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

It sounds that you don't want to track b at all:

git rm --cached b
echo b >> .gitignore
git add .gitignore
git commit -m "Add a, ignore b"

But if you want to track b, just making sure you want add any modification to it, then you can use git update-index:

git reset -- b # will unstage (remove from index)
git update-index --skip-worktree -- b
...
# when ready to add b
git update-index --no-skip-worktree -- b
git add -- b
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I want to track b, and file b is added by another man, so I want to specific the file in `index` to commit, and remain b in the index after commit. – Tanky Woo Jul 15 '14 at 11:40
  • @TankyWoo then the second part of my answer is for you: b remains tracked (not in the index but you don't need it there, unless "another man" is working in the same working tree than you on the same machine) – VonC Jul 15 '14 at 11:42
  • in the second part, first that should reset the changes, and `update-index --skip-worktree` just 'hidden' this file to working tree, and maybe afterwards I need do `--no-skip-worktree`. – Tanky Woo Jul 15 '14 at 11:53
  • I have updated my question, maybe the scenario I asked is a wrong use of git. – Tanky Woo Jul 15 '14 at 11:57
  • @TankyWoo I confirm that the second part will do what your update describes. the reset only un-stage the file (removes it from the index, but keep it in the working tree, with its current modification). When you are ready to add and commit that file, then yes, an `update-index --no-skip-worktree` will be necessary. – VonC Jul 15 '14 at 11:58
  • sorry, it's my mistake, `reset -- file` will remain changes in working directory. Thank you, I learned a new command. But this is a indirect way, I searched and fount no direct way -- just specific file, like `git commit file` – Tanky Woo Jul 15 '14 at 12:03
  • 1
    @TankyWoo indeed, a commit is not for a file, but for the all index content, hence the need to clean that index first. – VonC Jul 15 '14 at 12:04