Search code examples
gitgitpython

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


I have a directory containing lots of files and folders , when I run git init after going inside it then it will init all files and folder in that folder, but I want to know if there is a git way of adding only specific files and folder to that git init.


Solution

  • You'll find the need operations described in https://gitpython.readthedocs.io/en/stable/tutorial.html

    here is a few lines of code that might give you a picture of how to approach your need:

    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)