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

3

146

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.

Ouidaouija answered 20/6, 2013 at 20:51 Comment(0)
O
354

Ah ah! Checkout the previous commit, then checkout the master.

git checkout HEAD^
git checkout -f master
Ouidaouija answered 20/6, 2013 at 20:58 Comment(7)
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).Excretory
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.Ouidaouija
This fails on git 1.8.3 (mac) with: error: pathspec 'HEAD^' did not match any file(s) known to git.Frontiersman
@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.Ouidaouija
Didn't work for us when trying to replace CRLF by LF (although .gitattributes contains text eol=lf). Solution of @mechsin worked smoothly.Inconformity
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.Gary
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 thatWeatherbeaten
T
22

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.

Theadora answered 24/11, 2015 at 20:24 Comment(0)
V
2
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

Vermillion answered 25/9, 2021 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.