Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a file from the list that will be committed

Tags:

git

file

commit

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.

like image 557
Max Frai Avatar asked Aug 25 '10 09:08

Max Frai


People also ask

What does it mean when a file is committed?

Commit the file means that it should be tracked in source control. If someone clones (or has a clone of) the repo, and the checkout a given version of your code, they should get the version of somefile.

How do I get rid of committed changes?

Use Git reflog to check commits history. Git stash lets you discard changes and save them for later reuse. Try Git checkout --<file> to discard uncommitted changes to a file. Git reset --hard is for when you want to discard all uncommitted changes.


2 Answers

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.

like image 159
Cascabel Avatar answered Sep 24 '22 21:09

Cascabel


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

like image 32
DavidR Avatar answered Sep 24 '22 21:09

DavidR