1. GitHub preparation
1. Register a github account
Follow the prompts to register
2. Check the git version
Since macOS has git installed by default
Type in terminal
git -v
![](https://img-blog.csdnimg.cn/img_convert/626cd27d87170ec22ed44a0c8fe39dbe.png)
3. Set username and email
Enter any one username, and email is the email address bound to github
Because Git is a distributed version control system, each machine must report itself: your name and email address.
Pay attention to the --global parameter of the git config command. Using this parameter means that all Git warehouses on your machine will use this configuration. Of course, you can also specify a different user name and email address for a certain warehouse.
git config --global user.name "qyqyqyi"
git config --global user.email "[email protected]"
generate ssh public key
Open a terminal and type
ssh-keygen -t rsa -C "[email protected]"
Note: The content of " [email protected] " is the email address bound by github
Follow the prompts to generate the ssh public key file, the path is /Users/qianying/.ssh/id_rsa.pub
![](https://img-blog.csdnimg.cn/img_convert/33f8c363658eb66fee44dbb139f2027a.png)
Note: press Enter here, that is, there is no password by default, and you do not need to enter a password when verifying whether the binding is successful in the next step
Open the file and copy the contents
![](https://img-blog.csdnimg.cn/img_convert/5589ae9e1aad783d14c409d0682e3685.png)
configure ssh key
Move the mouse to the upper right corner and click [settings]
![](https://img-blog.csdnimg.cn/img_convert/3c54a8395bbf3d0d004fcdc48f2345f2.png)
Click【SSH and GPG keys】->【New SSH key】
![](https://img-blog.csdnimg.cn/img_convert/44247cdfb6d4b7258bd08c9ca7b9082b.png)
Paste the public key you just copied into it
![](https://img-blog.csdnimg.cn/img_convert/99ecaf1d010ec2089562f4cd66ce395c.png)
4. Verify that the git binding is successful
Type in terminal
ssh -T [email protected]
If you press Enter directly, it will display: Host key verification failed.
![](https://img-blog.csdnimg.cn/img_convert/71e6fa6150284c2add1115f73991bf77.png)
You need to enter yes here. If you set a password when generating the public key before, enter the password again here.
![](https://img-blog.csdnimg.cn/img_convert/9ee23aa753c6ebdbef5bdb5cf6fd3980.png)
Second, the use of github
First of all, Git's database is divided into two types: remote database and local database.
Remote database: Equipped with a dedicated server, a database established for sharing by multiple people.
Local database: For the convenience of the user's personal use, the database is configured on his own machine.
1. Local database
1.1 Create a local database
The first step is to create an empty directory locally
mkdir learngit
cd learngit
pwd
The second step is to turn this directory into a warehouse that Git can manage through the git init command
![](https://img-blog.csdnimg.cn/img_convert/3ec47ffc0843a67d9e0c2e456c3501a4.png)
1.2 Adding files to the index
The first step is to create a file sample.txt, enter some content casually, and save
第二步,使用status命令确认工作树和索引的状态
git status
![](https://img-blog.csdnimg.cn/img_convert/7bf6a3d626ff4dd823994b3902a6c01b.png)
从结果我们可以看到‘sample.txt’目前不是历史记录对象。
第三步,使用add命令将sample.txt加入到到索引,就可以跟踪它的变更了
git add sample.txt
git status
![](https://img-blog.csdnimg.cn/img_convert/6fb613ae3af7bf925f3c26606525194a.png)
1.3提交文件至本地数据库
git commit -m "xxx"
-m后面的引号里的内容是本次提交的说明,可以输入任意内容,当然最好是有意义的,这样你就能从历史记录里方便地找到改动记录。
现在使用命令commit将sample.txt文件提交到本地数据库
![](https://img-blog.csdnimg.cn/img_convert/5d71b4018e6120492a3d12f6e22b1b8b.png)
使用log命令,我们可以在数据库的提交记录看到新的提交。
git log
![](https://img-blog.csdnimg.cn/img_convert/1f73054fb5d444f43306442da3074ab4.png)
2.共享数据库
此时,我们已经在本地创建了一个Git仓库后,又想在GitHub创建一个Git仓库,并且让这两个仓库进行远程同步,这样,GitHub上的仓库既可以作为备份,又可以让其他人通过该仓库来协作。
2.1创建共享数据库(远程库)
第一步,登陆github,点击左上角的【create repository】或者右上角的【加号】->【New repository】来创建一个新的仓库
![](https://img-blog.csdnimg.cn/img_convert/7aee6166d804be98d0cd17d5c3b9cba5.png)
输入仓库名字,创建仓库
![](https://img-blog.csdnimg.cn/img_convert/a7cf2f4d53ea52dfff93bde85bb98de6.png)
创建成功,提示告诉我们可以创建一个新的仓库,或者将已有的仓库推送到共享数据库上。这里,我们将已有的本地仓库推送到github上的仓库。
![](https://img-blog.csdnimg.cn/img_convert/ccf9b2b7f091767be04043134b5fc9a2.png)
git remote add origin https://github.com/qyqyqyi/learngit.gitgit branch -M maingit push -u origin main
添加后,远程库的名字就是origin,这是Git默认的叫法,也可以改成别的
branch命令创建一个名字为main的分支
然后使用push命令将本地的main分支推送到GitHub上,可以使用 -u 参数指定一个默认主机,这样后面就可以不加任何参数使用git push,即将本地的main分支与GitHub上新的main分支相关联。
![](https://img-blog.csdnimg.cn/img_convert/576a83ac033532b1e20edd3ec3c1523d.png)
结果显示:2021年8月13日移除对密码认证的支持。请改用个人访问令牌。
获取个人访问令牌过程详见2.3
获取个人令牌后,将个人令牌代替密码输入,成功。
![](https://img-blog.csdnimg.cn/img_convert/204ab62f3affb79b1f78c64765cd6e0a.png)
推送成功后,可以立刻在GitHub页面中看到远程库的内容已经和本地一模一样
![](https://img-blog.csdnimg.cn/img_convert/a5a4e56b6484b070a5e398f8576f648b.png)
从现在起只要本地提交了,就可以同构下面的命令将本地最新main分支推送至GitHub。
git push origin main
2.2删除共享数据库(远程库)
如果添加的时候,地址写错了,就可以使用git remote rm 命令。使用前,建议先用git remote -v查看远程库信息:
![](https://img-blog.csdnimg.cn/img_convert/c2596a2f7da37a933caa884c04bfc08a.png)
然后,根据名字删除,比如删除origin
git remote rm origin
注意:这里的删除只是断开了本地和github之间的连接,并没有真正的删除GitHub上的库。如果想要真正的删除,需要登录GitHub,进行删除。
2.3个人访问令牌获取步骤
1.点击右上角->【settings】
2.在左下角找到【develop settings】
![](https://img-blog.csdnimg.cn/img_convert/a2a385d34b3486bb6d24de700acd8d88.png)
注:GitHub支持两种个人访问令牌:
第一种是fine-grained personal access token,即细粒度个人访问令牌
第二种是personal access token (classic),即个人访问令牌(分类化)
GitHub建议我们使用第一种,和第二种对比,它有许多优点,这里不再赘述,可以去官网查看。
3.点击【 Personal access tokens】->【Fine-grained tokens】->【Generate new token】
![](https://img-blog.csdnimg.cn/img_convert/9e04d2b5fdfa518ac7221dcb5e2639ec.png)
填写Token name、Expiration、Resource owner资源所有者
Repository access仓库访问权限根据需求选择,这里选择的是第二种,即这适用于资源所有者拥有的所有当前和未来存储库。还包括公共存储库(只读)。
![](https://img-blog.csdnimg.cn/img_convert/6f64333dbf3e17cf604925b15e97ca86.png)
根据需求进行选择权限(这里我也不是很懂,所以先全选了,之后再研究),最后点击创建令牌。
![](https://img-blog.csdnimg.cn/img_convert/c5794cedaf05e31e0409d1895589cf1d.png)
使用token替换 push命令中的密码即可(token自己保存,刷新后就没了)
三、总结
首先做好了使用github的准备工作:1.注册github账号2.查看git命令版本3.设置username和email4.生成ssh公钥5.配置ssh密钥6.验证git绑定成功
我们先创建本地库,再创建远程库,最后关联远程库。
touch xxx.txt //新建一个txt文件
git init //初始化本地仓库
git add xxx.txt //添加刚刚创建的README文档
git commit -m “注释" //提交到本地仓库,并写一些注释
git remote add origin https://github.com/yourname/xxxx.git //连接远程仓库并建了一个名叫:origin的别名,yourname记得替换成你的用户名,XXXX为你的版本库的名字
git branch -M main //创建一个分支名叫main的分支
git push -u origin main //将本地仓库的文件提交到别名为origin的地址的main分支下,-u为第一次提交,需要创建main分支,之后push时就不需要-u选项了
git push origin main // 将本地仓库合并到别名为origin地址的main分支下