What are some more forceful ways than a .gitignore to keep (force) files out of a repo?

前端 未结 2 881
轮回少年
轮回少年 2020-11-27 07:56

People on my dev team keep on pushing build specific files (node_modules and others) onto our repos despite these files being in a .gitignore file, presumably with git

相关标签:
2条回答
  • Is there some way I can make it totally impossible to push certain files onto a repo?

    Yep, You can use hooks like this to prevent several files to be committed.

    pre-receive hook

    #!/bin/sh
    
    # Check to see if this is the first commit in the repository or not
    if git rev-parse --verify HEAD >/dev/null 2>&1
    then
        # We compare our changes against the previous commit
        against=HEAD^
    else
        # Initial commit: diff against an empty tree object
        against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
    fi
    
    # Redirect output to screen.
    exec 1>&2
    
    # Check to see if we have updated the given file
    if [ $(git diff-tree -r --name-only $against | grep <ANY FILE YOU WANT TO FIND OUT HERE> ) ];
    then
    
        # Output colors
        red='\033[0;31m';
        green='\033[0;32m';
        yellow='\033[0;33m';
        default='\033[0;m';
        
        # personal touch :-)
        echo "${red}"
        echo "                                         "
        echo "                   |ZZzzz                "
        echo "                   |                     "
        echo "                   |                     "
        echo "      |ZZzzz      /^\            |ZZzzz  "
        echo "      |          |~~~|           |       "
        echo "      |        |-     -|        / \      "
        echo "     /^\       |[]+    |       |^^^|     "
        echo "  |^^^^^^^|    |    +[]|       |   |     "
        echo "  |    +[]|/\/\/\/\^/\/\/\/\/|^^^^^^^|   "
        echo "  |+[]+   |~~~~~~~~~~~~~~~~~~|    +[]|   "
        echo "  |       |  []   /^\   []   |+[]+   |   "
        echo "  |   +[]+|  []  || ||  []   |   +[]+|   "
        echo "  |[]+    |      || ||       |[]+    |   "
        echo "  |_______|------------------|_______|   "
        echo "                                         "
        echo "                                         "
        echo "      ${green}You have just committed code  " 
        echo "      ${red}Your code ${yellow}is bad.!!!      "
        echo "      ${red} Do not ever commit again    "
        echo "                                         "
        echo "${default}"
    fi;
    
    # set the exit code to 0 or 1 based upon your needs
    # 0 = good to push
    # 1 = exit without pushing.
    exit 0;
    

    Note:

    Github does not support using hooks in this way.
    They have their own WebHooks

    In this case you can use hooks as well but on the client side.
    The same code can be placed inside pre-commit hook on the client side.

    0 讨论(0)
  • 2020-11-27 08:47

    It's not an easy problem to solve as git is distributed.

    Server side filters like post commit hooks (or commit filter plugins if you are using Stash) allow the users to get themselves into a mess which is not great.

    The client pre-commit hook is cleaner because it prevents the users from polluting their repo but this solution isn't bullet proof.

    You could approach the problem from another perspective and move build artefacts and intermediate files out of you repository directory.

    0 讨论(0)
提交回复
热议问题