Git:分布式代码版本管理工具
SVN:集中式代码版本管理工具
Git基本操作
git init # 创建一个本地的git仓库,是隐藏文件夹,没事勿动
git status # 通过git检测git仓库中的文件和目录
git add . # 管理文件, . 表示所有文件和目录
git add index.html # 管理单独文件
git commit -m "item01" # 提交,并描述
git log # 查看版本记录/日志
git reflog # 查看版本和回退记录
git reset --hard # 版本号
git分支
git branch # 查看当前分支
git branch dev # 创建一个名为dev的分支
git checkout dev # 将工作切换到dev分支上
git checkout -b dev # 创建并切换,等于上面两个指令之和
git branch master
git checkout -b bug # 创建并切换到bug分支
git merge bug # 分支合并,首先切换到master分支,然后在master分支傻姑娘执行merge指令合并bug分支的代码
git branch -d bug # 删除bug分支
使用git往码云提交代码
# 全局设置
git config --global user.name "xx"
git config --global user.email "xxxx@163.com"
# 创建git仓库
mkdir it_will_rain_this_afternoon
cd it_will_rain_this_afternoon
git init
touch README.md
# 下面4步使用频率最高
git add README.md
git commit -m "first commit"
git remote add origin https://gitee.com/li_bo1020/it_will_rain_this_afternoon.git
git push -u origin master
# 已有仓库
cd existing_git_repo
git remote add origin https://gitee.com/li_bo1020/it_will_rain_this_afternoon.git
git push -u origin master
git远程仓库操作
GitHub、码云、GitLab
上传代码
1. 给远程仓库起名
git remote add origin 远程仓库地址
2. 向远程推送代码
git push -u origin 分支
在新电脑上第一次获取代码
1. 克隆远程仓库代码
git clone 远程仓库地址(内部已实现git remote add origin 远程仓库地址)
2. 切换分支
git checkout 分支
在新电脑上进行开发
1. 切换到dev分支进行开发
git checkout dev
2. 把master分支合并到dev(仅一次)
git merge master
3. 修改代码
4. 提交代码
git add .
git commit -m 'xx'
git pull origin dev
git push origin dev
回老电脑上继续写代码
1. 切换到dev分支进行开发
git checkout dev
2. 拉代码
git pull origin dev
3. 继续开发
4. 提交代码
git add .
git commit -m 'xx'
git push origin dev
git进阶
git回滚操作
git reset --hard 版本号
git reflog # 查看包括回滚操作的版本
git checkout --文件名 # 将文件从以前修改的工作区会滚到未修改的状态
git reset HEAD 文件名 # 将修改的文件从暂存区回滚