Skip to content

Files with .gitattributes Rules Do Not Reprocess on Branch Switch #145

Description

@IgorTodorovskiIBM

This issue was identified by @MikeFultonDev.
When switching between branches in a repository that uses .gitattributes to define the encoding rules (e.g., zos-working-tree-encoding=IBM-1047), existing files in the working tree are not reprocessed to apply the new branch's .gitattributes rules. This behavior only applies to files that are already present in the working directory; new or exclusive files from the branch are correctly processed and encoded.

This results in an inconsistent state where some files have the correct encoding, while others retain the encoding from the previous branch.

Steps to Reproduce

git clone https://github.com/IgorTodorovskiIBM/GitAttributeTest && cd GitAttributeTest
ls -lT
# result:
t ISO8859-1   T=on  -rw-rw-r--   1 ITODORO  CDEV           7 Jan 17 11:21 a.mac
t ISO8859-1   T=on  -rw-rw-r--   1 ITODORO  CDEV           7 Jan 17 11:21 b.mac
t ISO8859-1   T=on  -rw-rw-r--   1 ITODORO  CDEV           7 Jan 17 11:21 c.mac

main branch has files already encoded in ISO-8859-1.

Switch to another branch (attribute) where the .gitattributes specifies encoding zos-working-tree-encoding=IBM-1047.

git checkout attribute
cat .gitattributes
ls -lT

# results:
git checkout attribute
branch 'attribute' set up to track 'origin/attribute'.
Switched to a new branch 'attribute'
[ITODORO@ZOSCAN2B ~/projects/GitAttributeTest]$ cat .gitattributes
*.mac text zos-working-tree-encoding=IBM-1047
[ITODORO@ZOSCAN2B ~/projects/GitAttributeTest]$ ls -lT
total 8
t ISO8859-1   T=on  -rw-rw-r--   1 ITODORO  CDEV           7 Jan 17 11:21 a.mac
t ISO8859-1   T=on  -rw-rw-r--   1 ITODORO  CDEV           7 Jan 17 11:21 b.mac
t ISO8859-1   T=on  -rw-rw-r--   1 ITODORO  CDEV           7 Jan 17 11:21 c.mac
t IBM-1047    T=on  -rw-rw-r--   1 ITODORO  CDEV           9 Jan 17 11:21 e.mac

Only new or exclusive files in the attribute branch are encoded in IBM-1047. Pre-existing files are not reprocessed.

Workarounds

The following workarounds have been identified but are not ideal for frequent branch switching:

Removing all tracked files and re-checking out:

git ls-files -z | xargs -0 rm -f  
git checkout .  

Activity

  1. alvaroandresgarayromero commented on Mar 10, 2025

    @alvaroandresgarayromero

    To ensure all files are properly rechecked out or refreshed with the correct encoding for the current commit’s .gitattributes, you must renormalize them. Run the following commands at the root of your working directory:

    # removes all files from the cache index, but keeps them in the working tree.
    git rm --cached -r .
    
    # resets the working directory, causing Git to reapply all rules from .gitattributes
    git reset --hard
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions