GitHub创建项目并关联至本地项目

一、准备工作

  • github账号一枚
  • 安装过git
brew install git mac电脑使用终端安装git
  • 能够访问gitHub网址

有些网络不知道为何访问不了,比如说我现在的网络,只能开了vpn才能访问。。。

二、GitHub上创建项目

创建完成,这边没啥说明的,就按照流程走就OK了的

三、ssh关联

  • 首先先查看ssh是否创建过
image
ls -al ~/.ssh

ps:主要文件是

id_rsa.pub
id_rsa

1.ssh无创建过情况,新增一个ssh

tianledeMacBook-Pro:.ssh xxx$ ssh-keygen -t rsa -b 4096 -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/xxx/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/xxx/.ssh/id_rsa.
Your public key has been saved in /Users/xxx/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:GclQHufCH02UArsAyeYiWvEsE11aOzLQqw6CLOu8F7Y [email protected]
The key's randomart image is:
+---[RSA 4096]----+
|  .+.o+.+...o.   |
|  o.*+ * *.o.    |
|   B+.+ O o..    |
|. = =o o * .     |
|+o =    S .      |
|*..o             |
|o+. o            |
|o .E             |
|.+o              |
+----[SHA256]-----+
ps:
1、邮箱改为自己的邮箱
2、输入完ssh-keygen -t rsa -b 4096 -C "[email protected]"
一路回车!一路回车!一路回车!不要设置密码,不然后面后面每次push和pull都会提示输入密码,会相当的麻烦
3、4096指的是密钥的长度,也可以是2048,肯定是越长越安全喽
  • 复制id_rsa.pub里ssh key的值,也就是文件里的一大串,并在GitHub上绑定ssh


2.ssh有创建过情况

  • 查看ssh使用情况,如原来的需要用在别的地方则可以使用https方式去连接远程仓库,要是还是不行,就删除原来的ssh,重新新建一个
ssh -T [email protected]

ps:
由于我这边是重新创建的ssh文件,所以这边提示成功

三、将本地项目上传绑定至远程仓库

tianledeMacBook-Pro:yongjiatong-parent xxx$ touch README.md
tianledeMacBook-Pro:yongjiatong-parent xxx$ git init
Reinitialized existing Git repository in /java/yongjiatong/yongjiatong-parent/.git/
tianledeMacBook-Pro:yongjiatong-parent xxx$ git add *
tianledeMacBook-Pro:yongjiatong-parent xxx$ git commit -m "项目本地仓库上传"
[master (root-commit) 01442f6] 项目本地仓库上传
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md
tianledeMacBook-Pro:yongjiatong-parent xxx$ git remote add origin [email protected]:zjh793666062/yongjiatong-parent.git
tianledeMacBook-Pro:yongjiatong-parent xxx$ git push -u origin master
The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
To github.com:zjh793666062/yongjiatong-parent.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '[email protected]:zjh793666062/yongjiatong-parent.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
tianledeMacBook-Pro:yongjiatong-parent xxx$ git pull --rebase origin master
warning: no common commits
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:zjh793666062/yongjiatong-parent
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
First, rewinding head to replay your work on top of it...
Applying: 项目本地仓库上传
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging README.md
No changes -- Patch already applied.
tianledeMacBook-Pro:yongjiatong-parent xxx$ git push -u origin master
Branch 'master' set up to track remote branch 'master' from 'origin'.
Everything up-to-date
## 在GitHub创建仓库时没有添加README文件,先创建README.md文件
touch README.md

## Git初始化
git init

## 将文件存至暂存区
git add *

## 将暂存区内容提交到本地仓库
git commit -m "项目本地仓库上传"

## 连接远程仓库(SSH和HTTPS方式都行)
## 使用ssh方式
git remote add origin [email protected]:zjh793666062/yongjiatong-parent.git
## 使用https方式
git remote add origin https://github.com/zjh793666062/yongjiatong-parent.git

## 提交到远程仓库
git push -u origin master

ps:
git push 将当前分支推送至远程同名分支
git push origin [branch-name] 推送本地某分支至远程某分支
git push -u origin [branch-name] 推送本地某分支至远程某分支,并跟踪

## 这边会先报错,原因是远程仓库和本地仓库不匹配,需要先合并,再push
## 先拉取远程仓库master分支,再将本地仓库master分支代码push到远程
git pull --rebase origin master
git push -u origin master


这样就完成了GitHub创建项目并关联至本地项目的动作,有问题可以随时留言哦。
下面附上官方的中文说明文档与官方的SSH绑定说明文档地址

官方说明文档

SSH绑定说明文档地址

你可能感兴趣的:(GitHub创建项目并关联至本地项目)