1. 查看当前项目的所有远程分支
1 | git branch -r |
2. 查看当前项目的所有本地分支
1 | git branch |
3. 查看某个分支的最近一次提交
1 | # 使用 -1 选项可以限制日志输出为只显示一次提交 |
4. 查看某个作者的最近一次提交
1 | # <author-name> 是你要查找的作者的名称 |
5. 查看当前分支某个文件的最近一次提交
1 | # HEAD表示最新的提交 |
6. 查看某一次提交的所有文件
1 | # <commit-hash> 是你要查看的提交的哈希值或引用 |
7. 查看某一次提交的某个文件的详细内容
1 | git show 4505f7817a4f56b4e8581ea8985609b7ef47748b wyt/custMap/CustMapSurveyInfoToESRepo.yml |
8. 合并分支
1 | # 注意如果有冲突发生,你需要解决冲突并手动提交修改。冲突文件会在合并日志中显示 |
9. 暂存当前分支改动
1 | # 1.暂存当前分支的改动代码 |
10. git clone –recursive 这个参数告诉 Git 在克隆主仓库的同时,递归地初始化和更新该仓库中的子模块。
1 | git clone --recursive https://git.yun-sk.com/electronic/dzsy-parent.git |
11. 回滚错误的提交
1 | # 此方法有风险 |
1 | # 安全的回滚方法 |
12. 查看当前项目的远程分支地址
1 | # origin 是远程仓库的默认名称 |
13. git 的仓库概念(很重要)
1 | 工作区(你写代码) |
14. 使用场景
- 日常迭代
1
2
3
4
5
6# 1. 将工作区的代码提交到暂存区
git add .
# 2. 把“暂存区(stage)中的内容”生成一个版本快照,提交到本地仓库
git commit -m "xxx"
# 3. 把本地仓库的代码推送到远程仓库
git push - 修复线上BUG
1
2
3
4
5
6
7
8
9
10
11
12
13# 1. 把当前开发分支改动的代码藏起来
git stash -u // 包含未被跟踪的代码
# 2. 切换到线上分支
git checkout master
# 3. 拉取线上分支最新代码
git pull
# 4. 修复bug
# 5. 暂存代码
git add .
# 6.提交代码
git commit -m "fix: xxx"
# 7. 推送代码
git push - 恢复单个文件
1
2
3
4
5
6
7
8
9# 1. 查看单个文件的历史提交记录,注意文件是相对路径,不是绝对路径
用法:git log <file_path>
示例:git log src/views/index/components/china-map-core.vue
# 2. 查看单个文件某个提交版本的修改内容
用法:git show <commit_id>:<file_path>
示例:git show 13a9a38b50d6f24771a6bab3ee0e3b0bcc513faf:src/views/index/components/china-map-core.vue
# 3. 恢复指定版本
用法:git restore --source <commit_id> <file_path>
示例:git restore --source 13a9a38b50d6f24771a6bab3ee0e3b0bcc513faf src/views/index/components/china-map-core.vue - 新项目Git跟踪
- 方式一:
1
2
3
4
5
6
7
8
9
10
11
12
13
14# 1. 本地新建项目
示例:mkdir wangchonghu.cn
# 2. 将项目纳入本地GIT仓库
示例:git init
# 3. 在远程仓库建立同名项目,gitlab 或 github
示例:https://github.com/noodleOnce/wangchonghu.cn.git
# 4. 给本地Git仓库添加一个“远程仓库地址”,并取名叫 origin
示例:git remote add origin https://github.com/noodleOnce/wangchonghu.cn.git
# 5. 在远程仓库创建master分支
示例:git push --set-upstream origin master
# 6. 提交并推送代码
git add.
git git commit -m "个人博客项目初始化"
git push - 方式二:
1
2
3
4
5
6
7
8# 1. 在远程仓库建立同名项目,gitlab 或 github
示例:https://github.com/noodleOnce/wangchonghu.cn.git
# 2. 将远程仓库克隆到到本地仓库
示例:git clone https://github.com/noodleOnce/wangchonghu.cn.git
# 3. 提交并推送代码
git add.
git git commit -m "个人博客项目初始化"
git push