How do I force git to checkout the master branch and remove carriage returns after I've normalized files using the "text" attribute?

GitNewlinecore.autocrlf

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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

git checkout HEAD^
git checkout -f master

Solution 2 - Git

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.

Solution 3 - Git

Stash (save) your changes

> git stash

Take updated changes till the last commit from remote for your current branch (to start clean with no untracked/modified files .. just in case)

> git checkout .

Now switch to the master branch

> git checkout master

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJasonView Question on Stackoverflow
Solution 1 - GitJasonView Answer on Stackoverflow
Solution 2 - GitmechsinView Answer on Stackoverflow
Solution 3 - Gitani17View Answer on Stackoverflow