36

I'm working on a project where we have recently started using git. The setup was not perfect from start, so I've set up .gitattributes after people started cloning/working and I'm still making some changes to this file.

Consider the following setup...

Both Alice and Bob have cloned "repo.git" and the repository contains the file /myproj/src/file.ending with \n as line ending, i.e. the file does not contain \r characters.

They also both have .gitattributes with the following setting:

/myproj/src/file.ending -text

This tells git that file.ending should not be considered a text file and thus no line ending conversion should take place.

Accordingly, the files in Alice's and Bob's working tree also have \n as line ending.

Now, Alice makes the following change to .gitattributes:

/myproj/src/file.ending text

Alice would like this change to take effect, both for her and for Bob.

The only way I know of right now is quite intrusive:

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

I would like to avoid two things:

  • Alice has to commit her `.gitattributes` file before she can actually test it (reset above will overwrite her changes).
  • Bob has to wipe his index and working tree to get the update. Bob is not happy.

What is the preferred way of doing this?

15
  • touch myproj/src/file.ending Feb 6, 2018 at 13:31
  • @PetSerAl - touching the file is supposed to do what, exactly? Feb 6, 2018 at 14:15
  • @MarkAdelsberger That will change stat information for file in working directory, so it does not match cached stat information saved in index. Feb 6, 2018 at 14:18
  • @PetSerAl - That's not how git detects change. Feb 6, 2018 at 14:22
  • 1
    @MarkAdelsberger - git checkout alone is not enough.
    – jgreen81
    Feb 6, 2018 at 14:27

5 Answers 5

24

You don't have to reset hard (if I understand correctly what you're doing).

My case is similiar. I added a .gitattributes in a running project. I need the files I have and the files in online repo to be ruled by gitattr.

# This will force git to recheck and "reapply" gitattributes changes.
git rm --cached -r .
git add -A

Your commit will re-add all the .ending files you mention and you'll not lose any changes you may have. Of course, Bob will have to pull to get it.

3
  • 12
    This has a massive problem. It will add files that were not versioned and shouldn't be there.
    – Jan Hudec
    Oct 24, 2018 at 11:21
  • 2
    Indeed, this solution has to have a huge warning. For any mid to large size repository, this is definitely not a good thing to do Jun 11, 2020 at 9:44
  • Actually for me the solution mentioned in the question works better. After doing this, git converted the line endings. However all the files showed up as changed. git reset --hard worked better than git add -A.
    – Joerg S
    Oct 11 at 12:10
10

osse on irc://chat.freenode.net/#git gave me this method, and it works reasonably well:

git rm -r :/ && git checkout HEAD -- :/

This will complain if you have uncommitted changes in your tree.

Seems like there should be a better way though.

2
  • 3
    This answer doesn't work on Windows Powershell, which doesn't have && (ref). On Powershell, use git rm -r :/; git checkout HEAD -- :/. Jul 30, 2021 at 13:49
  • One (big) improvement would be to only apply git rm to the files that are affected by the .gitattributes change. In this case *.ending. So: find . -iname "*.ending" | xargs -n 1 git rm (maybe the -n 1 isn't needed but it gives you an output line for each input line and won't touch files that aren't known to git). Mar 15, 2022 at 15:27
6

By "would like this change to take effect", do you mean that Alice wants the working copies to switch to Windows-style line endings for both her and Bob? Then the first problem is, why is Alice taking responsibility for what's in Bob's working tree?

If the file is better described by the new attributes, so be it; the .gitattributes file can be edited, tested, and committed just like any other.

The procedure you suggest for getting the new attributes to take effect doesn't make a lot of sense, for two reasons:

First, why are you wiping the index? The text attribute affects the relationship between the index and the working copy. In your example it seems it's the working copy you need to change, not the index.

Second, why are you wiping everything from the index? Only the paths whose attributes have changed need to be addressed.

So in your example, if Alice wants to locally reflect the new attributes, the most that should be necessary is

rm myproj/src/file.ending
git checkout -- myproj/src/file.ending

Since this procedure doesn't overwrite the .gitattributes file, there's no need to prematurely commit it.

It's not clear to me what exactly makes Bob unhappy about your original procedure, so I don't know if this one makes him any happier. Perhaps he just wants the update to be automatic when he pulls; while it's not unreasonable to expect that, I'm not sure it's in the cards as git works.

The problem is how changes are detected. In almost every situation, if git's updating the working tree at the end of a merge or fast-forward (e.g. completing a pull), it need only compare the hashes of the indexed objects for the old commit and the new commit to tell if there's a change to apply.

The exception is if attributes (or filter definitions) change - as noted above, that doesn't change the index. But those conditions are relatively rare, and the checks for them are much more expensive than hash check that's right almost every time, so rather than burden every comparison with mostly-pointless costs git allows that when you know you've done certain things, you have to take an extra step.

So if this is going to happen once, just let the team communicate. "The attributes for this path are changing; you may want to refresh your working copies of the affected files."

If it's going to happen repeatedly, my best advice is to figure out why this keeps happening and fix it. You could try to set up some kind of scripted automation, maybe even with hooks, to detect and address attribute changes; but it's a lot of complication and will likely cause more trouble than it fixes.

10
  • 2
    Regarding the synchronization between Alice and Bob, I don't understand you are wondering why Bob wants these changes. As you state it, the .gitattributes change better describe the file so naturally, Bob wants to make use of this description. I'm wiping the whole index because using one specific file was just an example. It seems that rm alone (instead of git rm) is also useful. So you could use that instead. Was not aware. As stated in a comment to my original text, "touch" also seems to work and may be considered less intrusive.
    – jgreen81
    Feb 6, 2018 at 14:46
  • @eversceptic - It's not a question of whether Bob wants the changes. It's a question of who determines if Bob wants the changes. If I change attributes on a file, it isn't up to me to decide that your work tree needs to be changed; it's up to you. (As one example, Bob could be running on a Mac or a unix box, in which case the change won't even affect his work tree.) All Alice needs to do is make sure Bob is aware that the attributes changed; for her to "want the changes to take effect for Bob" is a bad requirement. Feb 6, 2018 at 15:24
  • @eversceptic - Even if the "single file" is just an example, that still doesn't mean it makes sense to wipe out everything. You know what paths' attributes have changed and there's no reason to mess with files at other paths. In particular, one of your two stated problems in the original question relates directly to using a procedure that messes up the .gitattributes file itself. Feb 6, 2018 at 15:26
  • @eversceptic - If you prefer to use touch instead of rm, use touch. I will continue to use rm. When the purpose of the command is to cause the file to be overwritten from the index, neither one is "less invasive". Feb 6, 2018 at 15:27
  • Regarding Alice's relationship to Bob, I see your point :) In our case, we had some problems that affected colleages and it was a fix to these problems. Saying that Bob wants the fix is a better statement than Alice wants to give Bob the fix.
    – jgreen81
    Feb 6, 2018 at 17:17
2

After you have committed the changes for .gitattributes run the following to apply the changes

git rm --cached -r .
git reset --hard
1
  • That is precisely what I said he doesn't want to do because it's too intrusive.
    – Martin
    Sep 3 at 13:48
0

In order to make the changes to take effect you need some kind of git server — for example Gitlab. Push the repo to Gitlab and then create a new folder, cd into the new folder that you created and then clone the repo into it via

git clone <repository> .

and then changes should be applied correctly.

Like this you avoid the quite intrusive other option that you mentioned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.