Git版本控制
备注
git是一个版本管理工具
集中式与分布式
集中式:所有版本或项目集中于一个项目里(服务器里)
缺陷:安全性、不稳定性
分布式:每一个人都有一个版本管理工具Git,项目间可以相互传递,沟通。
缺陷:没有集中式控制
Git功能
- 备份 代码备份
- 记录 记录版本提交历史
- 版本回退
- 共享
- 团队协作
linus为管理linux代码花两周用C写了Git
Git安装
- Mac:brew install git -> brew.sh(先安装apple包管理工具Homebrew)
- windows:https://git.scm.com/
- Adjusting your PATH env -> 选择第三项 添加环境变量
Github
github.com-> 注册 -> 验证邮箱git config --global user.name配置用户名git config --global user.email配置邮箱git config --list查询信息
Git使用
// 初始化Git仓库git init// 查看文件目录(.git为隐藏文件)git -al(ls -al)// 目录树.gitcat index.htmlgit status
Git三大区
工作区:初始化项目时产生的区域,也就是编辑代码时文件的所在区域(此时查看status会文件会显示红色)
// 查看文件状态git status
暂存区:过渡区域,将编辑好的一部分代码先放入暂存区,项目完成后再将文件合并提交(此时查看status会文件会显示绿色)
// 单个文件添加git add + 文件名// 当前目录内容全部添加git add .
版本区:与Github仓库进行版本同步
// ""内容为提交文件的描述git commit -m "initial project"
diff
git diff:比较工作区与暂存区差异(工作区和暂存区是同等级的)git diff --cached比较暂存区与版本区差异git diff master比较工作区与版本区的差异
常用命令
git reset HEAD <file>:暂存区与版本区一致git checkout <file>:暂存区(暂存区没有找版本区)覆盖工作区的内容git rm <file> / --cached:删除暂存区文件(或从git中删除)git commit -a -m <msg>:git add.git commit -m ""连续提交git reset --hard <version>:恢复版本区指定版本的内容到工作区git reflog:查看引用版本号
分支
git branch:查看分支git branch -r:列出所有远程分支git branch [branch-name]:新建一个分支,但依然停留在当前分支git checkout [branch]:切换到新分支git checkout -b [branch]:创建并切换到新分支git branch -d [branch]:删除分支git push origin --delete [branch-name]git branch -dr [remove/branch]删除远程分支
合并分支
git merge dev:将dev分支合并至master分支git log --oneline --graph:展示合并流程图
远程仓库
克隆远程仓库
git clone "url"
公钥管理
生成公钥
ssh-keygen -t rsa
添加远程仓库
git remote add origin git@github.com:Cerelise/git-test.git
查看远程仓库
git remote -v
提交工程至远程仓库
git push -u origin master
删除远程仓库关联
git remote rm origin
echo "# git" >> README.mdgit initgit add README.mdgit commit -m "first commit"git branch -M maingit remote add origin git@github.com:Cn-yuanye/git.gitgit push -u origin main
git pull --rebase origin main
