52

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.

jscs
  • 63,694
  • 13
  • 151
  • 195
Max Frai
  • 61,946
  • 78
  • 197
  • 306
  • Do you want to remove the file in the commit you are making or just not commit the changes that you have to that file yet? – CB Bailey Aug 25 '10 at 10:01
  • @charles-bailey I want add it to the commit and then remove from it. – Max Frai Aug 25 '10 at 10:20
  • That didn't answer my question, at least I'm not sure exactly what you mean. You said that you "don't wanna commit for the current moment". Does that mean that you just don't want to commit any changes to the file in the next commit, or you actively want the next commit to delete the files and you're then going to re-add it back in a subsequent commit? – CB Bailey Aug 25 '10 at 11:23
  • @charles-bailey first: just don't want to commit any changes to the file in the next commit. But I don't want to add all another files by hand. So I want to add all of them. And after that just remove some file from next commit. – Max Frai Aug 25 '10 at 11:55
  • If you use @ followed by my actual name (not a mistype), I get notified when you direct a comment at me. OK, I think that you want to add all files except one to your next commit. You shouldn't have to remove the one file from the commit because you didn't actually want to add it in the first place. – CB Bailey Aug 25 '10 at 12:54
  • 4
    I found this question googling for a different problem... I think the title is slightly misleading, maybe "Omit a file from being committed" would be better (you haven't committed yet so you're not removing it from a commit, which is what I need to do!) – Anentropic Sep 04 '12 at 15:11

10 Answers10

65

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.

Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • 10
    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 :) – Will Buck Mar 11 '13 at 21:14
  • 3
    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 – Xarses Mar 29 '13 at 20:22
44

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

Nilesh
  • 20,521
  • 16
  • 92
  • 148
DavidR
  • 449
  • 4
  • 2
  • 1
    If the file exists in the repo then executing this command and then commit will perform a file removal. – Marinos An Feb 21 '17 at 16:39
  • 1
    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 ` – Nicole Finnie Apr 10 '18 at 08:42
  • 1
    did not work for me. The removed file does not appear when I do git diff. It removes the file!! – hlitz Aug 09 '18 at 10:33
14

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
Ashish Sajwan
  • 705
  • 8
  • 16
6

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.

Doug
  • 32,844
  • 38
  • 166
  • 222
1

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

Community
  • 1
  • 1
ThR37
  • 3,965
  • 6
  • 35
  • 42
  • 2
    `git stash` is indeed handy, but it's way more complicated than `reset HEAD ` to use it for this case (not exactly what it's designed for). No need to bother with it, or patches. – Cascabel Aug 25 '10 at 13:29
  • @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... – ThR37 Aug 25 '10 at 14:37
1

if you did a git add and you haven't pushed anything yet, you just have to do this to unstage it from your commit.

git reset HEAD <file>

David
  • 3,488
  • 2
  • 21
  • 21
0

Answer:

git reset HEAD path/to/file
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Jordan Stefanelli
  • 1,446
  • 1
  • 13
  • 13
  • 1
    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. – Jim L Feb 07 '18 at 15:35
0

You have to reset that file to the original state and commit it again using --amend. This is done easiest using git checkout HEAD^.

Prepare demo:

$ git init
$ date >file-a
$ date >file-b
$ git add .
$ git commit -m "Initial commit"
$ date >file-a
$ date >file-b
$ git commit -a -m "the change which should only be file-a"

State before:

$ git show --stat
commit 4aa38f84e04d40a1cb40a5207ccd1a3cb3a4a317 (HEAD -> master)
Date:   Wed Feb 7 17:24:45 2018 +0100

    the change which should only be file-a

 file-a | 2 +-
 file-b | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

Here it comes: restore the previous version

$ git checkout HEAD^ file-b

commit it:

$ git commit --amend file-b
[master 9ef8b8b] the change which should only be file-a
 Date: Wed Feb 7 17:24:45 2018 +0100
 1 file changed, 1 insertion(+), 1 deletion(-)

State after:

$ git show --stat
commit 9ef8b8bab224c4d117f515fc9537255941b75885 (HEAD -> master)
Date:   Wed Feb 7 17:24:45 2018 +0100

    the change which should only be file-a

 file-a | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Daniel Alder
  • 5,031
  • 2
  • 45
  • 55
0

Most of these answers circulate around removing a file from the "staging area" pre-commit, but I often find myself looking here after I've already committed and I want to remove some sensitive information from the commit I just made.

An easy to remember trick for all of you git commit --amend folks out there like me is that you can:

  1. Delete the accidentally committed file.
  2. git add . to add the deletion to the "staging area"
  3. git commit --amend to remove the file from the previous commit.

You will notice in the commit message that the unwanted file is now missing. Hooray! (Commit SHA will have changed, so be careful if you already pushed your changes to the remote.)

Breedly
  • 12,838
  • 13
  • 59
  • 83
0

If you do not want to remove changes to be committed, you just need to:

git restore --staged <file>

After doing a git add/rm <file> you'll stage the file/s. The git restore --staged command will just undo the git add/rm <file> for the specified file, but you are not going to lose your work nor delete the file.