惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
Blog — PlanetScale
Blog — PlanetScale
Vercel News
Vercel News
L
LangChain Blog
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
D
DataBreaches.Net
云风的 BLOG
云风的 BLOG
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
About on SuperTechFans
博客园_首页
N
Netflix TechBlog - Medium
Y
Y Combinator Blog
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
B
Blog
Apple Machine Learning Research
Apple Machine Learning Research
罗磊的独立博客
美团技术团队
V
V2EX

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Git line ending config
2024-02-22 · via jdhao's digital space

This post is about how to set up Git so that people using different operating systems can work in the same repository, without messing up the line ending of source files.

Settings for personal use (not good, not recommended)#

We can change the core.autocrlf setting:

git config --global core.autocrlf true

By setting core.autocrlf to true, if your original file line ending is CRLF, it will not change, but when you commit your files, git will normalize the line ending to LF.

After the above setup, you can run the following command:

This will put the normalized text files in your repo to the git index, You can then commit the changes:

git commit -m "Renormalize line ending to LF"

Note that the files in your local working tree still have original line ending. For example, if foo.py has CRLF line ending, and bar.py has LF line ending, they will remain so in your working tree.

For repository#

The above setting is not enforceable, because everyone has to set it up individually. So it is not really ideal way to make sure everyone has the correct line ending setup.

We can use the .gitattributes file to enforce this. Create a file named .gitattributes in the root directory of the project:

* text=auto

# mark this file as always have crlf line ending on checkout
*.bat text eol=crlf

# mark pattern as binary file
*.jpg binary

The setting here will overwrite the core.autocrlf setting.

We can then run the re-normalize command to normalize the line ending:

git add --renormalize .
git commit -m "Renormalize line ending to LF"

But this will not change the line ending for the file in the working tree. If we run the following command:

It shows something like this:

i/lf    w/lf    attr/text=auto          .gitattributes
i/lf    w/lf    attr/text=auto          another-file.md
i/lf    w/crlf  attr/text=auto          demo.txt
i/lf    w/crlf  attr/text eol=crlf      execute.bat

i/lf means the file copy at index is using LF line ending. w/lf or w/crlf means the line ending used in the working tree. In this case, for demo.txt, the file copy in the working tree is still using the CRLF line ending.

To correct this and use LF line ending also for text file not explicitly specified to use CRLF, we can run the following command:

git rm --cached -r .
git reset --hard HEAD

After this, if you run the ls-files command again, you should be able to see something like this:

i/lf    w/lf    attr/text=auto          .gitattributes
i/lf    w/lf    attr/text=auto          another-file.md
i/lf    w/lf    attr/text=auto          demo.txt
i/lf    w/crlf  attr/text eol=crlf      execute.bat

References#