How can I add a .gitignore file to all my GitLab projects?

Other than writing a bash for-loop, is there a way to quickly add a standard .gitignore file to all my GitLab git repos at once?

I don’t think this is supported, but could be a nice feature.

For now, I think each GitLab organization can define their own .gitconfig file for reference, encourage users to default their computer to those patterns with git config --global core.excludesfile .gitignore, and/or check a .gitignore into each individual repo.

1 Like

Following is a simple quick and dirty bash script I wrote to run on our source repositories before syncing with our GitLab server.

I’ve run it successfully from GitBash on Windows and of course on Linux.

From the local repositories root directory, I run the following script. It will search for all _‘git init’_ed repository folders (.git) under the current directory. If a .gitignore file is present, it searches for the Section text contained in SString. If it doesn’t exist, then the script will add the Section text followed by the wildcards/files to ignore. If no .gitignore file exists, a new one is created with the required definitions.

There probably are better ways of doing this, but this fit my needs.

$> cat GitIgnores.sh

#!/bin/bash
## Create .gitignore file if it doesn't exist.
##  and/or update it to ignore video files if needed.

SString="## Ignore video files"

# Change the shell Internal Field Separator from default
#  'whitespace' ({space}, {tab}, {newline}) to 'newline-only' for
#  find results processing. This will correctly handle
#  spaces in file/path names. Remember to UNSET at end.
IFS=$'\n'
for DIR in $(/usr/bin/find ./ -type d -name ".git"); do
        echo -e "\nFound git repository '${DIR}'..."
        if [ ! -f "${DIR}"/.gitignore ] || [ ! -z $(grep '${SString}' "${DIR}"/.gitignore) ]; then
                # Create .gitignore file and set to ignore video files
                echo "Creating/updating .gitignore file in '${DIR}/.gitignore' ..."
                /bin/echo "${SString}" >> "${DIR}"/.gitignore
                /bin/echo -e "*.m4a\n*.mov\n*.mp4" >> "${DIR}"/.gitignore
        else
                echo "'${DIR}/.gitignore' file is up to date..."
        fi
done
unset IFS
## End ##