🙏 Please Add .gitattributes To Your Git Repository

Carl Saunders - Feb 3 '20 - - Dev Community

What Is .gitattributes?

The .gitattributes file allows you to specify the files and paths attributes that should be used by git when performing git actions, such as git commit, etc.

In other words git automatically saves the file according to the attributes specified, every time a file is created or saved.

One of these attributes is the eol (end of line) and is used to configure the line endings for a file. This article will now dive deeper into how to configure the line endings, so every developer uses the same value when using different machines / OSes across the repository.

Why .gitattributes (Developers At War ⚔️)?

Not all developers are the same, just because you develop on a Windows machine using Visual Studio Code, don't expect the next pull request to have been implemented using the same dev environment (MacOS machine using Sublime Text 2).

As mentioned above the developers are using different OSes (Windows and MacOS) the default for the line ending will differ. On the Windows machine the default for the line ending is a Carriage Return Line Feed (CRLF), whereas on Linux/MacOS it's a Line Feed (LF).

To the naked eye the content will look the same, so why should we bother???

Well, if you have prettier enabled and the endOfLine property is set to lf.



{
  "endOfLine": "lf"
}


Enter fullscreen mode Exit fullscreen mode

On the Windows machine the developer will encounter linting issues from prettier, like those below.

Code File With Prettier Linting Errors - .gitattributes

Code File With Prettier Linting Errors

This is where .gitattributes comes to the rescue and saves the day 🦸!

New Repository (Repo)

To add the .gitattributes to the repo first you need to create a file called .gitattributes into the root folder for the repo.

Below is an example of the contents for the .gitattributes file.



*.js    eol=lf
*.jsx   eol=lf
*.json  eol=lf



Enter fullscreen mode Exit fullscreen mode

Commit this file to the repo and push your changes to the server.



git add .
git commit -m "Added .gitattributes to repo"
git push


Enter fullscreen mode Exit fullscreen mode

Now when anyone gets the code from the repo the default correct line ending will be used automatically via git, when creating and modifying the files.

Add to Existing Git Repository (Repo)

Follow the steps mentioned in the New Repository (Repo) steps to create the .gitattributes file. Once the file has been pushed to the git server then make sure that your local repo is clean and has nothing to commit. Use git status to determine whether your repo is clean.



git status


Enter fullscreen mode Exit fullscreen mode

Note: If you still have files to push or commit, please make sure that these actions are performed or the files are stashed before you perform the next commands.

GitAttributes Reset



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


Enter fullscreen mode Exit fullscreen mode

The above commands will now update the files for the repo using the newly defined line ending as specified in the .gitattributes.

Any changes or new changes will automatically use the line endings specified for that file type.

Next step is to inform any team mate or collaborator, to run the GitAttributes Reset commands.

Now prettier won't complain about CR and all developers can now live in peace! ☮️

Code File With No Linting Errors - .gitattributes

Code File With No Prettier Linting Errors
. . . . . . . . . . . . . . . . .
Terabox Video Player