GitHub基本操作
一、为什么要使用Git&GitHub
Git是目前世界上最流行的代码版本管理工具 、 GitHub是通过Git进行版本控制的软件源代码托管服务平台,汇聚了世界上最优秀的开源项目核开发者。
二、什么是版本控制
Git是分布式版本控制软件
三、安装 Git、初始化仓库并做最简单的配置
安装
在git官网下载适合版本: ,下载之后按照提示安装。
安装完成之后,可以在终端验证一下 ,输入 git --version,返回当前Git版本,就说明安装成功了。
localhost:~ lizhao$ git --version
git version 2.18.0
初始化Git仓库
mkdir learn_git 新建一个文件夹;cd learn_git 找到文件夹;ls -al 看下文件夹里有哪些内容,发现只有2个内容;git init 初始化项目后,再ls -al 看下文件夹里有哪些内容,发现多了一条内容
localhost:desktop lizhao$ mkdir learn_git
localhost:desktop lizhao$ cd learn_git
localhost:learn_git lizhao$ ls -al
total 0
drwxr-xr-x 2 lizhao staff 64 2 25 16:45 .
drwx------@ 34 lizhao staff 1088 2 25 16:45 ..
localhost:learn_git lizhao$ git init
Initialized empty Git repository in /Users/lizhao/Desktop/learn_git/.git/
localhost:learn_git lizhao$ ls -al
total 0
drwxr-xr-x 3 lizhao staff 96 2 25 16:50 .
drwx------@ 34 lizhao staff 1088 2 25 16:45 ..
drwxr-xr-x 10 lizhao staff 320 2 25 16:50 .git
localhost:learn_git lizhao$
配置用户名和邮箱
localhost:learn_git lizhao$ git config --global user.name "superzhao"
localhost:learn_git lizhao$ git config --global user.email "116229934@qq.com"
localhost:learn_git lizhao$ git config --global --list
user.name=superzhao
user.email=116229934@qq.com
完成一个最简单的Git操作流程:工作区、暂存区和仓库
在learn_git文件夹下新建一个test.html,git status查看当前状态,git提示当前有一个文件未被跟踪,可以使用git add命令去跟踪
localhost:learn_git lizhao$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.html
nothing added to commit but untracked files present (use "git add" to track)
localhost:learn_git lizhao$
运行git add test.html之后,可以看到提示,git已经追踪到了这个new file,同时还提示,有一些变更需要去提交, (git rm --cached
localhost:learn_git lizhao$ git add test.html
localhost:learn_git lizhao$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: test.html
localhost:learn_git lizhao$
跟着git提示,去做提交,然后运行git status,提示当前没有需要提交的内容,工作树是干净的
localhost:learn_git lizhao$ git commit -m "creat"
[master (root-commit) eb4b462] creat
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.html
localhost:learn_git lizhao$ git status
On branch master
nothing to commit, working tree clean
localhost:learn_git lizhao$
刚刚的html文件的经历了3个状态
对文件新建、编辑后是在工作区进行的,这时文件的状态是已修改;当运行git add,文件的状态就变成了已暂存,并且从工作区移到了暂存区;运行git commit 命令,文件的状态就变成了已提交,同时从暂存区移到了仓库区。

git log 可以查看之前的提交日记
四、将本地仓库同步到远程GitHub仓库
新建一个仓库

HTTPS 和SSH区别
新建完成之后,有HTTPS 和SSH种同步方式,
HTTPS:使用用户名和个人访问令牌(Personal Access Token)进行认证
2021年8月后,GitHub不再支持直接用密码认证
每次推送可需要输入凭据(可配置缓存)
SSH:使用公钥和私钥对进行认证
需要生成SSH密钥对,将公钥添加到GitHub账户
配置后无需重复输入凭据
配置 GitHub 的 SSH 密钥
配置 GitHub 的 SSH 密钥主要分为本地生成密钥、和上传公钥到 GitHub 两个步骤
生成一个新的SSH密钥
ssh-keygen -t ed25519 -C "116229934@qq.com"
提示 "Enter a file in which to save the key" 时,直接按回车接受默认路径(如 ~/.ssh/id_ed25519)。
提示 "Enter passphrase" 时,可以直接回车跳过,也可以输入一个密码短语来增加一层安全保护
localhost:~ lizhao$ ssh-keygen -t ed25519 -C "1162299734@qq.com"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/lizhao/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/lizhao/.ssh/id_ed25519
Your public key has been saved in /Users/lizhao/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:nFb/zOLoI0u037JlUmMNJNjJJzkT+hBuVPRcbPGrU14 116229934@qq.com
The key's randomart image is:
+--[ED25519 256]--+
| o*=+..o. |
| o.oO=..o. |
| = .=+. .|
| o = . o .|
| S . = .oE|
| o . o =+ .|
| o . +o+. |
| ...oB .. |
| .+=+o |
+----[SHA256]-----+
localhost:~ lizhao$
复制公钥内容
需要将公钥(.pub 文件)的内容复制到剪贴板,以便下一步粘贴到GitHub网站上。
cat ~/.ssh/id_ed25519.pub
执行命令后,终端会显示一串以 ssh-ed25519 开头、以你的邮箱结尾的字符。将它完整地选中并复制
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICWjSpbu1rLF5W8OFOsIa6RZgPXnLDVerUZt6BeOvWEL 116229934@qq.com
在GitHub网站上添加公钥
登录 GitHub 网站,点击右上角你的头像,然后选择 Settings 。
在左侧边栏中,点击 SSH and GPG keys 。
点击绿色的 New SSH key 按钮 。
Title 字段:为这个密钥起一个容易识别的名字,比如 "My Personal Laptop" 。
Key Type 字段:保持默认的 "Authentication Key" 即可 。
Key 字段:将你在第三步中复制的公钥内容粘贴到这个大文本框中 。
点击 Add SSH key,并根据提示确认你的GitHub密码 。
验证连接
最后,在终端中运行以下命令,测试SSH连接是否配置成功
ssh -T git@github.com
如果看到类似下面的成功消息,就说明一切都配置好了!
Hi superzhao! You've successfully authenticated, but GitHub does not provide shell access.
localhost:~ lizhao$
浙公网安备 33010602011771号