Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

People on my development team keep on pushing build-specific files (folder node_modules and others) onto our repositories despite these files being in a .gitignore file, presumably with git add --all -f or something related to that.

It's a huge pain and getting people to stop doing it is proving difficult.

Is there some way I can make it totally impossible to push certain files onto a repository?

like image 557
Taylor Stevens Avatar asked Feb 15 '16 02:02

Taylor Stevens


People also ask

Which command is used to ignore the files which are not tracked?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a . gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.

Should .gitignore be added to repository?

Normally yes, . gitignore is useful for everyone who wants to work with the repository. On occasion you'll want to ignore more private things (maybe you often create LOG or something. In those cases you probably don't want to force that on anyone else.

How do you exclude or ignore some files from git commit?

Set “–assume-unchanged” to a path to exclude to check on git commit and it will exclude your file from git commit. You will need to use the git update-index and –assume-unchanged to exclude files from git commit.

Is Gitignore part of repo?

If you want the ignored files to be tracked as part of the repo, and shared by other people who check it out, then the . gitignore file(s) must be tracked (checked in and versioned) like any other file. If you don't want the list of ignored files to be checked in and versioned then add filenames to .


1 Answers

Is there some way I can make it totally impossible to push certain files onto a repository?

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.

like image 92
CodeWizard Avatar answered Oct 21 '22 06:10

CodeWizard