本系列笔记来自b站尚硅谷~ 真不错~
Git
Git是一个免费的、开源的分布式版本控制工具 VS 集中式版本工具
版本控制
为什么需要版本控制?
个人开发过渡到团队协作
版本控制工具
- 集中式版本工具
SVN …
事分两面,有好有坏。这么做显而易见的缺点是中央服务器的单点故障。如果服务器
宕机一小时,那么在这一小时内,谁都无法提交更新,也就无法协同工作。
- 分布式版本控制工具
Git…
工作机制
代码托管中心
Git安装
安装 无空格非中文
都下一步 除了下面这一步
git --version
常用命令
git config --global user.name = zhangsan
git config --global user.email = zhangsan@qq.com # 虚拟账号也可以 不需要真实的邮箱
linux命令都可以使用 git 和 linux是同一个人开发的
yy p vi中的复制粘贴
git add hello.txt #提交到暂存区
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: hello.txt
git rm --cached hello.txt #从暂存区移走了 文件真实还在
git commit -m "提交的信息" hello.txt #提交到本地库
git reflog #查看历史记录
在修改hello.txt 再提交 此时本地库有两个版本的hello.txt 当电脑上只显示一个hello.txt
查看历史记录
git log # 查看详细信息
git reflog # 查看缩短信息
## 版本穿梭
git reset --hard 版本号
git reflog # 看指针
86159@DESKTOP-2O58D28 MINGW64 /e/项目 (master)
$ git reflog
8373e29 (HEAD -> master) HEAD@{0}: commit: second commit
dd983a6 HEAD@{1}: commit (initial): first commit
86159@DESKTOP-2O58D28 MINGW64 /e/项目 (master)
$ git reset --hard dd983a6
HEAD is now at dd983a6 first commit
86159@DESKTOP-2O58D28 MINGW64 /e/项目 (master)
$ git reflog
dd983a6 (HEAD -> master) HEAD@{0}: reset: moving to dd983a6
8373e29 HEAD@{1}: commit: second commit
dd983a6 (HEAD -> master) HEAD@{2}: commit (initial): first commit
86159@DESKTOP-2O58D28 MINGW64 /e/项目 (master)
$ cat hello.txt
hello atguigu!
hello atguigu!
hello atguigu!
hello atguigu!
hello atguigu!
hello atguigu!
Git分支
从右往左看
开发自己分支的时候不会影响主分支的使用 相当于副本
分支操作
# 查看分支
$ git branch -v
* master dd983a6 first commit
# 创建分支
$ git branch hot-fix # 创建分支
$ git branch -v
hot-fix dd983a6 first commit
* master dd983a6 first commit
# 切换分支
git checkout hot-fix
# 修改hello.txt
git add hello.txt
git commit -m "hot-fix first commit" hello.txt
cat hello.txt # 新修改的文件 和master分支的hello.txt不一样
# 分支合并
#要在master分支上合并其他分支
git merge hot-fix #合并 hot-fix 分支
代码冲突
合并分支时,两个分支在同一个文件的同一个位置有两套完全不同的修改。Git无法替我们决定使用哪一个。必须人为决定新代码内容。
vim hello.txt
修改第2行
git add hello.txt
git commit -m "master test" hello.txt
git checkout hot-fix
vim hello.txt
修改第3行
git add hello.txt
git commit -m "hot-fix test" hello.txt
git checkout master
git merge hot-fix
手动合并代码
vim hello.txt
修改前
修改后
<<< ======== >>> 里面选一行 把另一行删掉 特殊符号也要删掉
:wq
git add hello.txt
# 不能带文件名
git commit -m "merge test"
cat hello.txt
git checkout hot-fix
cat hello.txt #还是自己的内容
#结论:只会修改合并的分支 , 不会修改被合并的分支
团队协作
团队内协作
跨团队协作