8

I have some files that was once in the git repository but later ignored. (mainly configuration files) However, when I run git reset --hard, the changes on these ignore files are also reset, where I am sure the head version has already got those files ignored.

Is this normal? And is there a way to make git reset --hard to ignore the changes on the ignored files?

cytsunny
  • 4,838
  • 15
  • 62
  • 129
  • Can you show the console output that demonstrates this? – Oliver Charlesworth Jan 05 '18 at 03:25
  • The message is not useful..... after I type `git reset --hard`, the output is `HEAD is now at 61ff2e2 Some more handle on file upload.` – cytsunny Jan 05 '18 at 03:28
  • @cytsunny Still, it's good to edit it into the question. Even if the output doesn't seem useful to you, it may be useful to people who are trying to answer your question. I'm not saying you should always include every bit of information possible just in case something is useful, but when someone asks for something specifically, it's better to include it than to assume they don't really need it. – David Z Jan 05 '18 at 03:32
  • I'm more thinking about a sequence of console actions that shows (say) creating an ignored file, running git status to prove it's ignored, then doing git reset, and showing that it's been deleted/reverted. – Oliver Charlesworth Jan 05 '18 at 03:38

2 Answers2

4

When you have already committed a file and ignored it afterwards, than you have to remove the file again in a separate commit.

When still want to use the file afterwards you should backup them beforehand.

cp <file> <file>.bak

You can remove ignored files like this:

git rm --cached <file>
git commit -m 'Remove ignored file'
mv <file>.bak <file>

For more information check out this question.

Huntro
  • 322
  • 1
  • 3
  • 16
4

You can also delete all ignore files using git clean:

When you have files in your .gitignore they won't be considered for changes, but still you might want to get rid of them, e.g. because they clutter your file system.

While a regular git clean will ignore them as well, passing the -x switch changes that:

git clean -x

If you want to see what would happen first, make sure to pass the -n switch for a dry run:

git clean -xn

Clean even harder by passing the -f (force cleaning under certain circumstances; I think this is also required by default) or -d (removes directories) switches:

git clean -xdf

Careful: You may be ignoring local config files like database.yml which would also be removed. Use at your own risk.

Source: https://makandracards.com/makandra/17529-git-how-to-remove-ignored-files-from-your-repository-s-directory

Noam
  • 333
  • 1
  • 18