Skip to content

Instantly share code, notes, and snippets.

@vicly
Last active October 17, 2018 01:36
Show Gist options
  • Select an option

  • Save vicly/8f0854a820d4e8fb869947da738ba804 to your computer and use it in GitHub Desktop.

Select an option

Save vicly/8f0854a820d4e8fb869947da738ba804 to your computer and use it in GitHub Desktop.
[how autocrlf works] #Git

https://stackoverflow.com/questions/1967370/git-replacing-lf-with-crlf

https://help.github.com/articles/dealing-with-line-endings/

Configure it

# view current setting
#   system wide		%ProgramFiles(x86)%\git\etc\gitconfig
#   per user		~/.gitconfig
#   per repo		.git/config
git config core.autocrlf


# set
# add autocrlf=false to system-wide gitconfig      # per-system solution
git config --global core.autocrlf false            # per-user solution
git config --local core.autocrlf false             # per-project solution


# Configure Git on OS X to properly handle line endings
git config --global core.autocrlf input

How it works (commit file -> repository -> checked out file)

core.autocrlf=true:      core.autocrlf=input:     core.autocrlf=false:
        repo                     repo                     repo
      ^      V                 ^      V                 ^      V
     /        \               /        \               /        \
crlf->lf    lf->crlf     crlf->lf       \             /          \      
   /            \           /            \           /            \

If you're working on Windows

  • use true, if to use the project under Unix as well, and you don't want to configure IDE to use Unix line ending.
  • use false, if to use the project under Windows only
  • NEVER use input, unless you have a good reason

Warning

  • git config settings can be overridden by gitattributes settings.
  • crlf -> lf conversion only happens when adding new files, crlf files already existing in the repo aren't affected.

force control .gitattributes

# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.c text
*.h text


# Declare files that will always have CRLF line endings on checkout.
*.bat text eol=crlf

# all text files detected by Git will use lf
* text eol=lf


# Denote all files that are truly binary and should not be modified.
*.jar binary
*.png binary
*.gif binary
*.ico binary

Refresh a repo after changing line ending

The best way is to first backup your files with Git, delete every file in your repository (except the .git directory), and then restore the files all at once.

git add . -u
git commit -m "save before refresh line endings"

# force Git to rescan the working directory
rm .git/index

# rewrite index to pick up all the new line ending
git reset

# show the rewritten,normalised files
git status

# add all your changed files back, and prepare a commit
# This is your chance to inspect which files were unchanged.
git add -u
# A lot of "warning: CRLF will be replaced by LF in file."

# rewrite .gitattributes
git ad .gitattributes

git commit -m "normalise all the line endings"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment