GIT
简介:
Git 一个免费、开源的分布式版本控制系统。
版本控制:一种记录文件内容变化,以便将来查阅特定版本修订情况的系统。个人开发过渡到团队。
集中式版本控制:CVS、SVN、VSS 缺点:单点故障
分布式版本控制: Git、Mercurial、Bazaar、Darcs
工作机制:
工作区(写代码) git add => 暂存区(临时存储)git commit => 本地库(历史版本) push => 远程库
git很像指针
1、GIT命令
命令 | 作用 |
---|---|
git config --global user.name 用户名 | 设置用户签名 |
git config --global user.email 邮箱 | 设置用户签名 |
git init | 初始化本地库 |
git status | 查看本地库状态 |
git add 文件名 | 添加到暂存区 |
git stash | 存到缓存区 |
git commit -m “日志信息” 文件名 | 提交到本地库 |
git reflog | 查看历史记录 |
git log | 查看详细日志 |
git reset --hard 版本号 | 版本穿梭 |
git rm --cached filename | 删除暂存区文件 |
案例:在工作区vim创建hello.txt文件,再提交到暂存区
//创建并编辑
vim hello.txt
//添加到暂存区
git add hello.txt
//从暂存区删掉,工作区还存在此文件
git re --cached hello.txt
//添加到在暂存区
git add hello.txt
//提交到本地库 git commit -m "日志信息" 文件名
git commit -m “first commit” hello.txt
//第一次提交到本地库之后 工作区和暂存区都看不到这个hello.txt文件了
git status
//查看简化版本信息 包含缩写版本号和日志信息
git reflog
//查看详细版本信息 包含完整的版本号、提交者、提交时间和日志信息
git log
2、修改文件,提交不同版本到本地库
git 按照行来修改文件,修改一行它会把修改之前的那一行删掉,用修改之后的那一行填充。
//vim 修改hello.txt内容
vim hello.txt
//再次查看 返回modified:hello.txt
git status
//添加到暂存区
git add hello.txt
//第二次提交到本地库
git commit -m "second commit" hello.txt
工作区存的就是最新提交到本地库的文件
3、版本穿梭
//最新的没之前的好,回退回去 git reset --hard 版本号
git reset --hard version number
//查看版本号
git reflog
114334b (HEAD -> master) HEAD@{0}: commit: third commit
271c4fd HEAD@{1}: commit: second commit
baed1b8 HEAD@{2}: commit (initial): first commit
//回退至second commit
git reset --hard 271c4fd
//查看日志 指针发生变化
git reflog
271c4fd (HEAD -> master) HEAD@{0}: reset: moving to 271c4fd
114334b HEAD@{1}: commit: third commit
271c4fd (HEAD -> master) HEAD@{2}: commit: second commit
baed1b8 HEAD@{3}: commit (initial): first commit
//工作区的文件也回退到第2版
GIT分支 分支特性 分支创建 分支转换 分支合并 代码合并冲突解决
4、分支管理
什么是分支
在版本控制过程中,同时推进多个任务,为每个任务,我们就可以
创建每个任务的单独分支。使用分支意味着程序员可以把自己的工作从开发主线上分离开来,开发自己分支的时候,不会影响主线分支的运行。对于初学者而言,分支可以简单的理解为副本,一个分支就是一个单独的副本。分支底层也是指针的引用
分支的好处:同时并行推进多个功能开发,提高开发效率,各个分支在开发过程中,如果某一个分支开发失败,不会对其他分支造成影响,失败的分支删除重新开发即可
5、分支命令
命令 | 作用 |
---|---|
git branch 分支名 | 创建分支 |
git branch -v | 查看分支 |
git checkout 分支名 | 切换分支 |
git merge 分支名 | 把指定的分支合并到当前分支上 |
git branch -D 分支名 | 删除分支 |
git branch 分支名 散列值 | 恢复分支 |
//查看分支
git branch -v
master 271c4fd second commit
//创建分支 将主线的内容复制了一份
git branch wubo
//切换分支
git checkout wubo
witched to branch 'wubo'
//查看wubo分支下的hello.txt ,和master 的指向一样是second commit
cat hello.txt
6、分支合并
//例如:将wubo分支合并到master分支,要在master分支下运行git merge wubo命令
git merge wubo
7、合并冲突
合并分支时,两个分支在同一个文件的同一个位置有两套完全不同的修改。git无法替我们决定使用哪一个。必须人为决定。
//在master分支修改hello.txt,然后添加缓存提交本地
vim hello.txt
git add hello.txt
git commit -m "master commit" hello.txt
//切换分支
git checkout wubo
//在wubo分支修改hello.txt,然后添加缓存提交本地
vim hello.txt
git add hello.txt
git commit -m "wubo commit" hello.txt
//再回到master分支进行合并,这个时候会产生合并冲突问题
git merge wubo
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.
MINGW64 ~/Desktop/git (master|MERGING)
//手动合并
vim hello.txt
<<<HEAD
当前分支代码
=====
合并分支代码
>>>>>>合并分支名
//修改后还要记得提交缓存和添加本地(不能带文件夹名)
git add hello.txt
git commit -m "merge master"
8、团队协作机制
指定分支clone代码
git clone -b branchName https:xxxx/xxxx/xxx.git
当提交不了时可以pull一下
git pull
提交代码到远程库
git push origin branchName
8.1 团队内协作
clone
push
pull
8.2 跨团队协作
fork
clone
push
Pull request -> 审核 -> merge
gitee码云 代码复制和迁移
GitLab 局域网 代码托管
GitLab 服务器搭建
on branch master 默认分支
no commit yet 目前还没提交
Untracked files 未被追踪的文件
vim命令
ESC + yy 复制
ESC + p 粘贴
i 进入编辑状态
ESC 退出编辑状态进入命令状态
:w 保存
:q 退出
:wq 保存退出
:wq! 强制保存退出
:x 保存退出