Ubuntu Terminal命令行新建倉庫並推送到遠程倉庫

一般狀況下,在本地新建一個倉庫以後,須要在遠端網頁端也新建一個空的同名倉庫,而後將二者進行關聯才能推送。html

那有沒有辦法直接在命令行就完成重新建到推送的過程而不須要中間在網頁端也操做一番呢?辦法固然是有的。node

下面以一個示例來講明一下。git

1. 準備測試文件github

首先作一些文件的準備,做爲這次實驗新建的本地倉庫 gittest 以及新建本地文件 README.md 並向此文件寫入 # git test 的內容,當前文件夾目錄結構爲 tree 指令結果所示。api

$ mkdir gittest

$ cd gittest

$ touch README.md

$ echo "# git test" >> README.md

$ tree .

.
└── README.md

2. 初始化本地倉庫ssh

對本地倉庫進行初始化curl

$ git init

$ git add README.md

$ git commit -m "update README"

提交時若是出現下面的提示,表示咱們須要首先認證本身的身份,以向項目代表是誰在提交。ide

*** Please tell me who you are.

Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

根據提示操做便可,以下示例。svn

$ git config --global user.email "phillee2016@163.com"

$ git config --global user.name "philleer"

$ git commit -m "update README"

至此本地倉庫已經創建完成,而且修改保存到 stage 當中。測試

3. 本地新建遠程倉庫

正常的流程是登錄 GitHub,在左上角點擊 New,新建一個遠程倉庫,而後再本地經過

$ git remote add origin https://github.com/philleer/gittest.git

將已有的本地倉庫與之關聯,而後將本地倉庫內容推送到GitHub上。

如今咱們經過命令行來實現這一操做。

$ curl -u 'philleer' https://api.github.com/user/repos -d '{"name":"gittest"}'

只須要把上面命令行中的 philleer 改爲本身的用戶名,gittest 改爲本身的新建倉庫名。

此時會讓你輸入登錄密碼,按指示輸入便可。

Enter host password for user 'philleer':

輸入以後回車確認,出現下面的結果表示已經新建成功。

{
  "id": 183884632,
  "node_id": "MDEwOlJlcG9zaXRvcnkxODM4ODQ2MzI=",
  "name": "gittest",
  "full_name": "philleer/gittest",
  "private": false,
  "owner": {
    "login": "philleer",
    "id": 22950478,
    "node_id": "MDQ6VXNlcjIyOTUwNDc4",
    "avatar_url": "https://avatars1.githubusercontent.com/u/22950478?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/philleer",
    "html_url": "https://github.com/philleer",
    "followers_url": "https://api.github.com/users/philleer/followers",
    "following_url": "https://api.github.com/users/philleer/following{/other_user}",
    "gists_url": "https://api.github.com/users/philleer/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/philleer/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/philleer/subscriptions",
    "organizations_url": "https://api.github.com/users/philleer/orgs",
    "repos_url": "https://api.github.com/users/philleer/repos",
    "events_url": "https://api.github.com/users/philleer/events{/privacy}",
    "received_events_url": "https://api.github.com/users/philleer/received_events",
    "type": "User",
    "site_admin": false
  },
  "html_url": "https://github.com/philleer/gittest",
  "description": null,
  "fork": false,
  "url": "https://api.github.com/repos/philleer/gittest",
  "forks_url": "https://api.github.com/repos/philleer/gittest/forks",
  "keys_url": "https://api.github.com/repos/philleer/gittest/keys{/key_id}",
  "collaborators_url": "https://api.github.com/repos/philleer/gittest/collaborators{/collaborator}",
  "teams_url": "https://api.github.com/repos/philleer/gittest/teams",
  "hooks_url": "https://api.github.com/repos/philleer/gittest/hooks",
  "issue_events_url": "https://api.github.com/repos/philleer/gittest/issues/events{/number}",
  "events_url": "https://api.github.com/repos/philleer/gittest/events",
  "assignees_url": "https://api.github.com/repos/philleer/gittest/assignees{/user}",
  "branches_url": "https://api.github.com/repos/philleer/gittest/branches{/branch}",
  "tags_url": "https://api.github.com/repos/philleer/gittest/tags",
  "blobs_url": "https://api.github.com/repos/philleer/gittest/git/blobs{/sha}",
  "git_tags_url": "https://api.github.com/repos/philleer/gittest/git/tags{/sha}",
  "git_refs_url": "https://api.github.com/repos/philleer/gittest/git/refs{/sha}",
  "trees_url": "https://api.github.com/repos/philleer/gittest/git/trees{/sha}",
  "statuses_url": "https://api.github.com/repos/philleer/gittest/statuses/{sha}",
  "languages_url": "https://api.github.com/repos/philleer/gittest/languages",
  "stargazers_url": "https://api.github.com/repos/philleer/gittest/stargazers",
  "contributors_url": "https://api.github.com/repos/philleer/gittest/contributors",
  "subscribers_url": "https://api.github.com/repos/philleer/gittest/subscribers",
  "subscription_url": "https://api.github.com/repos/philleer/gittest/subscription",
  "commits_url": "https://api.github.com/repos/philleer/gittest/commits{/sha}",
  "git_commits_url": "https://api.github.com/repos/philleer/gittest/git/commits{/sha}",
  "comments_url": "https://api.github.com/repos/philleer/gittest/comments{/number}",
  "issue_comment_url": "https://api.github.com/repos/philleer/gittest/issues/comments{/number}",
  "contents_url": "https://api.github.com/repos/philleer/gittest/contents/{+path}",
  "compare_url": "https://api.github.com/repos/philleer/gittest/compare/{base}...{head}",
  "merges_url": "https://api.github.com/repos/philleer/gittest/merges",
  "archive_url": "https://api.github.com/repos/philleer/gittest/{archive_format}{/ref}",
  "downloads_url": "https://api.github.com/repos/philleer/gittest/downloads",
  "issues_url": "https://api.github.com/repos/philleer/gittest/issues{/number}",
  "pulls_url": "https://api.github.com/repos/philleer/gittest/pulls{/number}",
  "milestones_url": "https://api.github.com/repos/philleer/gittest/milestones{/number}",
  "notifications_url": "https://api.github.com/repos/philleer/gittest/notifications{?since,all,participating}",
  "labels_url": "https://api.github.com/repos/philleer/gittest/labels{/name}",
  "releases_url": "https://api.github.com/repos/philleer/gittest/releases{/id}",
  "deployments_url": "https://api.github.com/repos/philleer/gittest/deployments",
  "created_at": "2019-04-28T09:10:16Z",
  "updated_at": "2019-04-28T09:10:16Z",
  "pushed_at": "2019-04-28T09:10:17Z",
  "git_url": "git://github.com/philleer/gittest.git",
  "ssh_url": "git@github.com:philleer/gittest.git",
  "clone_url": "https://github.com/philleer/gittest.git",
  "svn_url": "https://github.com/philleer/gittest",
  "homepage": null,
  "size": 0,
  "stargazers_count": 0,
  "watchers_count": 0,
  "language": null,
  "has_issues": true,
  "has_projects": true,
  "has_downloads": true,
  "has_wiki": true,
  "has_pages": false,
  "forks_count": 0,
  "mirror_url": null,
  "archived": false,
  "disabled": false,
  "open_issues_count": 0,
  "license": null,
  "forks": 0,
  "open_issues": 0,
  "watchers": 0,
  "default_branch": "master",
  "permissions": {
    "admin": true,
    "push": true,
    "pull": true
  },
  "allow_squash_merge": true,
  "allow_merge_commit": true,
  "allow_rebase_merge": true,
  "network_count": 0,
  "subscribers_count": 0
}
示例結果

4. 關聯本地倉庫與遠程倉庫

而後將本地倉庫與之關聯,並推送本地倉庫內容。按指示輸入帳戶密碼便可。

$ git remote add origin https://github.com/philleer/gittest.git

$ git push -u origin master

Username for 'https://github.com': philleer

Password for 'https://philleer@github.com':
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 264 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/philleer/gittest.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

這樣就完成了!不用每次都打開網頁新建倉庫,讓後再回到本地進行關聯了。

本地一條龍,全程無切換。


參考資料

[1] 命令行方式下在Github上新建遠程倉庫並PUSH https://blog.csdn.net/csm201314/article/details/78254777

相關文章
相關標籤/搜索