如何 git 初始化特定文件或文件夹,排除同一目录中的所有其他文件或文件夹?

How to git init specific file or folder excluding all other files or folder in the same directory?

我有一个包含大量文件和文件夹的目录, 当我 运行 git init 进入它之后它将初始化该文件夹中的所有文件和文件夹,但我想知道是否有 git 仅添加特定文件和文件夹的方法git init.

您会在https://gitpython.readthedocs.io/en/stable/tutorial.html

中找到需要的操作

这里有几行代码可能会让您大致了解如何满足您的需求:

import os
from git import Repo

gitPath="%s/.git" % path
if not os.path.isdir(gitPath):
  print("initializing git repository ...")
  repo=Repo.init(path)
  repo.index.add(...) # add you files and directories here as needed
else:
  repo=Repo(path)