ChatGPT解决这个技术问题 Extra ChatGPT

How do I remove files saying "old mode 100755 new mode 100644" from unstaged changes in Git?

For some reason, when I initially did a pull from the repository for a git project of mine, I got a ton of files in my working copy that have no discernible changes made to them, but keep showing up in my unstaged changes area.

I'm using Git Gui on Windows xp, and when I go to look at the file to see what has changed. All I see is:

old mode 100755  
new mode 100644  

Does anyone know what this means?

How can I get these files out of my list of unstaged changes? (Very annoying to have to go through 100's of files, just to pick out files I've recently edited and want to commit).

For the full, gory details on what core.filemode is all about, see my answer here. Note that every Git repository is supposed to have its own core.filemode setting, set by Git at the time Git created that repository; that setting is supposed to be the right one for that one repository. If it's wrong for some reason, you can change it.

L
Lynn

That looks like unix file permissions modes to me (755=rwxr-xr-x, 644=rw-r--r--) - the old mode included the +x (executable) flag, the new mode doesn't.

This msysgit issue's replies suggests setting core.filemode to false in order to get rid of the issue:

git config core.filemode false

+1. This means that git thinks that it can correctly set the executable bit on checked out files, but when it attempts to do so it doesn't work (or at least not in a way that it can read). When it then reads back the status of those files it looks like the executable bit has been deliberately unset. Setting core.filemode to false tells git to ignore any executable bit changes on the filesystem so it won't view this as a change. If you do need to stage an executable bit change it does mean that you have to manually do git update-index --chmod=(+|-)x <path>.
If, like me, the mode changes are important, you can set core.filemode to false, commit your actual code changes, and then set core.filemode to true and git will preserve the file changes.
I have the same problem, but it was due to using the same git repro via SSH git cmd line, and through Git Extensions on a mapped drive in Windows! . . Solution was the same, added into "config" [core] filemode = false
@robsch You can use git config --global ... to set the option in your global config file.
You can use git config -l --show-origin to see where each setting is coming from.
H
Honest Abe

Setting core.filemode to false does work, but make sure the settings in ~/.gitconfig aren't being overridden by those in .git/config.


Been there, done that. Sadly I found your comment only after having solved the problem myself. Still, +1!
If other users are cloning this project are on Windows, it might be best to actually just apply the change to the ~/.gitconfig file!
If you want to check on Windows Powershell. git config --list --show-origin | sls filemode or on Linux git config --list --show-origin | grep filemode. This will show you where you need to make the adjustments.
The core.filemode setting is supposed to be set in every .git/config. It is created automatically whenever Git creates a new repository, during git clone or git init, based on how your OS and file system work. It's not supposed to be wrong, but if it is, that's where to fix the setting: in the one specific repository.
a
auspicious99

This usually happens when the repo is cloned between Windows and Linux/Unix machines.

Just tell git to ignore filemode change. Here are several ways to do so:

Config ONLY for current repo: git config core.filemode false Config globally: git config --global core.filemode false Add in ~/.gitconfig: [core] filemode = false

Just select one of them.


Global config doesn't work because (i guess) git creates a repo with this options set to true (I've created a repo in linux)
when do these changes need to be applied?
S
Scott Willeke

I've encountered this problem when copying a git repo with working files from an old hard drive a couple times. The problem stems from the fact that the owner and permissions changed from the old drive/machine to the new one. The long and short of it is, run the following commands to straighten things out (thanks to this superuser answer):

sudo chmod -R -x . # remove the executable bit from all files

The former command will actually resolve the differences that git diff reported, but will revoke your ability to list the directories, so ls ./ fails with ls: .: Permission denied. To fix that:

sudo chmod -R +X . # add the executable bit only for directories

The bad news is that if you do have any files you want to keep executable, such as .sh scripts, you'll need to revert those. You can do that with the following command for each file:

chmod +x ./build.sh # where build.sh is the file you want to make executable again

Thanks, helped me a lot! One should also check that git config core.filemode is set to true, otherwise permission changes won't be detected. I also needed to refresh the git index after every change to pick it up.
The reason for me having this issue is exactly as described here: I've transferred all my projects to another harddrive when switching to my new computer. Thanks.
e
eballo

This worked for me:

git ls-files -m | xargs -L 1 chmod 644

Can you please explain what this does? Thanks.
This will include deleted files, so will fail as soon as a deleted file is encountered.
m
muhqu

I have faced the same issue. And this save my life:

This will revert all the permissions to what the diff is so you are left with nothing, but the changes you made to the files.

https://gist.github.com/jtdp/5443498

git diff -p -R --no-color \
| grep -E "^(diff|(old|new) mode)" --color=never  \
| git apply

More details in https://stackoverflow.com/a/4408378/450383


what does it do?
This is FANTASTIC! It finds all the files that had their permissions changed and reverts the changing permission without affecting anything else. Thank you!
S
SuperNova

It seems you have changed some permissions of the directory. I did the following steps to restore it.

$  git diff > backup-diff.txt                ### in case you have some other code changes 

$  git checkout .

a
auspicious99

The accepted answer to set git config core.filemode false, works, but with consequences. Setting core.filemode to false tells git to ignore any executable bit changes on the filesystem so it won't view this as a change. If you do need to stage an executable bit change for this repository anytime in the future, you would have to do it manually, or set core.filemode back to true.

A less consequential alternative, if all the modified files should have mode 100755, is to do something like

chmod 100755 $(git ls-files --modified)

which just does exactly the change in mode, no more, no less, without additional implications.

(in my case, it was due to a OneDrive sync with my filesystem on MacOS; by not changing core.filemode, I'm leaving the possibility open that the mode change might happen again in the future; in my case, I'd like to know if it happens again, and changing core.filemode will hide it from me, which I don't want)


M
Michael Weigel

This solution will change the git file permissions from 100755 to 100644 and push changes back to the bitbucket remote repo.

Take a look at your repo's file permissions: git ls-files --stage If 100755 and you want 100644

Then run this command: git ls-files --stage | sed 's/\t/ /g' | cut -d' ' -f4 | xargs git update-index --chmod=-x

Now check your repo's file permissions again: git ls-files --stage Now commit your changes:

git status

git commit -m "restored proper file permissions"

git push


D
Doppelganger

You could try git reset --hard HEAD to reset the repo to the expected default state.


If git wasn't able to set the executable bit correctly/consistently after a pull, it's not going to fair any better after a reset.
I moved some projects to an usb drive (fat32) and back again to my ubuntu machine (ext4) and ended up with a bunch of changed files, well, the attributes. git reset --hard HEAD worked perfectly for me. thanks
-1. OP states "just to pick out files I've recently edited and want to commit". This would remove those edits too.
-1 Suggesting this command in git is similar to saying "you can just rm -rf ./, I'm sure it won't have any unintended consequences".
No, this doesn't help and this is the problem. You reset and clean and still git status is showing the mode changes. This is a serious problem with windows git and I think it should be fixed, not just worked around by ignoring the file mode.
I
IskandarG

This happens when you pull and all files were executable in the remote repository. Making them executable again will set everything back to normal again.

chmod +x <yourfile> //For one file
chmod +x folder/* // For files in a folder

You might need to do:

chmod -x <file> // Removes execute bit

instead, for files that was not set as executable and that was changed because of the above operation. There is a better way to do this but this is just a very quick and dirty fix.


yes but that would create a change that has to be committed later, right?
W
Wae

You can use the following command to change your file mode back. git add --chmod=+x -- filename Then commit to the branch.


G
Goddard

This is like what bkm found, but it also takes into account any deleted files and only gives a warning while still applying the changes.

This works well to revert file mode settings from previous commit.

git diff -p --no-ext-diff --no-color --diff-filter=d | grep -E "^(diff|old mode|new mode)" | sed -e "s/^old/NEW/;s/^new/old/;s/^NEW/new/" | git apply

k
kEnobus

I had just the one troublesome file with the changed permissions. To roll it back individually, I just deleted it manually with rm <file> and then did a checkout to pull a fresh copy.

Luckily I had not staged it yet.

If I had I could have run git reset -- <file> before running git checkout -- <file>


C
Collin Krawll

I just ran into this issue when diffing my branch with master. Git returned one 'mode' error when I expected my branch to be identical to master. I fixed by deleting the file and then merging master in again.

First I ran the diff:

git checkout my-branch
git diff master

This returned:

diff --git a/bin/script.sh b/bin/script.sh
old mode 100755
new mode 100644

I then ran the following to fix:

rm bin/script.sh
git merge -X theirs master

After this, git diff returned no differences between my-branch and master.


what is theirs?
theirs tells the merge to use the version in the other branch (not the current branch, which is referred to as ours). More info on ours vs theirs here.
r
riley

A number of the solutions above will break if there are any deleted files.

This will list files that are only modified, and not deleted, and change them all to 755

git diff-files --name-only --diff-filter=d | xargs -L 1 chmod 755