开发者

Removing all files from a just initialized git repo staging area

开发者 https://www.devze.com 2023-02-27 07:18 出处:网络
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 .g开发者_Go百科it/info/

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 .g开发者_Go百科it/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


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.


For all the repo:

git rm -r --cached .
0

精彩评论

暂无评论...
验证码 换一张
取 消