概述
git是分布式的版本控制工具
安装
* 官网:https://git-scm.com
* document>book>1.5getting started
mac 环境变量配置
* 修改环境变量: vim ~/.bash_profile
* 让环境变量生效: source ~/.bash_profile
配置详解(目的是 告诉git 你是谁)
查看git 所有命令
git config
级别:
* global: 一台电脑中的一个用户 (推荐)
* system: 对应一台电脑
* local: 每个项目下的具体配置 (默认)
配置
//配置用户名
git config --global user.name "你的git用户名"
//配置邮箱
git config --global user.email "你的git账户邮箱"
//配置高亮
git config --global color.ui true
//配置编辑器
git config --global core.editor "vim"
//查看配置信息 --global的配置会放到 ~/.gitconfig文件中
cat ~/.gitconfig
//system 的配置 会放到 /etc/.gitconfig
//local 会放到每一个项目当中的 .git/config文件
比如在当前目录下 git init
就会在当前目录下多一个 .git/config的配置文件
git基本工作流程
* 初始化: git init
* 查看状态: git status
Untracked files: 没有追踪到的文件
index.php
* 添加git-stating-area: git add 文件名
将文件变化提交到缓存区
stating-area:类似于缓存区
为下一次commit做准备
teacher-zhou:git yu$ git add .
teacher-zhou:git yu$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: .gitignore
new file: git基本使用.md
* 添加到版本: git commit
* 查看提交信息: git log
可以看到一个hash值,这个值是当前版本的标识,可以用他来回复版本到这个点,使用 git reset --hard 哈希值
### commit 出错了的解决方案
git commit --amend 修正错误的版本
1,已经提交到了github,或者是团队自建的git服务器,即代码已经在团队公开了。
解决方案: 修改文件 重新 git add index.php git commit -m"修改 index.php上次提交的错误",来覆盖上次错误的版本
git add .
git commit -m"fixed"
2 没有push到服务器,仅仅是在本地的版本库中时发现错了
解决方案: 使用 git reset --hard(恢复到哈希值所在的版本) 哈希值
git reset --hard 哈希值 绝对恢复到这个版本,相当于穿越回过去。 回到18岁的时候,年龄也回18岁了
然后修改文件到你想要的样子
git commit --amend 修正版本
git reset --soft 哈希值 带着现在的改变回到过去的时间节点。 带着现在的年龄回到18岁那一年
忽略不必要的文件
* .gitignore
git分支理解与应用
git branch about 创建 about分支
git branch 查看当前有几个分支
git checkout about 切换到about分支
git merge about 合并about分支到 当前分支 上
git branch -d about 删除about分支
* 通过git分支同时做几件不同的事
比如:你之前写的一个模块,提交上去了。在开发第二个模块,开发一半的时候发现上一个版本有问题,需要修复,但是新功能做了一半。这个时候其实每一个新功能都开一个分支是很好的选择,相互不会影响
* git checkout -- ./index.js 把index.js恢复到上一个版本
源文件 index.js 已经在主分支上有一个版本
开发about页面时,创建一个新的分支 git branch about,切换到about分支,git checkout about,
开发about的新代码。发现原来master分支上index.js有错误,那么切换回master分支。git checkout master,获得master分支上 原来的index.js并做修改,git checkout -- ./index.js. 在切换回 about分支继续开发about的功能
合并分支处理冲突
* git checkout -b new-design 创建分支new-design,并进入该分支
* 在master分支下创建 index.html文件,并提交到版本库
* 在new-design分支下修改index.html中的title
* 在master分之下也修改同一个位置 title
* 在master分之下合并 new-design分支,会提示冲突
teacher-zhou:git yu$ git merge new-design
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
* 修改冲突内容,确定保留的内容后,重新 add 冲突文件,重新commit
git add index.html
git commit index.html
* 在做一次合并
git merge new-design
---
Git alias 快捷方式
* 添加快捷方式
git status ==》 git s
git config --global alias.s status
* 删除 git status 快捷方式
git config --global --unset alias.s
git stash
将当前工作的文件暂存起来,然后需要的时候在拿回来。
应用场景: 当你在一个新的分支上开发新功能时,开发到一半,突然有一个紧急的bug需要修复,那么我们可以把当前分支开发的内容暂存起来 git stash.然后切换到主分支 git checkout master.去做其他事,做好之后,切回新功能开发分支,执行git stash pop拿回刚才写了一半的内容,继续工作
* git stash: 可以理解为将工作台做了一半的内容暂存
* git stash pop: 拿回原来工作区的内容
* git stash list: 查看工作台有哪些临时存储内容