1.创建github仓库

(1)点击右上角的头像

image

(2)点击Your repositories

image

(3)点击New

image

(4)填写好Owner和Repository name点击Add a README file选择license,再点击Create repository

image

2.用git上传本地仓库到github仓库

git工作流:
image

(1)点击Code再点击Local查看github仓库地址

image

(2)将github仓库里的LICENSE和README.md文件克隆到工作区目录

cd your_workspace_dir
git clone https://github.com/your_user_name/your_repository_name.git
cp your_repository_name/LICENSE .
cp your_repository_name/README.md .
rm -r your_repository_name

git clone 的是你的github仓库https地址
用ls命令可以看到工作区目录多出了LICENSE和README.md

(3)初始化工作区目录

用git status命令可以看到没有git仓库

git init

再用git status命令可以查看git状态
用ls -a命令可以看到工作区目录多出了.git目录

(4)新建并切换为主分支

用git status可以看到现在是master分支

git checkout -b main

用git status可以看到现在是main分支

(5)把工作区目录文件添加到缓存区

注意不要添加敏感信息如密码,住址等

git add .

用git status可以看到缓存区新增的文件

(6)把缓存区文件提交到本地仓库

git commit -m "upload my repository"

参数-m是message的意思
会出现警告,那么

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

用git status命令可以看到缓存区清空了没有东西可以提交

(7)把本地仓库上传到github仓库

从CLI用用户密码登陆github的方式被移除了,可以用token或ssh登陆

用token的方法:

一.找到github相关设置

右上角头像->Settings->Developer settings->Personal access tokens->Tokens (classic)->Generate new token->Generate new token (classic)

二.输入github用户密码

三.选择范围repo

四.复制token

五.把本地仓库上传到github仓库

git push -u https://your_token@github.com/your_user_name/your_repository_name main -f

注意使用-f参数可能会导致问题
最后在网页上就可以看到上传的仓库了

用ssh方法:

一.创建.ssh文件夹(有.ssh文件夹则跳过)

cd ~/
mkdir .ssh

二.进入.ssh文件夹

cd .ssh

三.生成密钥

注意要生成足够强的密钥

ssh-keygen -b 4096 -t rsa

输入文件,输入密码
用ls命令会发现多出类似id_rsa, id_rsa.pub的文件

四.复制公钥

cat id_rsa.pub

注意id_rsa.pub是你自己的公钥文件
复制出现的内容

五.把公钥粘贴到github上

找到 右上角头像->Settings->SSH and GPG keys->New SSH key

填上Title,把公钥粘贴到Key,点击Add SSH key

六.把本地仓库上传到github仓库

在第2.用git上传本地仓库到github仓库中
(1)点击Code再点击Local查看github仓库地址
步骤中找到github仓库ssh地址

git remote add origin git@github.com:your_user_name/your_repository_name.git

上面添加的是你的github仓库的ssh地址

git push -u origin main -f  

注意使用-f参数可能会导致问题
输入生成密钥时设置的密码
最后就可以看到上传的仓库了