簡體   English   中英

不能使用 Python(子進程)進行 Git 推送?

[英]Cant Git Push Using Python (Subprocess)?

我有這個腳本,它應該將腳本當前目錄中的文件推送到存儲庫:

def pushToGit():
    currDir = os.path.dirname(os.path.realpath(sys.argv[0]))
    try:
        shutil.rmtree(os.path.join(currDir, '.git'))
    except:
        pass
    try:
        cp = cmd.run("git init", check=True, shell=True, cwd=currDir)
        cp = cmd.run(f"git remote add origin git@github.com:johnsmith/repo_hold.git", check=True, shell=True, cwd=currDir + "//")
        cp = cmd.run("git config user.name 'john smith'", check=True, shell=True, cwd=currDir + "//")
        cp = cmd.run("git config user.email 'john@smith.com'", check=True, shell=True, cwd=currDir + "//")
        cp = cmd.run("git add .", check=True, shell=True, cwd=currDir + "//")
        message = f"Some generated message here"
        cp = cmd.run(f"git commit -m '{message}'", check=True, shell=True, cwd=currDir + "//")
        cp = cmd.run("git push -u origin master", check=True, shell=True, cwd=currDir + "//")
        return True
    except Exception as e:
        return False

過程很簡單:只需初始化文件夾,添加配置,添加文件,用消息提交,然后推送。

在這個例子中,我嘗試使用 SSH 方法,因為它會自動運行,所以它不能輸入密碼。 SSH 密鑰位於同一個文件夾中(例如),並附加到我的 GitHub 帳戶。

但是,我遇到了以下錯誤:

* Running on http://127.0.0.1:2897/ (Press CTRL+C to quit)
Initialized empty Git repository in C:/Users/John/Desktop/my_repo/.git/
warning: LF will be replaced by CRLF in id_rsa.
The file will have its original line endings in your working directory
error: pathspec 'URL' did not match any file(s) known to git
error: pathspec 'added'' did not match any file(s) known to git
127.0.0.1 - - [08/Sep/2020 22:13:36] "←[37mGET /<url post request here> HTTP/1.1←[0m" 200 -

我不確定是什么導致了問題。 我不明白控制台中的錯誤。 這是我第一次使用 SSH 方法,但我不知道我會如何錯誤地使用它。

如果有幫助,這就是目錄中文件的樣子:

目錄視圖

我究竟做錯了什么?

首先,在您的命令之間添加一些 echo 以隔離觸發錯誤的那個。

其次,添加一個git status來檢查:

  • 如果您在正確的工作目錄中
  • 文件的狀態

pathspec 'added'' did not match any file(s) known to git的錯誤與pathspec 'added'' did not match any file(s) known to git似乎表明它正在嘗試使用上一個命令的輸出添加或提交文件。

使用gitpython-developers/GitPython會更安全。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM