Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing all files from a just initialized git repo staging area

Let's say I just created a repo

git init

and then did

git add .

I then did a

git status

and noticed that there were there a couple of files I didn't want. I went to .git/info/ and added those files to the exclude file. I then tried to

git reset .

but got a

fatal: Failed to resolve 'HEAD' as a valid ref.

Trying a

git reset --hard

had the same effect. I guess this is because I still didn't make a commit? How can I achieve the same effect on a new, comitless repo, then?

Thanks

like image 830
devoured elysium Avatar asked Apr 19 '11 19:04

devoured elysium


Video Answer


2 Answers

Update: the title of your question suggests you want to unstage all files, but from the text of your question, it sounds as if you just want to unstage a couple of them - you've now clarified this below, and I've added a bit to the end of my answer mentioning how to unstage every new file that has just been added.

You're correct that that error you see is because you haven't created any commits yet.

When you did git add . that "staged" everything in the work tree for your next commit. As well as ignoring those files, you also want to unstage them. If you type git status, it gives you a hint how to do this:

 # Changes to be committed:
 #   (use "git rm --cached <file>..." to unstage)

... so if the files that you want to unstage are foo.txt and bar.c, you would do:

git rm --cached foo.txt bar.c

Then you can carry on and create your first commit with:

git commit

... as you would expect.


You've clarified below that in fact you want to unstage every file you've just added - in that case you can, as grzuy says, modify that to:

git rm -r --cached .

... and start staging files more selectively with git add filename.

like image 89
Mark Longair Avatar answered Oct 11 '22 18:10

Mark Longair


For all the repo:

git rm -r --cached .
like image 39
grzuy Avatar answered Oct 11 '22 19:10

grzuy