一、git的基础知识

git是一个分布式代码版本管理控制系统

  • 记录当前产品代码的所有版本信息(历史修改信息),而且方便快速回退到某一个具体的版本
  • 方便团队协作开发,能够检测代码冲突,能够合并代码等

svn:在git诞生前就已经存在的版本控制系统,不过它是“集中式”管理
git:是分布式版本管理体统

  • 1.集中式版本控制系统
  • 2.分布式版本控制系统

image.png

二、git的工作管理和基础操作

1.安装

git --version 查看版本

2.配置

git config --list查看配置

git config <配置属性> ''设置配置

  • —local局部
  • —global 全局
  • —system系统

$ git config –global user.name xxx $ git config –global user.email xxx

3.初始化

git init 初始化

git init <文件名>创建并初始化

git clone <url> 克隆仓库

.gitignore声明不提交的文件

在当前目录(git仓库根目录)创建一个 “.gitignore” 文件,这个文件中存储了当git提交的时候所忽略的文件 可以基于WB创建(new -> file -> .gitignore) 可以基于linux命令 $ touch .gitignore (mac终端、git bash、或者集成了linux的dos,可以使用linux命令)

  1. # dependencies
  2. node_modules
  3. # testing
  4. /coverage
  5. # production
  6. /build
  7. # misc
  8. .DS_Store
  9. .env.local
  10. .env.development.local
  11. .env.test.local
  12. .env.production.local
  13. # npm or yarn
  14. npm-debug.log*
  15. yarn-debug.log*
  16. yarn-error.log*
  17. # webStorm
  18. .idea

4.提交

git add <file> 将工作文件修改提交到本地暂存区

git add . 将所有修改过的工作文件提交暂存区

git commit -m '备注' 将所有修改提交到历史区

git commit -am '备注'把工作区修改提交到历史区

git commit --amend修改上传提交备注

git status 查看提交状态

5.查看历史版本

git show 显示某次提交的内容

git show $id

git log 查看当前历史版本

git log --decorate查看所有分支历史版本

git log --n5显示几条

git log --author="name"name提交的历史版本

git log --before="20170202"日期前的历史版本

git log --graph图形化

6.分支

git branch 查看分支

git branch -av 查看分支

git branch <分支名>创建分支

git branch -b <分支名>创建分支并切换分支

git branch -m <分支名> <新分支名>修改分支名称

git branch -d <分支名>删除分支

git checkout <分支名>切换分支

git merge <分支>合并分支

7.远程仓库使用

git remote -v显示需要读写远程仓库使用的 Git 保存的简写与其对应的 URL

git remote add 添加远程仓库

git fetch origin从远程仓库中抓取与拉取

git push origin master推送到远程仓库

git remote show origin查看远程仓库

git remote rename pb paul远程仓库重命名

git remote rm paul远程仓库的移除

git pull origin从远程仓库拉取并合并到当前分支

8.比较

git diff工作区与暂存区区别(默认)

git diff --staged暂存区与HEAD

git diff <分支>..<分支> <文件名>比较文件区别

git diff HEAD~1 HEAD~2比较文件区别

git diff <commit> <commit> --<file>比较版本

9.获取帮助

git help <verb>

三、GIT工作原理

当我们在本地创建一个git仓库后,我们可以基于这个仓库管理我们的代码 git的工作流程 每一个git仓库都划分为三个区域 工作区:编辑代码的地方 暂存区:临时存储要生成版本代码的地方 历史区:存储的是生成的每一个版本代码 image.png 查看代码或者文件的状态 $ git status (当前处于哪个区域):

  • 红色(当前处于工作区,还没有提交到暂存区)
  • 绿色(当前处于暂存区,还没有提交到历史区)
  • 如果没有文件,代表三个区域代码已经同步,历史版本也在历史区生成了

工作区提交到暂存区 $ git add . / $ git add -A 把当前工作区中所有最新修改的文件,都提交到暂存区 暂存区到历史区 $ git commit -m’自己需要编写的备注信息’ 查看当前历史区提交的记录(查看版本信息) $ git log 工作区 VS 暂存区 $ git diff 工作区 VS 历史区(master分支) $ git diff master 暂存区 VS 历史区 $ git diff —cached