Как верно составить .gitignore для игнорирования папок?

Рейтинг: 0Ответов: 1Опубликовано: 14.06.2023

У меня есть такое дерево каталогов:

PS C:\Users\Seven\Documents\cmake_tests> tree   
Структура папок
Серийный номер тома: C85A-2EBA
C:.
├───.vscode
└───Ep2
    ├───build
    │   └───CMakeFiles
    │       ├───3.26.4
    │       │   ├───CompilerIdCXX
    │       │   │   ├───Debug
    │       │   │   │   └───CompilerIdCXX.tlog
    │       │   │   └───tmp
    │       │   └───x64
    │       │       └───Debug
    │       │           └───VCTargetsPath.tlog
    │       ├───CMakeScratch
    │       ├───e661cb9c5b653261656c96b2e40641de
    │       └───pkgRedirects
    └───source

Я хочу, чтобы при добавлении в репозиторий папки .vscode и build (и их содержимое) игнорировались, но не папка source. Для этого я создаю .gitignore с кодировкой UTF-8 и LF такого вида:

CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
*/build/*
*/.vscode/*
*/.vs/*

После внесения изменений в некоторые файлы из папки source, делаю следующие команды:

PS C:\Users\Seven\Documents\cmake_tests> git add .
warning: LF will be replaced by CRLF in .vscode/c_cpp_properties.json.
The file will have its original line endings in your working directory

PS C:\Users\Seven\Documents\cmake_tests> git status
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   .gitignore.txt
        new file:   .vscode/c_cpp_properties.json
        new file:   Ep2/build/ALL_BUILD.vcxproj
        new file:   Ep2/build/ALL_BUILD.vcxproj.filters
        new file:   Ep2/build/CMakeCache.txt
        new file:   Ep2/build/CMakeFiles/3.26.4/CMakeCXXCompiler.cmake
        new file:   Ep2/build/CMakeFiles/3.26.4/CMakeDetermineCompilerABI_CXX.bin
        new file:   Ep2/build/CMakeFiles/3.26.4/CMakeRCCompiler.cmake
        new file:   Ep2/build/CMakeFiles/3.26.4/CMakeSystem.cmake
        new file:   Ep2/build/CMakeFiles/3.26.4/CompilerIdCXX/CMakeCXXCompilerId.cpp
        new file:   Ep2/build/CMakeFiles/3.26.4/CompilerIdCXX/CompilerIdCXX.exe
        new file:   Ep2/build/CMakeFiles/3.26.4/CompilerIdCXX/CompilerIdCXX.vcxproj
        new file:   Ep2/build/CMakeFiles/3.26.4/CompilerIdCXX/Debug/CMakeCXXCompilerId.obj
        new file:   Ep2/build/CMakeFiles/3.26.4/CompilerIdCXX/Debug/CompilerIdCXX.exe.recipe
        new file:   Ep2/build/CMakeFiles/3.26.4/CompilerIdCXX/Debug/CompilerIdCXX.tlog/CL.command.1.tlog
        new file:   Ep2/build/CMakeFiles/3.26.4/CompilerIdCXX/Debug/CompilerIdCXX.tlog/CL.read.1.tlog
        new file:   Ep2/build/CMakeFiles/3.26.4/CompilerIdCXX/Debug/CompilerIdCXX.tlog/CL.write.1.tlog
        new file:   Ep2/build/CMakeFiles/3.26.4/CompilerIdCXX/Debug/CompilerIdCXX.tlog/CompilerIdCXX.lastbuildstate
        new file:   Ep2/build/CMakeFiles/3.26.4/CompilerIdCXX/Debug/CompilerIdCXX.tlog/link.command.1.tlog
        new file:   Ep2/build/CMakeFiles/3.26.4/CompilerIdCXX/Debug/CompilerIdCXX.tlog/link.read.1.tlog
        new file:   Ep2/build/CMakeFiles/3.26.4/CompilerIdCXX/Debug/CompilerIdCXX.tlog/link.write.1.tlog
        new file:   Ep2/build/CMakeFiles/3.26.4/VCTargetsPath.txt
        new file:   Ep2/build/CMakeFiles/3.26.4/VCTargetsPath.vcxproj
        new file:   Ep2/build/CMakeFiles/3.26.4/x64/Debug/VCTargetsPath.recipe
        new file:   Ep2/build/CMakeFiles/3.26.4/x64/Debug/VCTargetsPath.tlog/VCTargetsPath.lastbuildstate
        new file:   Ep2/build/CMakeFiles/CMakeConfigureLog.yaml
        new file:   Ep2/build/CMakeFiles/TargetDirectories.txt
        new file:   Ep2/build/CMakeFiles/cmake.check_cache
        new file:   Ep2/build/CMakeFiles/e661cb9c5b653261656c96b2e40641de/generate.stamp.rule
        new file:   Ep2/build/CMakeFiles/generate.stamp
        new file:   Ep2/build/CMakeFiles/generate.stamp.depend
        new file:   Ep2/build/CMakeFiles/generate.stamp.list
        new file:   Ep2/build/HelloAppBinary.vcxproj
        new file:   Ep2/build/HelloAppBinary.vcxproj.filters
        new file:   Ep2/build/Helloapp.sln
        new file:   Ep2/build/ZERO_CHECK.vcxproj
        new file:   Ep2/build/ZERO_CHECK.vcxproj.filters
        new file:   Ep2/build/cmake_install.cmake
        modified:   Ep2/source/main.cpp

PS C:\Users\Seven\Documents\cmake_tests>

Почему были добавлены файлы из папок build и .vscode? Может неверно составлена маска в gitignore? Я также пробовал ставить просто слэш после названия папки, но резульатат тот же - ненужные папки добавляются. Различные варианты беру отсюда. Как же тогда верно составить .gitignore для игнорирования ненужных папок?

Ответы

▲ 1Принят

Комментарий пользователя @andreymal помог решить проблему. Действительно, нужно было убрать расширение у файла .gitignore