GitHub API Push接口

最近研究一下GItHub API,想通过API直接提交文件,先把关键信息放上来,后面再来排版。

注意

1、需要先在GitHub上创建一个token,方法:
Settings–>Developer settings–>Personal access tokens–>Generate new token,选择对应的权限即可。
2、curl命令不要回车,最后是在编写完命令后把所有回国符都删除掉,不然就会报错:无法解析Json格式。

1、获取仓库的信息

curl  https://api.github.com/repos/bookmark/bmarks
{ ......
  "url": "https://api.github.com/users/bookmark",
  "html_url": "https://github.com/bookmark",
  "followers_url": "https://api.github.com/users/bookmark/followers",
  "following_url": "https://api.github.com/users/bookmark/following{/other_user}",
  "gists_url": "https://api.github.com/users/bookmark/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/bookmark/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/bookmark/subscriptions",
  "organizations_url": "https://api.github.com/users/bookmark/orgs",
  "repos_url": "https://api.github.com/users/bookmark/repos",
  "events_url": "https://api.github.com/users/bookmark/events{/privacy}",
  "received_events_url": "https://api.github.com/users/bookmark/received_events",
  "type": "User",
  "site_admin": false
  },
  "private": false,
  "html_url": "https://github.com/bookmark/bmarks",
  "description": null,
  "fork": false,
  "url": "https://api.github.com/repos/bookmark/bmarks",
  "forks_url": "https://api.github.com/repos/bookmark/bmarks/forks",
  "keys_url": "https://api.github.com/repos/bookmark/bmarks/keys{/key_id}",
  "collaborators_url": "https://api.github.com/repos/bookmark/bmarks/collaborators{/collaborator}",
  "teams_url": "https://api.github.com/repos/bookmark/bmarks/teams",
  "hooks_url": "https://api.github.com/repos/bookmark/bmarks/hooks",
  "issue_events_url": "https://api.github.com/repos/bookmark/bmarks/issues/events{/number}",
  "events_url": "https://api.github.com/repos/bookmark/bmarks/events",
  "assignees_url": "https://api.github.com/repos/bookmark/bmarks/assignees{/user}",
  "branches_url": "https://api.github.com/repos/bookmark/bmarks/branches{/branch}",
  "tags_url": "https://api.github.com/repos/bookmark/bmarks/tags",
  "blobs_url": "https://api.github.com/repos/bookmark/bmarks/git/blobs{/sha}",
  "git_tags_url": "https://api.github.com/repos/bookmark/bmarks/git/tags{/sha}",
  "git_refs_url": "https://api.github.com/repos/bookmark/bmarks/git/refs{/sha}",
  "trees_url": "https://api.github.com/repos/bookmark/bmarks/git/trees{/sha}",
  "statuses_url": "https://api.github.com/repos/bookmark/bmarks/statuses/{sha}",
  "languages_url": "https://api.github.com/repos/bookmark/bmarks/languages",
  "stargazers_url": "https://api.github.com/repos/bookmark/bmarks/stargazers",
  "contributors_url": "https://api.github.com/repos/bookmark/bmarks/contributors",
  "subscribers_url": "https://api.github.com/repos/bookmark/bmarks/subscribers",
  "subscription_url": "https://api.github.com/repos/bookmark/bmarks/subscription",
  "commits_url": "https://api.github.com/repos/bookmark/bmarks/commits{/sha}",
  "git_commits_url": "https://api.github.com/repos/bookmark/bmarks/git/commits{/sha}",
  "comments_url": "https://api.github.com/repos/bookmark/bmarks/comments{/number}",
  "issue_comment_url": "https://api.github.com/repos/bookmark/bmarks/issues/comments{/number}",
  "contents_url": "https://api.github.com/repos/bookmark/bmarks/contents/{+path}",
  "compare_url": "https://api.github.com/repos/bookmark/bmarks/compare/{base}...{head}",
  "merges_url": "https://api.github.com/repos/bookmark/bmarks/merges",
  "archive_url": "https://api.github.com/repos/bookmark/bmarks/{archive_format}{/ref}",
  "downloads_url": "https://api.github.com/repos/bookmark/bmarks/downloads",
  "issues_url": "https://api.github.com/repos/bookmark/bmarks/issues{/number}",
  "pulls_url": "https://api.github.com/repos/bookmark/bmarks/pulls{/number}",
  "milestones_url": "https://api.github.com/repos/bookmark/bmarks/milestones{/number}",
  "notifications_url": "https://api.github.com/repos/bookmark/bmarks/notifications{?since,all,participating}",
  "labels_url": "https://api.github.com/repos/bookmark/bmarks/labels{/name}",
  "releases_url": "https://api.github.com/repos/bookmark/bmarks/releases{/id}",
  "deployments_url": "https://api.github.com/repos/bookmark/bmarks/deployments",
  .......
}

2、创建仓库
下面两种方法都可以,意义相同

curl -X POST -H "Content-Type: application/json" https://api.github.com/user/repos?access_token=796365420b6bb92eca9d02ammm309a8032f0571e -d "{'name': 'Hello-World'}"

curl -X POST -H "Authorization: token 796365420b6bb92eca9d02ammm309a8032f0571e" -H "Content-Type: application/json" https://api.github.com/user/repos -d '{"name": "Hello-World"}'

3、获取文件内容
比如:

curl https://api.github.com/repos/bookmark/bmarks/contents/README.md

4、获取一个文件或目录或链接或子模块的内容

GET /repos/:owner/:repo/contents/:path

#参数:
path  string  The content path.
ref   string  The name of the commit/branch/tag. Default: the repository’s default branch (usually master)

To get a repository’s contents recursively, you can recursively get the tree. This API has an upper limit of 1,000 files for a directory. If you need to retrieve more files, use the Git Trees API. This API supports files up to 1 megabyte in size.

官方链接:https://developer.github.com/v3/repos/contents/#get-contents

4、创建一个文件

PUT /repos/:owner/:repo/contents/:path

#参数
path    string  Required. The content path.
message string  Required. The commit message.
content string  Required. The new file content, Base64 encoded.
branch  string  The branch name. Default: the repository’s default branch (usually master)
curl -X PUT -H "Content-Type: application/json" https://api.github.com/repos/bookmark/bmarks/contents/dir/test?access_token=796365420b6bb92eca9d02ammm309a8032f0571e -d '{"message": "my commit message", "content": "bXkgbmV3IGZpbGUgY29udGVudHM="}'

5、更新一个文件

PUT /repos/:owner/:repo/contents/:path

#参数
path            string  Required. The content path.
message         string  Required. The commit message.
content         string  Required. The updated file content, Base64 encoded.
sha             string  Required. The blob SHA of the file being replaced.
branch          string  The branch name. Default: the repository’s default branch (usually master)

sha值为上一次操作文件返回的值,如丢失可以通过下面的第6条获取

curl -X PUT -H "Content-Type: application/json" https://api.github.com/repos/bookmark/bmarks/contents/dir/test?access_token=796365420b6bb92eca9d02ammm309a8032f0571e -d '{"message": "my commit message","content": "bXkgdXBkYXRlZCBmaWxlIGNvmmmlbnRz","sha": "0d5a690c8fad5e605a6e8766295d9d459d65de42"}'

6、获取提交信息

GET /repos/:owner/:repo/commits

#参数
sha     string  SHA or branch to start listing commits from. Default: the repository’s default branch (usually master).
path    string  Only commits containing this file path will be returned.
author  string  GitHub login or email address by which to filter by commit author.
since   string  Only commits after this date will be returned. This is a timestamp in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ.
until   string  Only commits before this date will be returned. This is a timestamp in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ.

所有提交信息,倒叙
curl https://api.github.com/repos/bookmark/bmarks/commits
最后一次提交信息
curl https://api.github.com/repos/bookmark/bmarks/commits/master

7、删除文件

DELETE /repos/:owner/:repo/contents/:path

#参数
path    string  Required. The content path.
message string  Required. The commit message.
sha     string  Required. The blob SHA of the file being replaced.
branch  string  The branch name. Default: the repository’s default branch (usually master)

你可能感兴趣的:(Git)