使用git把代码上传到github

创建仓库

  1. 注册GitHub帐号,通过邮箱验证
  2. 在GitHub创建仓库
    1. 点击右方的“new repository”按钮创建一个仓库


      使用git把代码上传到github_第1张图片
      image
    2. Repository name框取一个名字
    3. 点击Create repository完成创建

安装git

  1. https://git-scm.com/downloads下载并安装git
    • ubuntu可以使用命令 sudo apt-get install git

设置ssh key

1. 创建用户名和密码

git config --global user.name "hello"
git config --global user.email "[email protected]"

2. 生成ssh密钥

输入命令:

ssh-keygen -t rsa -C "[email protected]"
  • 此处邮箱为github的邮箱

连续按三个回车:

Generating public/private rsa key pair.
Enter file in which to save the key (/home/xuanli/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/xuanli/.ssh/id_rsa.
Your public key has been saved in /home/xuanli/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Fn9I3+UV3NxeMQT4G75KyMjNteTrfxuU+8PSjF1Bkr0 [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|            ..**+|
|           . o +B|
|        . . . o.=|
|         + o + E+|
|        S oo+ +oo|
|      ..= =..o. o|
|       o = +  Bo.|
|          . .+ Bo|
|          .+o.o.+|
+----[SHA256]-----+

出现类似这样的提示代表密钥创建完成。

Your public key has been saved in /home/xuanli/.ssh/id_rsa.pub.

指定了密钥存放位置

3. 添加到github

  • 打开github,点击头像,选择settings
使用git把代码上传到github_第2张图片
image
  • 选择SSH and GPG keys
使用git把代码上传到github_第3张图片
image
  • 点击new SSH key

  • title随便填写,复制id_rsa.pub中的内容,到key中

使用git把代码上传到github_第4张图片
image
  • 点击 Add SSH key

4.检测是否成功

输入命令

ssh -T [email protected]

输入yes,如果HI之后的名字是你github的用户名的话说明成功

上传代码到github

进入项目根目录

1.初始化本地仓库

输入命令

git init

这个命令会在当前目录创建一个 .git 的文件

2.添加文件到暂存区

 git add .

.代表所有,也可以指定文件名

3.将暂存区文件提交到仓库区

git commit -m '版本描述'
  • -m 后边跟得是版本描述

4.创建关联地址

git remote add origin [email protected]:XXX/XXX.git
  • origin 后为github仓库的地址,要选择ssh地址而不是https地址
使用git把代码上传到github_第5张图片
image

5.上传到github

git push -u origin master

上面命令将本地的master分支推送到origin主机,同时指定origin为默认主机。

到现在为止,便上传完成。

你可能感兴趣的:(使用git把代码上传到github)