我有一個 python 專案,我正在嘗試撰寫一個腳本將專案檔案夾推送到 git
應該采取哪些步驟
我嘗試過 https://www.freecodecamp.org/news/automate-project-github-setup-mac/ 但似乎無法修復它
uj5u.com熱心網友回復:
首先,您需要在 Github UI 上創建一個 repo。創建存盤庫后,Github 將為您提供該存盤庫的 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 等。
uj5u.com熱心網友回復:
我用這個代碼。假設您設定了 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 [email protected]:user/repository.git")
os.system('git config --global user.email "[email protected]"')
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提交中的 。
我已經使用它好幾個月了,我發現它是最好的方法。因為我已經自動化了任務,所以到目前為止我沒有遇到任何問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/313255.html
