tags:

views:

62

answers:

3

Hello, I have a list of changed files in git repository. There is one file I don't wanna commit for the current moment. Can I do:

git commit -a

To commit all files and then somehow remove that file from current commit? After such removing it should still be in the list of uncommited files.

A: 

You could stage (git add) all the files, unstage (git rm --cached) the one you don't want and then commit.

git add -u .
git rm --cached <path to the file to be unstaged>
git commit
Miikka
No. This will not do what you want. This will commit the file as a deletion.
Jefromi
+6  A: 

You want to do this:

git add -u
git reset HEAD path/to/file
git commit

Be sure and do this from the top level of the repo; add -u adds changes in the current directory (recursively).

The key line tells git to reset the version of the given path in the index (the staging area for the commit) to the version from HEAD (the currently checked-out commit).

And advance warning of a gotcha for others reading this: add -u stages all modifications, but doesn't add untracked files. This is the same as what commit -a does. If you want to add untracked files too, use add . to recursively add everything.

Jefromi
A: 

Maybe you could also use stash to store temporaly your modifications in a patch file and then reapply it (after a checkout to come back to the old version). This could be related to this other topic : How would I extract a single file (or changes to a file) from a git stash?.

Elenaher
`git stash` is indeed handy, but it's way more complicated than `reset HEAD <path>` to use it for this case (not exactly what it's designed for). No need to bother with it, or patches.
Jefromi
@Jefromi Yes you're probably right but it's always useful to know about alternative ways (and the linked topic is well-answered so...). I should maybe have given the link in a comment...
Elenaher