我是GitPython的新手,我正在尝试将python程序阶段本身开发到一个新的git存储库(我的-新的-回购)。
我的main.py如下:
import git
repo = git.Repo.init('my-new-repo')
# List all branches
for branch in repo.branches:
print(branch)
# Provide a list of the files to stage
repo.index.add(['main.py'])
# Provide a commit message
repo.index.commit('Initial commit')文件树:
├── main.py
├── my-new-repo (directory)
├── .git但是当我运行它时,它会返回以下错误:
No such file or directory: 'main.py'
Traceback (most recent call last):
File "/home/aaron/Downloads/GitPython/main.py", line 17, in <module>
repo.index.add(['main.py'])
File "/home/aaron/Downloads/GitPython/git/index/base.py", line 815, in add
entries_added.extend(self._entries_for_paths(paths, path_rewriter, fprogress, entries))
File "/home/aaron/Downloads/GitPython/git/util.py", line 144, in wrapper
return func(self, *args, **kwargs)
File "/home/aaron/Downloads/GitPython/git/index/util.py", line 109, in set_git_working_dir
return func(self, *args, **kwargs)
File "/home/aaron/Downloads/GitPython/git/index/base.py", line 694, in _entries_for_paths
entries_added.append(self._store_path(filepath, fprogress))
File "/home/aaron/Downloads/GitPython/git/index/base.py", line 639, in _store_path
st = os.lstat(filepath) # handles non-symlinks as well
FileNotFoundError: [Errno 2] No such file or directory: 'main.py'
Process finished with exit code 1发布于 2022-10-27 01:46:09
GitPython的repo.index.add函数从repo的目录中对文件进行分级。git.Repo.init('my-new-repo')在my目录(可能是新的)中创建一个新的回购。如果main.py不在回购目录中,那么GitPython将无法看到它。
要解决这个问题,您可以将main.py复制到repo的目录中。如下所示:
import git
import shutil
repo = git.Repo.init('my-new-repo')
# List all branches
for branch in repo.branches:
print(branch)
# copy main.py into my-new-repo
shutil.copy('main.py', 'my-new-repo/main.py')
# Provide a list of the files to stage
repo.index.add(['main.py'])
# Provide a commit message
repo.index.commit('Initial commit')https://stackoverflow.com/questions/74215818
复制相似问题