首页 > 解决方案 > GithubAPI 提交没有快进

问题描述

因为有些日子我尝试与 GitHub API 交互以创建提交。我的问题是 GIT 中的结果,在对新提交的引用更新之后。Github 每次都会返回:

Status Code 422: {
    "message": "Update is not a fast forward",
    "documentation_url": "https://docs.github.com/enterprise/2.22/rest/reference/git#update-a-reference"
}

但我不想在主分支上创建一个快进提交,只是一个新的提交。我忘记了哪个参数或者我设置了错误?

为了重现我使用邮递员的错误:

  1. 创建 Blob - https://{{server}}/api/v3/repos/:owner/:repo/git/blob
{
    "content":"This is my content at {{currentDate}}", 
    "encoding":"utf-8"

}

-> 将 response.sha 保存在“shaBlob”中。
2. getRef - https://{{server}}/api/v3/repos/:owner/:repo/git/ref/heads/master
-> 将 response.object.sha 保存在“shaMaster”中。
3. getLastCommit - https://{{server}}/api/v3/repos/:owner/:repo/git/commits/:commit_sha
-> 在“shaMasterTree”中保存 response.tree.sha
4. 创建树 - https ://{{server}}/api/v3/repos/:owner/:repo/git/trees
正文:

{
    "base_tree": "{{shaCommitLast}}",
    "tree": [{
        "path": "readme.md",
        "mode": "100644",
        "type": "blob",
        "sha": "{{shaBlob}}"
    }]
}

-> 在 shaTreeNew 中保存 response.sha
5. 创建提交 - https://{{server}}/api/v3/repos/:owner/:repo/git/commits 正文:

{
    "message": "This is a bot Commit",
    "tree": "{{shaTreeNew}}",
    "author": {
        "name": "ASLob",
        "email": "aslob@mydomain.com",
        "date": "{{currentDate}}"
    },
    "committer": {
        "name": "ASLob",
        "email": "aslob@mydomain.com",
        "date": "{{currentDate}}"
    }
}

-> 在 shaCommitNew 中保存 response.sha 6. 更新参考 - https://{{server}}/api/v3/repos/:owner/:repo/git/refs/:ref 正文:

{
    "sha": "{{shaCommitNew}}",
    "force": false
}

标签: gitgithubgithub-api

解决方案


推荐阅读