跳到主要内容

Git Attributes

提示

The .gitattributes file is a configuration file in a Git project used to define file attributes, allowing better control over how files are processed and displayed.

By adding a .gitattributes file to your project, you can set attributes for specific files or file types, thereby optimizing the version control and collaboration process. The main features include:

  • File Encoding: Define the encoding of files to ensure they are correctly interpreted across different operating systems and editors.
  • Line Endings: Specify the line ending format of files, such as Unix (LF), Windows (CRLF), or Mac (CR).
  • Merge Strategy: Configure the strategy used when merging files to avoid conflicts and ensure correct merging.
  • Binary File Handling: Define how binary files are handled to prevent them from being misinterpreted as text files.
  • Git LFS Support: Specify which files should be managed by Git LFS (Large File Storage) to optimize version control for large files.

Properly configuring .gitattributes can significantly enhance the performance and collaboration efficiency of your Git project.

Creating a .gitattributes File

  1. Log in to GitCode: First, make sure you have logged into your GitCode account.
  2. Enter the Project: Go to the page of the project that contains the .gitattributes file you want to create.
  3. Click Add File: Click the "+" button at the top right of the project page and select "New File".
  4. Name the .gitattributes File: In the filename field, enter ".gitattributes" (make sure the filename starts with a dot).
  5. Define File Attributes: In the file, define file attributes and rules. For example, you can set the file's encoding, line endings, and merge strategy.
  6. Click "Commit Changes": At the top right of the page, click the "Commit changes" button to save the .gitattributes file.

attributes1 attributes2

Example

Here is an example for a Java code file:

# Java sources
*.java text diff=java
*.kt text diff=kotlin
*.groovy text diff=java
*.scala text diff=java
*.gradle text diff=java
*.gradle.kts text diff=kotlin

# These files are text and should be normalized (Convert crlf => lf)
*.css text diff=css
*.scss text diff=css
*.sass text
*.df text
*.htm text diff=html
*.html text diff=html
*.js text
*.jsp text
*.jspf text
*.jspx text
*.properties text
*.tld text
*.tag text
*.tagx text
*.xml text

# These files are binary and should be left untouched
# (binary is a macro for -text -diff)
*.class binary
*.dll binary
*.ear binary
*.jar binary
*.so binary
*.war binary
*.jks binary

# Common build-tool wrapper scripts ('.cmd' versions are handled by 'Common.gitattributes')
mvnw text eol=lf
gradlew text eol=lf

For more examples, refer to https://gitcode.com/alexkaratarakis/gitattributes