Basic use of github under Mac (with detailed process)

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

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]" 
  • Follow the prompts to generate the ssh public key file, the path is /Users/qianying/.ssh/id_rsa.pub

  • 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

configure ssh key

  • Move the mouse to the upper right corner and click [settings]

  • Click【SSH and GPG keys】->【New SSH key】

  • Paste the public key you just copied into it

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.

  • You need to enter yes here. If you set a password when generating the public key before, enter the password again here.

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

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
  • 从结果我们可以看到‘sample.txt’目前不是历史记录对象。

  • 第三步,使用add命令将sample.txt加入到到索引,就可以跟踪它的变更了

git add sample.txt
git status

1.3提交文件至本地数据库

git commit -m "xxx"
  • -m后面的引号里的内容是本次提交的说明,可以输入任意内容,当然最好是有意义的,这样你就能从历史记录里方便地找到改动记录。

  • 现在使用命令commit将sample.txt文件提交到本地数据库

  • 使用log命令,我们可以在数据库的提交记录看到新的提交。

git log

2.共享数据库

  • 此时,我们已经在本地创建了一个Git仓库后,又想在GitHub创建一个Git仓库,并且让这两个仓库进行远程同步,这样,GitHub上的仓库既可以作为备份,又可以让其他人通过该仓库来协作。

2.1创建共享数据库(远程库)

  • 第一步,登陆github,点击左上角的【create repository】或者右上角的【加号】->【New repository】来创建一个新的仓库

  • 输入仓库名字,创建仓库

  • 创建成功,提示告诉我们可以创建一个新的仓库,或者将已有的仓库推送到共享数据库上。这里,我们将已有的本地仓库推送到github上的仓库。

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分支相关联。

  • 结果显示:2021年8月13日移除对密码认证的支持。请改用个人访问令牌。

  • 获取个人访问令牌过程详见2.3

  • 获取个人令牌后,将个人令牌代替密码输入,成功。

  • 推送成功后,可以立刻在GitHub页面中看到远程库的内容已经和本地一模一样

  • 从现在起只要本地提交了,就可以同构下面的命令将本地最新main分支推送至GitHub。

git push origin main

2.2删除共享数据库(远程库)

  • 如果添加的时候,地址写错了,就可以使用git remote rm 命令。使用前,建议先用git remote -v查看远程库信息:

  • 然后,根据名字删除,比如删除origin

git remote rm origin
  • 注意:这里的删除只是断开了本地和github之间的连接,并没有真正的删除GitHub上的库。如果想要真正的删除,需要登录GitHub,进行删除。

2.3个人访问令牌获取步骤

  • 1.点击右上角->【settings】

  • 2.在左下角找到【develop settings】

注:GitHub支持两种个人访问令牌:

  • 第一种是fine-grained personal access token,即细粒度个人访问令牌

  • 第二种是personal access token (classic),即个人访问令牌(分类化)

  • GitHub建议我们使用第一种,和第二种对比,它有许多优点,这里不再赘述,可以去官网查看。

  • 3.点击【 Personal access tokens】->【Fine-grained tokens】->【Generate new token】

  • 填写Token name、Expiration、Resource owner资源所有者

  • Repository access仓库访问权限根据需求选择,这里选择的是第二种,即这适用于资源所有者拥有的所有当前和未来存储库。还包括公共存储库(只读)。

  • 根据需求进行选择权限(这里我也不是很懂,所以先全选了,之后再研究),最后点击创建令牌。

  • 使用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分支下

Guess you like

Origin blog.csdn.net/qyqyqyi/article/details/128652728