How do I make Git ignore file mode (chmod) changes?

后端 未结 11 1182
天命终不由人
天命终不由人 2020-11-22 02:29

I have a project in which I have to change the mode of files with chmod to 777 while developing, but which should not change in the main repo.

Git pick

相关标签:
11条回答
  • 2020-11-22 02:39

    By definining the following alias (in ~/.gitconfig) you can easily temporarily disable the fileMode per git command:

    [alias]
    nfm = "!f(){ git -c core.fileMode=false $@; };f"
    

    When this alias is prefixed to the git command, the file mode changes won't show up with commands that would otherwise show them. For example:

    git nfm status
    
    0 讨论(0)
  • 2020-11-22 02:39

    Simple solution:

    Hit this Simple command in project Folder(it won't remove your original changes) ...it will only remove changes that had been done while you changed project folder permission

    command is below:

    git config core.fileMode false

    Why this all unnecessary file get modified: because you have changed the project folder permissions with commend sudo chmod -R 777 ./yourProjectFolder

    when will you check changes what not you did? you found like below while using git diff filename

    old mode 100644
    new mode 100755
    
    0 讨论(0)
  • 2020-11-22 02:41

    If you have used chmod command already then check the difference of file, It shows previous file mode and current file mode such as:

    new mode : 755

    old mode : 644

    set old mode of all files using below command

    sudo chmod 644 .

    now set core.fileMode to false in config file either using command or manually.

    git config core.fileMode false
    

    then apply chmod command to change the permissions of all files such as

    sudo chmod 755 .
    

    and again set core.fileMode to true.

    git config core.fileMode true
    

    For best practises don't Keep core.fileMode false always.

    0 讨论(0)
  • 2020-11-22 02:42

    If

    git config --global core.filemode false
    

    does not work for you, do it manually:

    cd into yourLovelyProject folder
    

    cd into .git folder:

    cd .git
    

    edit the config file:

    nano config
    

    change true to false

    [core]
            repositoryformatversion = 0
            filemode = true
    

    ->

    [core]
            repositoryformatversion = 0
            filemode = false
    

    save, exit, go to upper folder:

    cd ..
    

    reinit the git

    git init
    

    you are done!

    0 讨论(0)
  • 2020-11-22 02:44

    Try:

    git config core.fileMode false
    

    From git-config(1):

    core.fileMode
        Tells Git if the executable bit of files in the working tree
        is to be honored.
    
        Some filesystems lose the executable bit when a file that is
        marked as executable is checked out, or checks out a
        non-executable file with executable bit on. git-clone(1)
        or git-init(1) probe the filesystem to see if it handles the 
        executable bit correctly and this variable is automatically
        set as necessary.
    
        A repository, however, may be on a filesystem that handles
        the filemode correctly, and this variable is set to true when
        created, but later may be made accessible from another
        environment that loses the filemode (e.g. exporting ext4
        via CIFS mount, visiting a Cygwin created repository with Git
        for Windows or Eclipse). In such a case it may be necessary
        to set this variable to false. See git-update-index(1).
    
        The default is true (when core.filemode is not specified
        in the config file).
    

    The -c flag can be used to set this option for one-off commands:

    git -c core.fileMode=false diff
    

    And the --global flag will make it be the default behavior for the logged in user.

    git config --global core.fileMode false
    

    Changes of the global setting won't be applied to existing repositories. Additionally, git clone and git init explicitly set core.fileMode to true in the repo config as discussed in Git global core.fileMode false overridden locally on clone

    Warning

    core.fileMode is not the best practice and should be used carefully. This setting only covers the executable bit of mode and never the read/write bits. In many cases you think you need this setting because you did something like chmod -R 777, making all your files executable. But in most projects most files don't need and should not be executable for security reasons.

    The proper way to solve this kind of situation is to handle folder and file permission separately, with something like:

    find . -type d -exec chmod a+rwx {} \; # Make folders traversable and read/write
    find . -type f -exec chmod a+rw {} \;  # Make files read/write
    

    If you do that, you'll never need to use core.fileMode, except in very rare environment.

    0 讨论(0)
  • 2020-11-22 02:48

    undo mode change in working tree:

    git diff --summary | grep --color 'mode change 100755 => 100644' | cut -d' ' -f7- | xargs -d'\n' chmod +x
    git diff --summary | grep --color 'mode change 100644 => 100755' | cut -d' ' -f7- | xargs -d'\n' chmod -x
    

    Or in mingw-git

    git diff --summary | grep  'mode change 100755 => 100644' | cut -d' ' -f7- | xargs -e'\n' chmod +x
    git diff --summary | grep  'mode change 100644 => 100755' | cut -d' ' -f7- | xargs -e'\n' chmod -x
    
    0 讨论(0)
提交回复
热议问题