git 创建版本库 2 种方式:
- git init : 直接将一个文件夹变成一个仓库
mkdir learn-git
cd learn-git
git init
- git clone : 从已有仓库克隆下来同名仓库
git clone https://github.com/xxxx/remote-repo.git

git 三个工作区域:
- 工作区 Working Directory
- 暂存区 Staging Area/Index
- 本地仓库 Local Repository

git 三个仓库的推进关系

文件所处状态与操作

git 提交文件步骤
git init // 创建仓库
git status // 查看仓库的状态
git add // 文件添加到暂存区
git commit // 提交文件到本地仓库
git log [--oneline] // 查看提交日志

git reset 回退到之前的某一个“版本”
git reset 回退有3种模式:
- soft 模式:保留工作区和暂存区内容
- hard 模式:删除工作区和暂存区内容
- mixed 模式:只保留工作区内容

例如:soft 模式回退
git reset --soft 5af90b8

例子: hard 模式回退
git reset --hard HEAD^ // 使用 hard 回退模式回退到上一版本(HEAD^ 指上一版本)

- git reset soft 和 mixed 使用场景是当你觉得仓库中几个版本的提交没有太大意义时,可以通过这2中模式进行版本的回退,然后重新 add 和 commit, 提交一个新的有意义的版本。
- git reset hard 模式是你真的要放弃本地所有要修改内容的时候,要谨慎使用 hard 模式。但是如果不小心误操作的话,可以使用 git reflog 命令来查看我们操作的历史记录 。通过 git reflog 可以查询到误操作命令的前一个版本号。
git diff 查看差异

git diff // 比较工作区和暂存区的差异
git diff HEAD // 比较工作区和版本库的差异
git diff --cached // 比较暂存区和版本库的差异
git diff 5abdefd 6dfdgsd // 比较2个版本直接的差异
git diff HEAD^ HEAD // 比较前一版本和当前版本差异
git diff HEAD~ HEAD // 比较前一版本和当前版本差异
git diff HEAD~2 HEAD // 比较 HEAD 之前的 2 个版本
git diff HEAD~2 HEAD file3.txt // 只比较指定文件这间的版本差异
git rm 删除文件
rm file // window 或 linux 下删除“工作区”文本
git add file // 只有加上只一步才能完整从暂存区删除文件
git rm file // 把文件从工作区和暂存区同时删除
git rm --cached file // 把文件从暂存区删除,但是保留在当前工作区中

ignore git 应该忽略文件
.ignore 文件生效的前提是忽略的文件不能是已经被添加到版本库里面的。


git 远程仓库
- 可以通过 git remote -v 命令查看当前仓库所对应远程仓库的别名和地址
git remote -v

- 将本地仓库与远程仓库关联起来
git remote add origin git@github.com/xxx/first-repo.git // origin 就是我们远程仓库的别名
git branch -M main // 指定分支的名称为 main
git push -u origin main // 将本地的 main 分支与远程 origin main 分支关联起来 -u --up-stream-to
git pull origin main:main // 拉取远程仓库内容
网友评论