Remove a file from the list that will be committed

123,756

Solution 1

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.

Solution 2

git rm --cached will remove it from the commit set ("un-adding" it); that sounds like what you want.

Solution 3

if you have already pushed your commit then. do

git checkout origin/<remote-branch> <filename>
git commit --amend

AND If you have not pushed the changes on the server you can use

git reset --soft HEAD~1

Solution 4

Use stash; like this:

git add .
git reset Files/I/Want/To/Keep
git stash --keep-index
git commit -a -m "Done!"
git stash pop

If you accidentally commit a file, and want to rewrite your git history, use:

git reset HEAD~1 path/to/file
git commit -a -m "rollback"
git rebase -i HEAD~2

and squash to the two leading commits. You can write a helper script to do either of these if you have a known set of files you prefer not to automatically commit.

Solution 5

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

Share:
123,756
Hanut
Author by

Hanut

Updated on February 15, 2020

Comments

  • Hanut
    Hanut about 4 years

    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.

  • Cascabel
    Cascabel over 13 years
    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.
  • ThR37
    ThR37 over 13 years
    @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...
  • Will Buck
    Will Buck about 11 years
    To set at piece any minds that are nervous, this set of commands will not change your actual code files on disk, it will only take the specified file out of this current commit (which is what was originally asked). Big ups :)
  • Xarses
    Xarses about 11 years
    You should also edit .gitignore and add the path/to/file if you know that you don't ever want it added to the commit
  • Marinos An
    Marinos An about 7 years
    If the file exists in the repo then executing this command and then commit will perform a file removal.
  • Jim L
    Jim L about 6 years
    Based on the commentary on the question, this looks like something different than the OP actually wants to do. The question wasn't really clear on that.
  • Nicole Finnie
    Nicole Finnie about 6 years
    This is the right answer to this question. The best answer didn't help my problem. I had to remove the file from the commit list by issuing git rm --cached <file>
  • hlitz
    hlitz over 5 years
    did not work for me. The removed file does not appear when I do git diff. It removes the file!!