做出改变
在工作目录中显示修改后的文件,为您的下一次提交暂存
$ git status
暂存文件,准备提交
$ git add [file]
暂存所有更改的文件,准备提交
$ git add .
将所有暂存文件提交到版本化历史记录
$ git commit -m "commit message"
将所有跟踪的文件提交到版本化历史记录
$ git commit -am "commit message"
取消暂存文件,保留文件更改
$ git reset [file]
将所有内容恢复到最后一次提交
$ git reset --hard
已更改但未暂存内容的差异
$ git diff
已 commited 但尚未提交的内容的差异
$ git diff --staged
在指定分支之前应用当前分支的任何提交
$ git rebase [branch]
配置
设置将附加到您的提交和标签的名称
$ git config --global user.name "name"
设置将附加到您的提交和标签 tags 的电子邮件地址
$ git config --global user.email "email"
启用 Git 输出的一些着色
$ git config --global color.ui auto
在文本编辑器中编辑全局配置文件
$ git config --global --edit
显示本地 repo
配置设置
$ git config --list
删除全局设置
$ git config --global --unset <entry-name>
使用分支
列出所有本地分支
$ git branch
列出所有分支,本地和远程
$ git branch -av
切换到 my_branch
,并更新工作目录
$ git checkout my_branch
创建并切换到新分支new_branch
$ git checkout -b new_branch
删除名为 my_branch
的分支
$ git branch -d my_branch
删除本地存在远程不存在的分支
$ git remote prune origin
将分支 A
合并到分支 B
$ git checkout branchB
$ git merge branchA
标记当前提交
$ git tag my_tag
从远程分支中创建并切换到本地分支
$ git checkout -b <branch-name> origin/<branch-name>
:- | :- |
---|---|
行首 # | 全行注释,不支持行尾类注释 (转义 \# ) |
行首 ! | 否定模式 (转义 \! ) |
** | 匹配任意路径 |
* | 匹配任意多个字符 |
? | 匹配任意一个字符 |
doc/** | 匹配 doc 文件夹下的全部内容 |
doc/**/a | 匹配任意深度路径下的 a 文件或文件夹 |
/ | 表示路径分隔符,不区分操作系统 |
/ 结尾 | 仅会匹配文件夹,否则会匹配文件和文件夹 |
空行 | 不匹配任何文件 |
行尾空格 | 默认被忽略,可使用\ 进行转义 |
行首空格 | 被正常处理,不会被忽略 |
当前 .gitignore
文件定义规则的优先级高于上级路径 .gitignore
定义规则的优先级;后定义的规则优先级高于前面定义规则的优先级
# 忽略当前目录logs文件夹下的全部内容
/logs/
/logs/*
/logs/**
# 上述几条规则等效
# 忽略 Mac 系统文件,包括任意子路径下的同名文件(夹)
.DS_store
# 忽略 node_modules 文件夹,包括任意子路径下的同名文件夹
node_modules/
# 忽略任意子路径下build、target文件夹,
# 但不忽略src/main、src/test下的build、target文件夹
build/
!**/src/main/**/build/
!**/src/test/**/build/
target/
!**/src/main/**/target/
!**/src/test/**/target/
# 使用 ! 重新包含指定文件(夹)
!logs/.gitkeep
同步
从该 Git 远程获取所有分支
$ git fetch [alias]
将远程分支合并到当前分支以使其保持最新状态
$ git merge [alias]/[branch]
# 没有快进
$ git merge --no-ff [alias]/[branch]
# 仅快进
$ git merge --ff-only [alias]/[branch]
将本地分支提交传输到远程存储库分支
$ git push [alias] [branch]
从跟踪远程分支获取并合并任何提交
$ git pull
将另一个分支的一个特定提交合并到当前分支
$ git cherry-pick [commit_id]
.gitattributes
# 设置默认行为,以防人们没有设置 core.autocrlf
* text=auto
# 明确声明您希望始终规范化并在结帐时
# 转换为本机行结尾的文本文件
*.c text
*.h text
# 声明在结帐时始终以 CRLF 行结尾的文件
*.sln text eol=crlf
# 表示所有真正二进制且不应修改的文件
*.png binary
*.jpg binary
计入存储库语言
# 标记或取消标记要根据存储库的语言统计数据而
# 忽略或默认隐藏差异的路径
search/index.json linguist-generated=true
# 以下属性统计 SQL 文件
*.sql linguist-detectable=true
# 从统计信息中排除
docs/formatter.rb linguist-documentation=false
# 将它们从统计信息中排除
special-vendored-path/* linguist-vendored
# 将所有 .rb 文件检测为 Java 文件
*.rb linguist-language=Java
修改远程 Commit 记录
$ git rebase -i HEAD~3
# 表示要修改当前版本的倒数第三次状态
# 将要更改的记录行首单词 pick 改为 edit
pick 96dc3f9 提交 commit 描述内容 1
pick f1cce8a 提交 commit 描述内容 2
pick 6293516 提交 commit 描述内容 3
# Rebase eeb03a4..6293516 onto eeb03a4
# (3 commands)
#
# Commands:
# p, pick = 使用提交
# r, reword = 使用提交,但编辑提交消息
# e, edit = 使用提交,但停止修改
# s, squash = 使用提交,但融合到先前的提交中
# f, fixup = 像 squash,但丢弃此提交的日志消息
# x, exec = 使用 shell 运行命令(该行的其余部分)
# d, drop = 删除提交
保存并退出,会弹出下面提示
# 您现在可以修改提交,使用
#
# git commit --amend
#
# 对更改感到满意后,运行
#
# git rebase --continue
#
# 1. 通过这条命令进入编辑更改 commit,保存退出
$ git commit --amend
# 2. 保存退出确认修改,继续执行下面命令,
$ git rebase --continue
# 如果修改多条记录反复执行上面两条命令直到完成所有修改
# 最后,确保没有人提交进行推送,最好不要加 -f 强制推送
$ git push -f origin master
git 迁移
- 从原地址克隆一份裸版本库
$ git clone --bare https://github.com/username/project.git
- 然后新建一个地址,比如一下
$ https://gitee.com/username/newproject.git
- 进入project.git这个全裸版本库,以镜像推送的方式上传代码到newproject上。
$ cd project.git
$ git push --mirror https://gitee.com/username/newproject.git
- 使用新地址,直接 Clone 到本地就可以了。
$ git clone https://gitee.com/username/newproject.git
子模块递归
# 添加所有已存在的子模块
$ git submodule foreach --recursive git submodule add <repository_url>
# 更新所有子模块到最新提交
$ git submodule foreach --recursive git pull origin master
# 检出特定的子模块路径
$ git submodule foreach --recursive git checkout <branch_name>
# 获取仓库中的所有子模块变化
$ git submodule foreach --recursive git fetch
# 获取并合并子模块的远程分支
$ git submodule foreach --recursive git pull origin <branch_name>
# 将子模块还原到父仓库中的初始提交
$ git submodule foreach --recursive git checkout .
# 获取子模块的更新并忽略本地修改
$ git submodule foreach --recursive git fetch --all
$ git submodule foreach --recursive git reset --hard origin/master
配置 http 和 socks 代理
# 查看代理
$ git config --global http.proxy
$ git config --global https.proxy
$ git config --global socks.proxy
# 设置代理
# 适用于 privoxy 将 socks 协议转为 http 协议的 http 端口
$ git config --global http.proxy http://127.0.0.1:1080
$ git config --global https.proxy http://127.0.0.1:1080
$ git config --global socks.proxy 127.0.0.1:1080
# 取消代理
$ git config --global --unset http.proxy
$ git config --global --unset https.proxy
$ git config --global --unset socks.proxy
# 只对 github.com 设置代理
$ git config --global http.https://github.com.proxy socks5://127.0.0.1:1080
$ git config --global https.https://github.com.proxy socks5://127.0.0.1:1080
# 取消 github.com 代理
$ git config --global --unset http.https://github.com.proxy
$ git config --global --unset https.https://github.com.proxy
Fork仓库同步上游仓库
-
设置上游仓库
$ git remote add upstream https://github.com/jaywcjlove/reference.git
-
本地项目操作
$ git fetch upstream # 获取上游仓库更新 $ git stach # 暂存本地修改(如果有) $ git branch -a # 列出所有远程仓库地址(非必须) $ git rebase remotes/upstream/main # 使用远程仓库的提交记录来重写本地提交记录 $ git push -f # 强制推送到远程(github)仓库 $ git stach pop # 恢复暂存的本地修改(如果有)
格式
<type>(<scope>): <short summary>
│ │ │
│ │ └─⫸ 紧凑简短的描述,无需大写,也不需要用句号结尾
│ │
│ └─⫸ Commit 范围: animations|bazel|benchpress|common|compiler|compiler-cli|core|
│ elements|forms|http|language-service|localize|platform-browser|
│ platform-browser-dynamic|platform-server|router|service-worker|
│ upgrade|zone.js|packaging|changelog|docs-infra|migrations|ngcc|ve|
│ devtools....
│
└─⫸ Commit 类型: build|ci|doc|docs|feat|fix|perf|refactor|test
website|chore|style|type|revert
类型 | 描述 |
---|---|
feat: | 新特性 |
fix(scope): | 修复 scope 中的 Bug |
feat!: / feat(scope)!: | breaking change / 重构 API |
chore(deps): | 更新依赖 |
类型 | 描述 |
---|---|
build: | 变更影响的是构建系统或者外部依赖 (如: gulp, npm) |
ci: | 修改了 CI 配置文件或脚本 (如: Github Action, Travis) |
chore: | 【重要】 变更不影响源代码或测试(如更新了辅助工具、库等) |
docs: | 只修改了文档 |
feat: | 【重要】 一个新特性 |
fix: | 【重要】 修复了一个 Bug |
perf: | 增强性能的代码变更 |
refactor: | 并非修复 Bug 或添加新特性的代码变更 |
revert: | 回退代码 |
style: | 变更不影响一些有意义的代码 (如: 删除空格、格式化代码、添加分号等) |
test: | 添加测试代码或修正已有的测试 |