Original text: dev. The to/deadlybyte /…
The.gitAttributes file allows you to specify the attributes of files and paths that git should use when performing git actions such as git commit.
In other words, whenever a file is created or saved, Git automatically saves the file as specified by these properties.
One of the attributes is EOL (End of Line), which is used at the end of the line in the configuration file. This article discusses how to configure the end of the line so that every developer using different machines and operating systems across repositories can use the same value.
⚔️. Can GitAttributes calm the programmer wars?
Not all developers are uniform, and the next time you write Code using Visual Studio Code on a Windows host, the Code submitted by a pull Request may be developed in Sublime Text 2 on a MacOS host.
Because it is common for developers to use different operating systems, each operating system handles the end of a line differently. In Windows, the default Carriage Return Line Feed (CRLF) is used for the end of a Line. Linux/MacOS uses only newline LF (Line Feed).
The naked eye looks the content is the same, why should whole these unitary moths 🤔??
So if you also prettier and set endOfLine like this:
{
"endOfLine": "lf"
}
Copy the code
Developers using Windows will encounter the following syntax tips:
This is where.GitAttributes comes in and saves the day!
Configure.gitAttributes for the new project
Start by creating a.gitAttributes file in the project root directory with the following contents:
*.js eol=lf
*.jsx eol=lf
*.json eol=lf
Copy the code
Commit the file in the repository and push the changes to the server:
git add .
git commit -m "Added .gitattributes to repo"
git push
Copy the code
This way, when someone takes code from the repository and creates or modifs its files, the correct line end by default will be used automatically via Git.
Add.gitAttributes to an existing project
Create the.gitAttributes file as described in the previous section. Once the file is pushed to the Git server, make sure the local repository is clean and has nothing to commit. Git status
git status
Copy the code
Note: If there are still files to push or commit, make sure these actions are completed first or stash stash stash before the next command is executed.
GitAttributes Reset
git rm --cached -r .
git reset --hard
Copy the code
These two commands will update the repository file using the new.gitAttributes definition line end rule.
Any changes will automatically apply the new line end based on the matching file type.
The next step is to notify your team or partner and run the above two commands as well.
Now Prettier isn’t complaining about CR anymore, and developers can live together. ☮ ️
–End–
View more front-end good article please search fewelife concern public number reprint please indicate the source