118

Okay, so I added the file .gitattributes with lines like this

*.css text
*.js text
etc...

I then followed the instructions at http://git-scm.com/docs/gitattributes#_checking-out_and_checking-in

$ rm .git/index     # Remove the index to force Git to
$ git reset         # re-scan the working directory
$ git status        # Show files that will be normalized
$ git add -u
$ git add .gitattributes
$ git commit -m "Introduce end-of-line normalization"

But now my working copy still has the carriage returns! I have untracked files that I would like to keep. How do I have git checkout the master branch again with the normalized files?

I know the files are normalized in the repository because when I clone the repo, I have all the files without the carriage returns.

Jason
  • 10,225
  • 8
  • 57
  • 79

2 Answers2

292

Ahah! Checkout the previous commit, then checkout the master.

git checkout HEAD^
git checkout -f master
Jason
  • 10,225
  • 8
  • 57
  • 79
  • 5
    Thanks for this workaround, but it's glaring issue in git that "checkout -f" doesn't really force re-checkout. Another woraround would be to remove all working copy files first (i.e. everything but .git dir). – pfalcon Mar 18 '14 at 03:38
  • Ah, yes, thanks for that! Hearing that, I'm guessing we could just remove the files of interest and run checkout. For me there was actually only one file I was trying to get corrected. But of course it may be all the files, hundreds, or thousands. – Jason Mar 19 '14 at 15:06
  • This fails on git 1.8.3 (mac) with: error: pathspec 'HEAD^' did not match any file(s) known to git. – dval Oct 10 '14 at 14:39
  • @dval, look at the edits to this post and you'll see an alternative way to do this. I am running on a Mac, too, and this worked for me. I also encourage you to upgrade your git to 2+ and you can use Homebrew to do this in a safe maintainable way. – Jason Jul 14 '15 at 23:29
  • Didn't work for us when trying to replace CRLF by LF (although ``.gitattributes`` contains ``text eol=lf``). Solution of @mechsin worked smoothly. – Jidehem Dec 21 '17 at 10:37
  • 2
    This doesn't really work. GIT will only update files that changed between the two commits (with some exceptions). If a repository is brand new, eg. only two commits and the first happens to be empty, then this solution will work. Otherwise, you need to forcibly delete all files as described by mechsin's answer. – jstine Jan 06 '18 at 15:21
  • hey! i bring to here after get this error `error: cannot stat 'empresas/presupuesto/acciones' Permission denied ` when intent to merge develop into master and then all was good when force git to checkout with `git checkout -f master` +1 for that – Elvis Technologies Jan 12 '19 at 04:23
18

As others have pointed out one could just delete all the files in the repo and then check them out. I prefer this method and it can be done with the code below

git ls-files -z | xargs -0 rm
git checkout -- .

or one line

git ls-files -z | xargs -0 rm ; git checkout -- .

I use it all the time and haven't found any down sides yet!

For some further explanation, the -z appends a null character onto the end of each entry output by ls-files, and the -0 tells xargs to delimit the output it was receiving by those null characters.

mklement0
  • 245,023
  • 45
  • 419
  • 492
mechsin
  • 543
  • 6
  • 10