29 Haziran 2021 Salı

git .gitattributes Dosyası

1. End Of Line Formatlama Nasıl Olmalı
Açıklaması şöyle. Windows ve Linux karışık ortamlarda çalışıyorsak 1. seçenek tercih edilmeli.
1. Checkout Windows-style, commit Unix-style
Git will convert LF to CRLF when checking out text files. When committing text files, CRLF will be converted to LF. For cross-platform projects, this is the recommended setting on Windows ("core.autocrlf" is set to "true")

2. Checkout as-is, commit Unix-style
Git will not perform any conversion when checking out text files. When committing text files, CRLF will be converted to LF. For cross-platform projects this is the recommended setting on Unix ("core.autocrlf" is set to "input").

3. Checkout as-is, commit as-is
Git will not perform any conversions when checking out or committing text files. Choosing this option is not recommended for cross-platform projects ("core.autocrlf" is set to "false")
Burada listelenmiyor ama bence her zaman Linux style checkout ve commit en iyisi.

2. Formatting and Whitespace Uygulanması İçin Yöntemler
Formatting ve whitespace için iki seçenek var
1. git config ile istenilen değeri atamak
2. .gitattributes dosyasına eklemeler yapmak 

Bu İki Yöntemden .gitattributes Tercih Edilmeli 
Açıklaması şöyle. Yani depomuza (repository) .gitattributes dosyası eklemek, herkese teker teker "git config ..." yapmaktan daha kolay
Originally, Git for Windows introduced a different approach for line endings that you may have seen: core.autocrlf. This is a similar approach to the attributes mechanism: the idea is that a Windows user will set a Git configuration option core.autocrlf=true and their line endings will be converted to Unix style line endings when they add files to the repository.

The difference between these two options is subtle, but critical: the .gitattributes is set in the repository, so its shared with everybody. But core.autocrlf is set in the local Git configuration. That means that everybody has to remember to set it, and set it identically.
Kısaca şöyle yaparız. Kod için şöyle yaparız
# linux line-endings
*        text eol=lf
Eğer zaten git'e ekli dosyalar varsa ve .gitattributes sonradan ekleniyorsa, .gitattributes dosyasını oluşturduktan sonra dosyayı IntelliJ ile açıp satır sonunu LF yaparız. Bu durumda git dosyada değişiklik olduğunu düşünüyor. Daha sonra şöyle yaparız
git add --renormalize .
Bunu yapınca git artık değişiklik olduğunu düşünmüyor.


1. git config Yöntemi
Windows'ta End Of Line formatlamayı Linux gibi için şöyle yaparız
git config --global core.autocrlf true
Linux'ta End Of Line formatlamayı Linux gibi için şöyle yaparız
git config --global core.autocrlf input

2. .gitattributes Dosyası Yöntemi
Not : IntelliJ kullanıyorsak Settings > Editor > Code Style altında Line separator alanı Unix and macOs (\n) seçilmeli. Böylece her şey her zaman Linux gibi olacak

.gitattributes dosyasındaki text değerinin açıklaması şöyle
text
This attribute enables and controls end-of-line normalization. When a text file is normalized, its line endings are converted to LF in the repository. To control what line ending style is used in the working directory, use the eol attribute for a single file and the core.eol configuration variable for all text files.
Örnek - *.sh Dosyaları İçin LF'i muhafaza etmek
Windows bilgisayarda checkout yaparken bash betiklerinin (script) bozulmaması için şöyle yaparız
*.sh text eol=lf
Aynı şey sanırım şöyle de yapılabilir.
# Never modify line endings of our bash scripts
*.sh -crlf
Örnek - Dosya Uzantısına Göre Karışık Kullanım
Şöyle yaparız
# auto
*           text=auto     
*.txt       text

# windows line-endings
*.vcproj    text eol=crlf 

# linux line-endings
*.sh        text eol=lf   

*.jpg       -text
Örnek - auto normalization
Şöyle yaparız
* text=auto
Açıklaması şöyle. Böylece Windows'tan commit yapsak bile repository 'de LF olarak saklanır. Windows checkout yaparken CRLF olarak alır, Linux ise LF olarak alır.
With this set, Windows users will have text files converted from Windows style line endings (\r\n) to Unix style line endings (\n) when they’re added to the repository.


Hiç yorum yok:

Yorum Gönder