git建立本地仓库并上传代码到GitHub

一、添加密钥

1.如果没有密钥,则需要创建,并将公钥复制到github

https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

2.如果有密钥,将私钥添加到缓存
ssh-add ~/.ssh/id_ed25519
3.将公钥添加到github的SSH keys设置中
4.本地配置个人信息
git config --global user.name "xxxx" 
git config --global user.email "xxxxx@qq.com"
5.测试连接

ssh -T git@github.com

6.始终缓存私钥配置

编辑 ~/.ssh/config 文件(如果文件不存在,可以创建):

添加以下内容:(IdentityFile替换为自己的私钥)

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa

保存后再次测试连接。

二、新建项目并提交代码

1.在本地项目目录创建本地仓库
git init

输入完命令后项目目录会有一个隐藏的.git文件夹

2.上传所有代码到本地仓库
git add .(后面这个点不能少了)
3.代码上传到本地仓库后,执行提交命令
git commit - m "项目名称"
4.在GitHub上新建一个repository
5.关联本地仓库并上传代码
git remote add origin git@github.com:Sidney-Tan/neovim-configs.git(新建repository后会有一个地址)

或者直接clone ssh的那一项链接。

6.最后执行上传推送命令
git push origin master

(github账户配置里默认main为主分支,可更改为master)
完成代码上传GitHub

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

滚动至顶部