When git reset –hard doesn’t work


git


Sometimes things go wrong and we want to start from scratch. Provided you’re using Git as your version control system, it’s highly probable you’re a friend of git reset --hard.
However, I’ve found cases when git reset –hard doesn’t work. Things like clean or -f parameter doesn’t work either.
I did however found out, that sometimes on Windows machines, line endings like to live their own lives and switch randomly between CRLF and LF.
I found a solution I want to share with you. However, I do not see myself as a Git pro, so if you found out other one for such issue, let me know! 😇

First thought

First thought would be to just stop git from doing so.
Generally in either your global or repository config you have section called core. An entry called autocrlf resides there and is often responsible for behavior mentioned above.
Quick fix for this would be to just set the flag to false by calling in your repo (or adding --global after config for global scope):


If config change fails

Been here, seen that. It seems that sometimes there is an issue with line endings that is also driven by .gitattributes file, even if it is not present in repository!
A fix for this is to remove (non-exisiting) .gitattributes, then stage the change and then reset changes like you used to do:

Note: git add -A is necessary here and it can’t be changed to git add * because it won’t stage file deletion that we did with rm command.

If you do have .gitattributes included in your repository, you might want to actually tweak it with proper line endings per extension defined instead. 🔨

Leave a Reply

Your email address will not be published. Required fields are marked *