【问题标题】:Best way to push the project folder into GitHub将项目文件夹推送到 GitHub 的最佳方法
【发布时间】:2021-10-11 14:27:57
【问题描述】:

我有一个 python 项目,我正在尝试编写一个脚本来将项目文件夹推送到 git

应该采取哪些步骤

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

【问题讨论】:

  • 你应该解释你的具体问题和你尝试了什么
  • 我的问题是制作一个脚本来将实际项目推送到github
  • 您从该教程中的脚本中遇到了什么问题?
  • 顺便说一句,我会警惕 自动化 将东西推送到 github。大多数体面的编辑器都有 git 集成,但你不能自动化提交:你必须手动完成,否则毫无价值。同上推/拉/合并/分支。 git push 并不是很难做到,但任何自动化解决方案要么是 git push 的别名,要么在您有不兼容的更改时处理所有边缘情况时必须非常复杂

标签: python shell github


【解决方案1】:

首先,您需要在 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 不是为图像、pdf 等二进制文件而设计的。

【讨论】:

  • 我可以在shell脚本中写同样的吗
  • 当然。这些都是 bash 命令。但是,如果您打算使用文件夹名称来参数化 Github 存储库 URL,则需要创建与它们所在的文件夹同名的存储库。
  • 提示:如果您的所有存储库都使用相同的语言(例如:python),请在所有这些文件夹中添加 standard python gitignore。然后你就可以运行git add .gitignore ; git add .,而不是手动选择文件。
【解决方案2】:

我使用此代码。假设您已设置 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

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

【讨论】:

    猜你喜欢
    • 2021-12-22
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    相关资源
    最近更新 更多