我比较笨,喜欢简单粗暴的。总体来说就是干三件事:
- 建立本地仓库,建立github仓库
- 建立本地仓库与github网站的连接,为本地仓库管理员(就是我)授权,能够pull(从github取回资源)还能push(把我本地仓库的东东放到github中备份)
- 初始化git,配置git,进行push 和pull
1.1 建立本地仓库
比如我现在用vim的markdown写个笔记
mkdir yuan_note
cd yuan_note
然后巴拉巴拉往里面放了很多东西,这就是我的本地仓库,一个文件夹,搞定啦!
1.2 建立github仓库
当然是申请个账号了,有了账号之后,点击右上角加号,New repository
image.png
2.1 建立两者连接
首先申请个ssh (security shell),先到根目录创建ssh文件夹,然后创建.ssh
cd
mkdir .ssh
ssh-keygen -t rsa -C "你的github邮箱"
image.png
copy ssh钥匙
pbcopy <~/.ssh/id_rsa.pub
在你的github网址,右上角头像,setting点进去 SSH and GPG keys -》New SSH key
Title 随便起一个,比如邮箱+一些标识, key里面,把刚才复制的粘贴进去
image.png
然后把证书和github关联,测试连接
ssh -T git@github.com
然后输入yes
image.png
如果有问题,github有很多帮助找错误的
https://docs.github.com/en/authentication/troubleshooting-ssh/error-permission-denied-publickey
https://docs.github.com/en/get-started/getting-started-with-git/managing-remote-repositories
以上就是给我的github配个锁,然后我这里有个ssh的钥匙可以开锁的过程。
2.2 为本地仓库管理员(就是我)授权
git config --global user.name "随便起个名字"
git config --global user.email "我的github邮箱"
3. 初始化git,配置git,进行push 和pull
cd yuan_note
git init
git add .
git commit -m "添加的备注"
git remote add my_origin https://github.com/XXXX/yuan_note.git //你刚才创建的github的repository,以https开头以.git结尾的
//0. 这时候可以检测一下你的remote仓库也就是上一步设置的是否正确
git remote -v
//0.1 如果设置错误了
git remote remove my_origin
// 1. 取回资源,从刚才设置的my_origin 的master分支取回数据到这个文件夹中
git pull --rebase https://github.com/XXXX/yuan_note.git
//或者直接
git pull my_origin
Git在202012后将不支持使用密码push,如果大家直接
git push my_origin master会发现报错为
remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.
remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information
这时候需要在github----setting----1. [Developer settings]----Personal access tokens-----Generate new token
然后把他copy下来,记作密码YYA
再输入
//2. 将本地(本地就是刚才git init这个文件夹)push到github备份,将这个文件夹的数据发射到刚才设置的my_origin的master分支中取
git push -u my_origin main
//Username for 'https://github.com':
//Password for 'https://yzmhust@gmai.com@github.com': [输入刚才copy的密码YYA]
就可以了!