PHPackages                             eliashaeussler/gitattributes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. eliashaeussler/gitattributes

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

eliashaeussler/gitattributes
============================

Parser for .gitattributes files in an object-oriented way

2.0.1(5mo ago)48[1 issues](https://github.com/eliashaeussler/gitattributes/issues)GPL-3.0-or-laterPHPPHP ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0CI passing

Since Nov 8Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/eliashaeussler/gitattributes)[ Packagist](https://packagist.org/packages/eliashaeussler/gitattributes)[ RSS](/packages/eliashaeussler-gitattributes/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (10)Versions (8)Used By (0)

Object-oriented `.gitattributes` file handling
==============================================

[](#object-oriented-gitattributes-file-handling)

[![Coverage](https://camo.githubusercontent.com/fc2e63ff41d1dec7f73913cee34fdea22a9f494a66167b934142c080c3b02e4f/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c73436f7665726167652f6769746875622f656c6961736861657573736c65722f676974617474726962757465733f6c6f676f3d636f766572616c6c73)](https://coveralls.io/github/eliashaeussler/gitattributes)[![CGL](https://camo.githubusercontent.com/7b5d63a9f8fad685953608205c8b9c96d34ed5be55578cc1314df25860e3fca6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f656c6961736861657573736c65722f676974617474726962757465732f63676c2e79616d6c3f6c6162656c3d63676c266c6f676f3d676974687562)](https://github.com/eliashaeussler/gitattributes/actions/workflows/cgl.yaml)[![Tests](https://camo.githubusercontent.com/844c6dde2bf23bf61e1cd87e5fc5b8225c9a6529f466c14d734374469013c7f0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f656c6961736861657573736c65722f676974617474726962757465732f74657374732e79616d6c3f6c6162656c3d7465737473266c6f676f3d676974687562)](https://github.com/eliashaeussler/gitattributes/actions/workflows/tests.yaml)[![Supported PHP Versions](https://camo.githubusercontent.com/3532f247f248bc803eb19c032b91783e28dca91df978239f3533184a7f72fdab/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f656c6961736861657573736c65722f676974617474726962757465732f7068703f6c6f676f3d706870)](https://packagist.org/packages/eliashaeussler/gitattributes)

A PHP library to parse and dump `.gitattributes` file in an object-oriented way. The library provides a `GitattributesDumper` and `GitattributesParser` to either read or write contents to or from `.gitattributes` files. All attributes as of Git v2.46.0 according to the [official documentation](https://git-scm.com/docs/gitattributes) are supported.

🔥 Installation
--------------

[](#-installation)

[![Packagist](https://camo.githubusercontent.com/18ca5b4af59e64bd4eeea65af4ad7bd4b62998e6658d1326ba21b403c442038a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f656c6961736861657573736c65722f676974617474726962757465733f6c6162656c3d76657273696f6e266c6f676f3d7061636b6167697374)](https://packagist.org/packages/eliashaeussler/gitattributes)[![Packagist Downloads](https://camo.githubusercontent.com/0e1887dc6ae90fbb1069014a22bf770983a53495d0ea4064c28eb1348c3343bb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f656c6961736861657573736c65722f676974617474726962757465733f636f6c6f723d627269676874677265656e)](https://packagist.org/packages/eliashaeussler/gitattributes)

```
composer require eliashaeussler/gitattributes
```

⚡ Usage
-------

[](#-usage)

### Parse rules from `.gitattributes` file

[](#parse-rules-from-gitattributes-file)

The main parsing functionality is provided by the [`GitattributesParser`](src/GitattributesParser.php). It can be used as follows:

```
use EliasHaeussler\Gitattributes;

$parser = new Gitattributes\GitattributesParser(__DIR__);
$ruleset = $parser->parse('.gitattributes');
```

The returned ruleset contains the original filename as well as all parsed rules as instances of [`Rule\Rule`](src/Rule/Rule.php). A rule contains the file pattern and a list of attributes:

```
foreach ($ruleset->rules() as $rule) {
    echo $rule->pattern()->toString().' ';

    foreach ($rule->attributes() as $attribute) {
        echo $attribute->toString().' ';
    }

    echo PHP_EOL;
}
```

Important

Only attribute names listed in the [official documentation](https://git-scm.com/docs/gitattributes)are supported by the library. Using other than the supported attributes will raise an exception. See [`Rule\Attribute\AttributeName`](src/Rule/Attribute/AttributeName.php) for an overview.

### Dump `.gitattributes` file from rules

[](#dump-gitattributes-file-from-rules)

It is also possible to create a new `.gitattributes` file by dumping a list of prepared rules. This functionality is provided by the [`GitattributesDumper`](src/GitattributesDumper.php):

```
use EliasHaeussler\Gitattributes;

$rules = [
    // You can create rules in an object-oriented way
    new Gitattributes\Rule\Rule(
        new Gitattributes\Rule\Pattern\FilePattern('/tests'),
        [
            Gitattributes\Rule\Attribute\Attribute::set(Gitattributes\Rule\Attribute\AttributeName::ExportIgnore),
        ],
    ),
    // ... or using a string input
    Gitattributes\Rule\Rule::fromString('/phpunit.xml export-ignore'),
];

$dumper = new Gitattributes\GitattributesDumper(__DIR__);
$result = $dumper->dump('.gitattributes', $rules);
```

Note

A file must not exist when dumping file contents. Otherwise, an exception is thrown.

🧑‍💻 Contributing
----------------

[](#‍-contributing)

Please have a look at [`CONTRIBUTING.md`](CONTRIBUTING.md).

⭐ License
---------

[](#-license)

This project is licensed under [GNU General Public License 3.0 (or later)](LICENSE).

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance78

Regular maintenance activity

Popularity9

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 79.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~98 days

Total

5

Last Release

161d ago

Major Versions

1.0.2 → 2.0.02025-11-18

PHP version history (3 changes)1.0.0PHP ~8.1.0 || ~8.2.0 || ~8.3.0

1.0.2PHP ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0

2.0.0PHP ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/144cefe55242b883c87cb537463f3ba75a0f8198fc5b602b50c838aae31fe7ee?d=identicon)[eliashaeussler](/maintainers/eliashaeussler)

---

Top Contributors

[![renovate[bot]](https://avatars.githubusercontent.com/in/2740?v=4)](https://github.com/renovate[bot] "renovate[bot] (199 commits)")[![eliashaeussler](https://avatars.githubusercontent.com/u/16313625?v=4)](https://github.com/eliashaeussler "eliashaeussler (50 commits)")

---

Tags

composer-packagegitattributes

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/eliashaeussler-gitattributes/health.svg)

```
[![Health](https://phpackages.com/badges/eliashaeussler-gitattributes/health.svg)](https://phpackages.com/packages/eliashaeussler-gitattributes)
```

###  Alternatives

[doctrine/rst-parser

PHP library to parse reStructuredText documents and generate HTML or LaTeX documents.

64233.6k9](/packages/doctrine-rst-parser)[couscous/couscous

Documentation website generator

84167.6k24](/packages/couscous-couscous)[liip/serializer

High performance serializer that works with code generated helpers to achieve high throughput.

128365.2k4](/packages/liip-serializer)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

595.2M386](/packages/shopware-core)[symfony/json-streamer

Provides powerful methods to read/write data structures from/into JSON streams.

14440.0k8](/packages/symfony-json-streamer)[shyim/danger-php

Port of danger to PHP

8544.9k](/packages/shyim-danger-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
