Max Frai
Max Frai

Reputation: 64336

Remove a file from the list that will be committed

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.

Upvotes: 52

Views: 141966

Answers (10)

DavidR
DavidR

Reputation: 489

git rm --cached will remove all from the commit set ("un-adding" it); that sounds like what you want. For just one file: git rm --cached <file>. Like suggested @Nicole Finnie in the comment.

Upvotes: 48

Gerard Duch
Gerard Duch

Reputation: 61

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.

Upvotes: 0

David
David

Reputation: 4113

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>

Upvotes: 1

Breedly
Breedly

Reputation: 14286

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

Upvotes: 0

Daniel Alder
Daniel Alder

Reputation: 5382

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(-)

Upvotes: 2

Jordan Stefanelli
Jordan Stefanelli

Reputation: 1494

Answer:

git reset HEAD path/to/file

Upvotes: 0

Ashish Sajwan
Ashish Sajwan

Reputation: 704

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

Upvotes: 14

Doug
Doug

Reputation: 35186

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.

Upvotes: 6

ThR37
ThR37

Reputation: 4085

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

Upvotes: 1

Cascabel
Cascabel

Reputation: 497322

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.

Upvotes: 65

Related Questions