1

As a repo creator/admin I want to ensure that people working on both Windows and Linux won't have problems with line endings (CRLF vs LF). I've read that .gitattributes is a good way to do that. I've also read that I need to commit to my repo just like any other file at its root.

But since it's apparently just like any other file do I need to commit it to every branch? Do I need to watch out for this while creating branches (and possibly some other actions)?

I've really tried googling for that but just couldn't find anything.

6
  • Technically, .gitattributes isn't "on a branch" or even "in a branch": it's in commits. The branch names move and even get deleted; it's the commits that are the solid parts of a repository, and hence the commits that matter. Note that Git automatically de-duplicates otherwise-duplicated files in commits, so if the .gitattributes contents in ten thousand commits never changes, there's really only the one underlying copy, being shared by all the commits. Commented Oct 8, 2021 at 19:21
  • Yes, but I either have to make sure this commit is shared by all branches or (less optimal but still possible) have every branch have its own commit with this file. Commented Oct 11, 2021 at 8:19
  • 1
    Each branch name picks out one commit. That commit—whatever it is—is, by definition, the last commit that is "on" that branch. The parent (or parents) of that commit are also on that branch; the parents of the parents are on the branch; and so on. The .gitattributes file needs to go into every commit. Commented Oct 11, 2021 at 8:21
  • Note that each new commit automatically includes all the files from its parent unless you explicitly add or remove something. So you generally just create the .gitattributes early on, after which it's just automatically in every new commit. Commented Oct 11, 2021 at 8:22
  • Ok, got that, thanks. But the bottom line doesn't change - one way or another I have to make sure that .gitattributes is included in every commit. Commented Oct 11, 2021 at 8:24

1 Answer 1

4

Yes, just like .gitignore it will only be effective when it's checked out.

Meaning if you check out a branch that doesn't have it then it won't do anything.

But usually that's solved by merging it into main or master and all other branches will eventually fork from there. If you have any other long-running branches (such as a maintenance branch for an older version), then you can decide to cherry-pick it onto there as well.

Sign up to request clarification or add additional context in comments.

2 Comments

That's what I suspected, thanks. I'll wait before accepting the answer in case others want to add something. ;)
Technically .gitattributes can affect a commit that isn't checked out: either because it will be, once the commit gets checked out (so then it affects that future checkout), or when using git archive. But overall, yes. :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.