git的管理工具
1.0 git安装
Git可以在Linux、Unix、Mac和Windows这几大平台上正常运行使用。
下载地址
1.2. 初次运行 Git 前的配置
打开 git 命令行工具
# 配置 用户名
git config --global user.name xxx
# 配置 邮箱
git config --global user.email xxx
# 查看用户名
git config user.name
1.3 创建版本库
git init

1.4 添加到暂存区
现在我们的代码 和 git 仓库 还没有直接的联系 查看 git 仓库状态 输入 git status
输入 git add *
对所有文件进行跟踪

因此,我们可以 随时的 输入
git status
来查看当前仓库的状态,来获得 提示。
1.5 提交本地仓库
暂存区的意思 只是暂时存储文件,当需要把对文件的操作 永久存储下来时,需要在把暂存区中的文件提交到到本地仓库。
入以下命令进行提交,同时 还需要备注 信息 如 初始化项目 、 新增了购物接口 等。
git commit -m
"初始化项目"
重新输入 git status 查看git 仓库状态

1.5.1总结
把刚才操作的流程,换成专业的术语。
- 工作目录 :刚才操作的文件夹 我的第一个网站 就称为 工作目录
- 暂存区 : 是用来存放 对文件进行了 跟踪,但是还没有 提交到 本地仓库 的地方
-
本地仓库: 最终实现 文件版本管理的地方。
1525853953043.png
1.6 忽略文件
有时候,在工作目录下的某些文件,是属于私人的或者是项目运行所产生的临时文件,并不需要添加到 版本控制中。 如 新增一个文件 私人密码

查看git仓库状态
git status

此时 我们可以使用 git 规定的一个文件 .gitignore 在里面指定需要过滤的文件
直接在windows 右键 新建文件 gitignore 会创建失败。
使用命令行的方式创建
touch .gitignore
编辑 .gitignore 文件 直接写入 要忽略的文件名即可
# 忽略该文件
私人密码.txt
查看git仓库状态
git status

大部分情况下,我们也需要将 .gitignore 文件一起提交到本地仓库中实行版本控制
添加到暂存区 该文件 使用 git add * 无效 需要手动指定文件名
git add .gitignore
提交到本地仓库
git commit -m "添加了忽略文件列表"
1.6.1 忽略文件语法
-
语法大部分和正则类似
-
空行或是以#开头的行即注释行将被忽略;
-
语法大部分和正则类似
-
空行或是以#开头的行即注释行将被忽略;
# 这种是注释
-
以斜杠 “/” 结尾表示目录;
css/
-
以星号 “*” 通配多个字符;
*.js
-
以问号 “?” 通配单个字符
-
以方括号 “[]” 包含单个字符的匹配列表;
-
以叹号 “!” 表示不忽略(跟踪)匹配到的文件或目录;
-
可以在前面添加斜杠 “/” 来避免递归
# 忽略根目录下的 css 文件夹 /css # 忽略所有的css文件夹 css/
2.1 推送到远程仓库
因为在团队开发中,我们的项目文件是需要和组员进行分享的,所以实现这个功能,就必须得借助远程仓库。
远程仓库只是 本地仓库的一个备份。
-
github做为最著名的git仓库托管商,是行业内的绝对权威.它给无数的开发者提供了共同学习发展的平台.
-
码云是github的国产版,对国人做了针对性的优化,在国内也是使用者众多
我们主要演示 github的使用,码云 强烈建议 课下 自己学习使用。
2.2 注册 github
先注册一个 github帐号

2,3 新建远程仓库
1 一个github帐号可以建立多个远程仓库,一般 一个项目使用一个仓库。

2.4 填写仓库信息

2.5创建成功

2.6 推送到远程仓库
远程仓库建立完毕之后,我们可以 将 之前的 本地仓库 我的第一个网站 推送到上面新建的 test 远程仓库上
先记录远程仓库的地址

https://github.com/itcastWsy/test.git
把远程仓库 记录在一个 变量 origin 上 该名字可自定义
git remote add origin https://github.com/itcastWsy/test.git
在推送到远程仓库之前,先确保 本地仓库已经 执行过 commit 了,这样 才会保证 本地仓库 和远程仓库一致。
查看本地仓库状态
git status

开始推送
git push -u origin master
提示输入用户名 ,直接输入即可 如 itcastWsy 然后按下回车

提示输入密码

没有看到报错,就是 推送成功

同时,刷新一下 github 页面 看到远程仓库上 显示出 仓库的信息了

2.7 从远程仓库克隆
假设 建立本地仓库、 推送到远程仓库 的工作都是项目经理完成了。此时,你做为一个新加入项目的 程序猿,要做的事就是从远程仓库上 克隆 代码。
--问项目经理拿 远程仓库的地址
https://github.com/itcastWsy/test.git
在你的电脑上任意目录下(如 桌面) 开始克隆
在桌面上 打开 git bash 命令行工具 输入
git clone https://github.com/itcastWsy/test.git

2.8 从远程仓库获取更新
此时,项目经理的代码 和 你的代码 是一模一样的。现在 项目经理 新增了一个文件 home.html,并把它提交到远程仓库上。
新建 home.html 文件
touch home.html
添加到暂存区
git add *
提交到本地仓库
git commit -m "新增了home.html"
推送到远程仓库
git push

查看远程仓库

那么现在 做为程序员的你 需要把代码 进行更新
回到 test 文件夹内,打开 git bash 命令行

输入以下命令 进行更新
git pull

2.9 git clone 和 git pull 的区别
- git clone 是克隆,只需要执行一次
- git pull 是 更新,后期反复使用
3.0 还原文件到上次commit状态
假设做为程序员的你,刚刚把代码更新下来,便上厕所去了,这个时候你的熊孩子趁你不在,对着你的键盘就是一顿 啪啪啪 ,把你 home.html 文件敲得面目全非。
你现在想要做的事,就是把 home.html 还原到 拉取下来的状态。
原来的home.html 内容是空的。(你是不知道里面的内容的)
熊孩子 把 home.html 改成了
<h1>钱多话少死的早</h1>
开始还原,在 test 目录下,输入命令行
git checkout home.html

如果想要还原多个文件,可以
- 还原文件夹 css 文件夹
git checkout css
- 还原当前目录的所有文件
git checkout ./
3.1 查看版本历史
做为 新加入项目的你,想要了解 这个项目,到底做过了哪些版本,想要看到之前每一次提交时的备注信息
此时,你的 test 文件夹内 输入 命令进行查看
git log

3.2 还原到某一个版本
做为 新加入项目的你,来获取了最新的代码之后,发现项目太大了,你不好去学习和了解 其中的某一个模块的功能和代码。如

此时,可以把整个项目 还原到 完成了登录功能的状态
记录 该版本的 commit 字段

3db7762c593251f1a78e518fdd3ed6d6cad626bc
开始还原 (commit 字段 最少 写 6位)
git reset -–hard 3db7762c593251f1a78e518fdd3ed6d6cad626bc

如果,在次状态下又想回到 最新的版本 新增了home.html 呢 查看提交信息
git log
发现 完成登录功能 之后的日志信息 丢失了。

此时,输入
git reflog

还原到最新的版本
git reset --hard 5038cc9

3.3. 小结
- git checkout xxx 只能还原文件到上一个版本
- git reset --hard 'commit的id' 可以还原到任意版本
- git reflog 可以查看丢失了的版本的日志信息
4.0 配置ssh
我们把文件从本地仓库推送到远程仓库的方式有两种
- HTTPS 每次都要手动输入 用户名和密码
- SSH 配置证书后,不用手动输入用户名和密码
4.1 配置证书
在git bash 命令行中输入
····
ssh-keygen -t rsa -C "邮箱地址"
····
然后一直按回车。直到出现如下界面 代表本地 证书生成成功

输入命令 打印密钥
····
cat ~/.ssh/id_rsa.pub
····

按以下步骤进行粘贴即可

输入
····
ssh -T git@github.com
····
出现以下界面代表成功。如果失败,建议多尝试几次。

4.2 将提交方式 HTTPS 改为 SSH
复制 SSH 地址

····
git@github.com:itcastWsy/test.git
····
修改 origin 地址
因为之前已经将 地址 存入 origin 变量了 。查看 origin
····
git remote -v
····

此时,将origin的地址 改为 ssh 地址即可
····
git remote set-url origin git@github.com:itcastWsy/test.git
····
重新查看 git remote -v 发现修改成功

按照以上步骤执行完毕之后,再次推送到远程仓库时,就不用再输入用户名和密码了。
5.0 常用git命令
注解 | 命令 |
---|---|
git reset HEAD XXX | 从暂存区移出 |
git diff | 查看编辑过的文件和 版本库的区别 |
git config user.name xxx | 配置当前仓库的用户名 |
git config user.email xxx | 配置当前仓库的邮箱 |
git init | 初始化 git 仓库 |
git add xxx | 添加到暂存区 |
git commit -m "备注" | 提交到本地仓库 |
git commit -m "备注" -a | git add 和 git commit 的综合 |
git remote -v | 查看远程仓库地址 |
git remote add 远程仓库名 远程仓库地址 | 添加远程仓库地址 |
git remote rm 远程仓库名 | 删除远程仓库 |
git remote set-url 远程仓库名 远程仓库地址 | 修改远程仓库地址 |
git push 远程仓库地址 master | 提交到远程仓库 |
git push 远程仓库地址 master -u | 提交到远程仓库 (以后 git push 即可) |
git clone 远程仓库地址 | 克隆仓库 |
git pull | 拉取更新 |
ssh-keygen -t rsa -C "邮箱地址" | 生成 ssh证书 |
cat ~/.ssh/id_rsa.pub | 查看ssh证书 |
git reset --hard "commit Id" | 还原到某版本 |
git reset --hared HEAD^ | 还原到上一个版本 |
git log | 查看版本历史 |
git reflog | 查看更强大的版本历史 |
git checkout 文件名 | 还原文件到上一个版本 |
git branch 分支名 | 创建分支 |
git checkout 分支名 | 切换到分支 |
git merge 分支名 | 合并分支 |
git branch | 查看分支 |
git branch -d 分支名 | 删除分支 |