Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new git repository and already git reset does not work

Tags:

git

1) I did git init in a folder

2) added some project files

3) I did git add * so it added recursively all files

4) I realised that I added a bunch of files I don't want to be versioned, so I wanted to undo the add

5) git reset gives me the following error:

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

6) git status still shows all the added files

7) I tried git reset --hard HEAD and I got the following error:

fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions

8) git status still shows all the added files

What should I do at this point?

like image 333
lurscher Avatar asked Oct 08 '10 22:10

lurscher


People also ask

How do I completely reset a git repository?

If you want to restart a git repo, nuke the . git directory and do git init add your files/directories and commit.

Does git reset hard remove new files?

git reset --hard is a classic command in this situation - but it will only discard changes in tracked files (i.e. files that already are under version control). To get rid of new / untracked files, you'll have to use git clean !

Do we need to commit after git reset hard?

First of all git reset — hard is a very dangerous command because it eliminates all of your noncommitted changes. Be sure to always double check that the output of git status is empty (clean) before you begin using it. Git records the state of the files when you stage them with git add or when you make a commit.


1 Answers

git rm [-r] --cache -- <file>…

would be the right command to remove from the index the file you don't want to commit.
Any reset command won't work, since you have to any commit yet in your newly created repository.

From the git rm man page:

--cached

Use this option to unstage and remove paths only from the index.
Working tree files, whether modified or not, will be left alone.

-r

Allow recursive removal when a leading directory name is given.

<file>…

Files to remove.
Fileglobs (e.g. *.c) can be given to remove all matching files. If you want git to expand file glob characters, you may need to shell-escape them.
A leading directory name (e.g. dir to remove dir/file1 and dir/file2) can be given to remove all files in the directory, and recursively all sub-directories, but this requires the -r option to be explicitly given.

like image 144
VonC Avatar answered Oct 23 '22 05:10

VonC