通常情况下,在本地新建一个仓库之后,需要在远端网页端也新建一个空的同名仓库,然后将两者进行关联才能推送。

那有没有办法直接在命令行就完成从新建到推送的过程而不需要中间在网页端也操作一番呢?办法当然是有的。

下面以一个示例来说明一下。

1. 准备测试文件

首先做一些文件的准备,作为此次实验新建的本地仓库 gittest 以及新建本地文件 README.md 并向此文件写入 # git test 的内容,当前文件夹目录结构为 tree 指令结果所示。

$ mkdir gittest

$ cd gittest

$ touch README.md

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

$ tree .

.
└── README.md

2. 初始化本地仓库

对本地仓库进行初始化

$ git init

$ git add README.md

$ git commit -m "update README"

提交时如果出现下面的提示,表示我们需要首先认证自己的身份,以向项目表明是谁在提交。

*** 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.

根据提示操作即可,如下示例。

$ 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

Ubuntu Terminal命令行新建仓库并推送到远程仓库的更多相关文章

  1. Git 创建远程仓库并克隆到本地,创建本地仓库并推送到远程仓库

    配置用户信息 配置的是你个人的用户名称和电子邮件地址.这两条配置很重要,每次 Git 提交时都会引用这两条信息,说明是谁提交了更新,会随更新内容一起被永久纳入历史记录 git config --glo ...

  2. Git创建本地仓库并推送至远程仓库

    作为一名测试同学,日常工作经常需要checkout研发代码进行code review.自己极少有机会创建仓库,一度以为这是一个非常复杂过程.操作一遍后,发现也不过六个步骤,so,让我们一起揭开这神秘面 ...

  3. Git操作_本地仓库第一次推送到远程仓库

    实现目的: 本地已经安装好Git,pycham已经有一个项目,打算放到Git远程仓库 前提条件:本地配置好了公钥,且GIT 上关联好公钥,步骤如下: git本地仓库连接github操作步骤:windo ...

  4. 使用git命令将本地项目推送到远程仓库

    将本地项目推送到远程仓库 这里先放一张图, 有助于理解git命令 1. 在GitHub上新建一个仓库 注意不要勾选自动生成README.md文件, 否则会产生某些问题, README.md文件到时可以 ...

  5. git将本地仓库推送到远程仓库

    如何将本地仓库推送到公司远程仓库? 1:前提是你本地安装好git.先把远程git仓库克隆到本地  git clone 远程仓库的地址(SSH) 2: git  branch //查看本地分支 3: g ...

  6. 如何把已有的本地git仓库,推送到远程新的仓库(github private)并进行远程开发;

    最近因为疫情,在家干活,连接不上之前的gitlab 服务器:所以不得把现有的代码迁移到github 的私有仓库来进行开发:下面简要记录迁移的过程: 首先,确保你已经配置好本地访问远程私有仓库的所有权限 ...

  7. git 本地项目推送至远程仓库

    1 在本地文件夹下创建一个 Git 仓库(如test目录下) git init 2 此时test文件夹即是你的maste主分支,你可以在改文件夹下写自己的项目 3 将test文件夹下的内容提交至暂存区 ...

  8. Git如何把本地代码推送到远程仓库

    Git如何把本地代码推送到远程仓库 1. 初始化版本库 $ git init 2. 添加文件到版本库(只是添加到缓存区),.代表添加文件夹下所有文件 $ git add . 3. 把添加的文件提交到版 ...

  9. Git本地初始化并推送到远程仓库

    git常用命令 1.全局配置git用户名邮箱 git config --global user.name '你的名字' git config --global user.email '你的邮箱地址' ...

随机推荐

  1. Maven笔记之核心概念及常用命令

    Maven的核心概念 Maven是一款服务于java平台的自动化构建工具. 自动化构建工具还有:make->ant->maven->gradle       1.约定的目录  2.P ...

  2. Java读取系统默认时区

    工作中,遇到一个Java读取默认时区的问题,后来看了openjdk的源码,大致整理一下过程 public class Test { public void test(){ TimeZone.getDe ...

  3. css 14-CSS3属性详解:Web字体

    14-CSS3属性详解:Web字体 #前言 开发人员可以为自已的网页指定特殊的字体(将指定字体提前下载到站点中),无需考虑用户电脑上是否安装了此特殊字体.从此,把特殊字体处理成图片的方式便成为了过去. ...

  4. css进阶 00-准备

    前言 css 进阶的主要内容如下. #1.css 非布局样式 html 元素的分类和特性 css 选择器 css 常见属性(非布局样式) #2.css 布局相关 css 布局属性和组合解析 常见布局方 ...

  5. C#爬虫,让你不再觉得神秘

    1.使用第三方类库 HtmlAgilityPack 官方网址:https://html-agility-pack.net/?z=codeplex. // From File 从文件获取html信息 v ...

  6. 一个小技巧助您减少if语句的状态判断

    作者:依乐祝 首发地址:https://www.cnblogs.com/yilezhu/p/14174990.html 在进行项目的开发的过程中, if 语句是少不了的,但我们始终要有一颗消灭 if  ...

  7. Centos7.6上部署Supervisor来监控和操作各类服务

    supervisor    是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统.它可以很方便的监听.启动.停止.重启一个 ...

  8. net core 3.1使用ElasticSearch 全文搜索引擎

    ElasticSearch 是一个开源的搜索引擎,建立在一个全文搜索引擎库 Apache Lucene 基础之上. Lucene 可以说是当下最先进.高性能.全功能的搜索引擎库,无论是开源还是私有. ...

  9. AutoMapper的源码分析

    最近有一个小项目需要提供接口给第三方使用,接口会得到一个大的XML的字符串大约有8个对象100多个字段,在映射到Entity只能通过反射来赋值避免重复的赋值,但是明显感觉到性能下降严重,因为以前接触过 ...

  10. 后端Long类型传到前端精度丢失的正确解决方式

    原因:前端js对Long类型支持的精度不够,导致后端使用的Long传到前端丢失精度,比如现在分布式id生成算法"雪花算法"在使用中就会出现问题. 解决方式: 1.后端的Long类型 ...