将项目文件夹推入GitHub的最佳方式



我有一个python项目,我正试图写一个脚本将项目文件夹推入git

应该采取哪些步骤

我试过了https://www.freecodecamp.org/news/automate-project-github-setup-mac/但似乎无法解决它

首先,您需要在Github UI上创建一个repo。创建了repo之后,Github会给你一个这个repo的URL。假设您已经设置了SSH密钥,那么您就可以使用该URL推送您的提交,如下所示:

项目目录:

git init  # initialize a repo; create a .git folder with git internal data
git add file1.py file2.sh file3.js README.md  # choose which files you'd like to upload
git commit -m "Add project files"  # create a commit
git remote add origin "github repo url"
git push  # upload commit to Github

自动执行"第一个推送"对于几个项目来说比较容易。只是要小心你要git添加的文件。git不是为像图像、pdf等二进制文件的版本而设计的。

我使用这个代码。假设您已经设置了SSH密钥。

import os
from datetime import datetime
current_time = datetime.now().strftime("%H:%M:%S")

os.system("git init")
os.system("git remote rm origin")
os.system("git remote add origin git@github.com:user/repository.git")
os.system('git config --global user.email "email@example.com"')
os.system('git config --global user.name "username"')
os.system("git rm -r --cached .")
os.system("git add .")
git_commit_with_time = f'git commit -m "update:{current_time}"'
os.system(git_commit_with_time)
os.system("git push -f --set-upstream origin master")

您也可以根据自己的需要定制。

你可以在提交中使用其他东西来代替current_time

我已经用了好几个月了,我发现这是最好的方法。因为我已经自动完成了这项任务,到目前为止我还没有遇到任何问题。

最新更新