File contents Git 高级技巧及应用
一、基础命令隐藏技巧
git clone --mirror (镜像): 主要用于仓库迁移(常见不同托管平台):实现仓库一比一完整迁移: 保留所有分支,提交历史、标签等
git clone --mirror <原仓库地址>
cd repo.git
git remote set-url origin <新仓库地址>
git push --mirror
git config优先级
仓库配置 > 全局配置 > 系统配置
git config --global -e 编辑全局配置文件gitconfig文件: C:/Users/86178/.gitconfig
git config --system -e 编辑系统配置文件gitconfig文件: C:/Program Files/Git/etc/gitconfig
git config --local -e 编辑本地仓库配置文件: /projects/.git/config
git config除了配置用户名、邮箱,还可以配置alias、editor、凭证等等
git alias 命令行简化
simple
git config --global alias.co checkout 命令行配置; 逐个加
git config --global -e 编辑gitconfig配置
advance: 编辑.bash_profile进一步简化
alias gco='git checkout'
alias gsync='git pull; git push'
PS: 此时在git bash 中可以gco代替git checkout.
// goods alias
[alias]
co = checkout
cob = checkout -b
coo = !git fetch && git checkout
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit -n 20
br = branch
brd = branch -d
st = status
aa = add -A .
unstage = reset --soft HEAD^
cm = commit -m
amend = commit --amend -m
fix = commit --fixup
undo = reset HEAD~1
rv = revert
cp = cherry-pick
pu = !git push origin `git branch --show-current`
fush = push -f
mg = merge --no-ff
rb = rebase
rbc = rebase --continue
rba = rebase --abort
rbs = rebase --skip
rom = !git fetch && git rebase -i origin/master --autosquash
save = stash push
pop = stash pop
apply = stash apply
rl = reflog
PS: .bash_profile 默认在 C:/Users/86178/.bash_profile 路径下,没有就手动创建即可。
git commit
git commit -am [message],跳过暂存区提交 等价于 git add -u + git commit -m
git commit --amend (结合Git add重写上次Commit History)
git commit --no-verify / -n [跳过校验,继续提交;结合 Git Hooks]
git checkout
git checkout(theirs/ours选项): 并非自动完成提交,需要git add配合,常用于合并代码时选择别人的还是自己的。
git checkout --ours .
git checkout --theires .
git checkout [hash]用于临时测试,查看历史版本,此时Head为游离状态。
git merge
git merge --no-ff 禁用fast-forward合并;强制创建一个合并提交;建议使用,保证各自提交历史完整,结构清晰
git merge --squash 将合并结果压缩成一个commit提交到目标分支,代替合并提交,不会保留被合并的分支的历史记录;同时需要手动commit提交
git merge -m [message] 修改默认合并提交信息
git merge --abort 撤销合并 : 主要用于刚合并代码,未提交(commit),可以撤回合并。
二、git进阶命令
git merge vs git rebase
git merge
fastForward / Three way Merge
merge策略:
Ort、Resolve、Recursive、Octopus、Ours、Subtree
git reabse (变基)
git rebase [baseBranch] [branchName]
git reabse -i [Hash]: 交互式变基:整理提交历史
作用: 提供一个交互式Vim窗口:rebase-todo待办列表(反序排列): 可以对过往Commit历史进行排序、压缩、合并、丢弃、编辑等。
PS: 其中比如edit可以结合 git add 和 git commit 和 git reabse --continue,将其中一个提交拆分成多个小提交;
git rebase -i HEAD~n: 整理最近N次提交历史
simple situation
rebase过程中没有冲突。直接esc + :wq保存退出
complex situation
rebase 过程中,发生冲突,需要优先解决冲突; git add. + git rebase --continue,循环多次,直至解决所有冲突,才会终止 reabse 进程.
注意: 如果 rebase 在 vim 窗口进程报错,记得 rm -rf /.git/rebase-merge
git reabse --onto选项
git rebase A^ B --onto [newBase] (默认情况,左开右闭,如果想包含 A,使用^)
git reabse --onto [newBase] [branch1] [branch2]
检出 branch2,将 branch2 与 branch1 分叉后的 commits,移到 newBase 分支的顶端,在不依赖 branch1 分支的情况下
git rebase --onto [commitHashA] [commitHashB]: 用于移除当前分支的一些commits
如分支 A---B---C---D---E, 执行 git rebase --onto B D,此时 c,d 提交会被删除(左开右闭)
git reset vs git revert
git reset
simple: 取消变更
git reset --hard: 取消所有变更
git reset [path] 如取消文件路径下所有文件的变更: git reset './src'
git reset HEAD [fileName] 取消单个文件的暂存
advance: 重置回滚历史
git reset [--mixed/--soft/--hard/--keep/--merge] [hash]
mixed: reset Index, keep work
soft: keep work And Index,but reset Head
hard: reset Index And Work
说明: 如果用hard模式去回滚,会丢掉当前commit后的所有commit历史,配合git reflog保命操作。较危险.如果推送远程,需用--force/-f 强制推送。
git revert
simple: 撤销非merge commit
git revert [Hash]: 撤销某次Commit,相当于一个反向的镜像,保留完整的提交历史,较安全。
advance: 撤销merge commit
git revert -m [parent-number] [Hash]: 选择主分支编号,进行revert
git subModule vs git subTree
git subModule: 用于管理子模块
git subTree: 用于管理子树
git cherry-pick: 将指定范围的commit应用到当前分支。
git cherry-pick [hash]: 转移单个commit
git cherry-pick [commitHashA commitHashB ...] 转移多个commit
注意: git cherry-pick 的 commit 会合到当前分支,生成对应的新的 commit
git cherry-pick A..B 转移A到B之内的所有提交,不包括A,且A必须早于B提交,否则失效,且不会报错
git cherry-pick A^..B 转移A到B之内的所有提交,包含A
git cherry-pick -m parent-number 选择主分支编号,进行cherry-pick
git stash(储藏): 用于临时保存工作进度
simple
git stash / git stash save / git stash push -m [message]
git stash list
git stash pop [stash-name]
git stash apply [stash-name]
git stash clear
git stash show [stash-name] / git stash show [stash-name] -p (查看更具体的stash内容)
advance - git stash branch [branchName] [stash-name]: 从储藏中签出一个分支 - git stash -u / --include-untracked: 储藏没有被Git追踪的文件 - git stash -a / --all: 储藏所有的文件,包含ignore的文件
PS: 注意drop与apply的区别。
三、Git提交规范 (Conventional Commits): 约定式提交:旨在规范你的Commit message信息。
约定式提交
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]
type只能是以下任意一种:
feat:新增功能(feature)
fix:修复缺陷(bug fix)
docs:文档变更
style:代码格式调整(不影响代码运行)
refactor:代码重构(既不修复bug也不新增功能)
perf:性能优化
test:测试相关变更
build:构建系统或外部依赖变更
ci:持续集成配置变更
chore:其他不影响源码的变更
revert:回滚某个提交
scope 作用范围
description 描述信息
PS: type/description必填,其余可选,起源于Anglar规范
Husky与Git Hooks: 约定代码规范和提交规范
$ npx husky-init && npm install
$ npm install --save-dev @commitlint/cli @commitlint/config-conventional
$ npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
$ git add .husky
$ git commit -m "feat: add commitlint"
$ git push
Conventional Commits: https://www.conventionalcommits.org/en/v1.0.0/
四、Git Flow 工作流
五、Git GUI工具与插件(VSCode)
GUI 工具
SourceTree: 界面干净清爽
GitKraken: 收费
Tortoise Git: 功能全,界面丑陋
gitK /git-gui (Git自带GUI界面)
$ gitk or git-gui
VSCode插件
GitLens: 鼠标悬浮可查看代码提交作者、提交时间
Git Graph: 图形化GUI工具
Git File History:方便查看文件过往历史时刻代码
六、推荐阅读
1 --- 2 name: codermakers-interview-questions-git 3 description: Git 高级技巧及应用 4 --- 5 ## Git 高级技巧及应用 6 7 ### 一、基础命令隐藏技巧 8 9 - git clone --mirror (镜像): 主要用于仓库迁移(常见不同托管平台):实现仓库一比一完整迁移: 保留所有分支,提交历史、标签等 10 11 ```shell 实践 12 git clone --mirror <原仓库地址> 13 cd repo.git 14 15 git remote set-url origin <新仓库地址> 16 git push --mirror 17 ``` 18 19 - git config优先级 20 - 仓库配置 > 全局配置 > 系统配置 21 - git config --global -e `编辑全局配置文件gitconfig文件: C:/Users/86178/.gitconfig` 22 - git config --system -e `编辑系统配置文件gitconfig文件: C:/Program Files/Git/etc/gitconfig` 23 - git config --local -e `编辑本地仓库配置文件: /projects/.git/config` 24 > git config除了配置用户名、邮箱,还可以配置alias、editor、凭证等等 25 - git alias 命令行简化 26 - simple 27 - git config --global alias.co checkout `命令行配置; 逐个加` 28 - git config --global -e `编辑gitconfig配置` 29 - advance: 编辑.bash_profile进一步简化 30 31 ```shell 32 alias gco='git checkout' 33 alias gsync='git pull; git push' 34 ``` 35 36 > PS: 此时在git bash 中可以gco代替git checkout. 37 38 ```js 39 // goods alias 40 [alias] 41 co = checkout 42 cob = checkout -b 43 coo = !git fetch && git checkout 44 lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit -n 20 45 br = branch 46 brd = branch -d 47 st = status 48 aa = add -A . 49 unstage = reset --soft HEAD^ 50 cm = commit -m 51 amend = commit --amend -m 52 fix = commit --fixup 53 undo = reset HEAD~1 54 rv = revert 55 cp = cherry-pick 56 pu = !git push origin `git branch --show-current` 57 fush = push -f 58 mg = merge --no-ff 59 rb = rebase 60 rbc = rebase --continue 61 rba = rebase --abort 62 rbs = rebase --skip 63 rom = !git fetch && git rebase -i origin/master --autosquash 64 save = stash push 65 pop = stash pop 66 apply = stash apply 67 rl = reflog 68 ``` 69 70 > PS: .bash_profile 默认在 C:/Users/86178/.bash_profile 路径下,没有就手动创建即可。 71 72 - git commit 73 - git commit -am [message],跳过暂存区提交 等价于 git add -u + git commit -m 74 - git commit --amend (结合Git add重写上次Commit History) 75 - git commit --no-verify / -n [跳过校验,继续提交;结合 Git Hooks] 76 77 - git checkout 78 - git checkout(theirs/ours选项): 并非自动完成提交,需要git add配合,常用于合并代码时选择别人的还是自己的。 79 - git checkout --ours . 80 - git checkout --theires . 81 - git checkout [hash]用于临时测试,查看历史版本,此时Head为游离状态。 82 83 - git merge 84 - git merge --no-ff `禁用fast-forward合并;强制创建一个合并提交;建议使用,保证各自提交历史完整,结构清晰` 85 - git merge --squash `将合并结果压缩成一个commit提交到目标分支,代替合并提交,不会保留被合并的分支的历史记录;同时需要手动commit提交` 86 - git merge -m [message] `修改默认合并提交信息` 87 - git merge --abort `撤销合并` : 主要用于刚合并代码,未提交(commit),可以撤回合并。 88 89 ### 二、git进阶命令 90 91 - git merge vs git rebase 92 - git merge 93 - fastForward / Three way Merge 94 - merge策略: 95 - Ort、Resolve、Recursive、Octopus、Ours、Subtree 96 - git reabse (变基) 97 - git rebase [baseBranch] [branchName] 98 - git reabse -i [Hash]: `交互式变基:整理提交历史` 99 - 作用: 提供一个交互式Vim窗口:rebase-todo待办列表(反序排列): 可以对过往Commit历史进行排序、压缩、合并、丢弃、编辑等。 100 > PS: 其中比如edit可以结合 git add 和 git commit 和 git reabse --continue,将其中一个提交拆分成多个小提交; 101 - git rebase -i HEAD~n: `整理最近N次提交历史` 102 - simple situation 103 - rebase过程中没有冲突。直接esc + :wq保存退出 104 - complex situation 105 - rebase 过程中,发生冲突,需要优先解决冲突; git add. + git rebase --continue,循环多次,直至解决所有冲突,才会终止 reabse 进程. 106 > 注意: 如果 rebase 在 vim 窗口进程报错,记得 rm -rf /.git/rebase-merge 107 - git reabse --onto选项 108 - git rebase A^ B --onto [newBase] (默认情况,左开右闭,如果想包含 A,使用^) 109 - git reabse --onto [newBase] [branch1] [branch2] 110 > 检出 branch2,将 branch2 与 branch1 分叉后的 commits,移到 newBase 分支的顶端,在不依赖 branch1 分支的情况下 111 - git rebase --onto [commitHashA] [commitHashB]: `用于移除当前分支的一些commits` 112 > 如分支 A---B---C---D---E, 执行 git rebase --onto B D,此时 c,d 提交会被删除(左开右闭) 113 114 - git reset vs git revert 115 - git reset 116 - simple: 取消变更 117 - git reset --hard: `取消所有变更` 118 - git reset [path] `如取消文件路径下所有文件的变更: git reset './src'` 119 - git reset HEAD [fileName] `取消单个文件的暂存` 120 - advance: 重置回滚历史 121 - git reset [--mixed/--soft/--hard/--keep/--merge] [hash] 122 - mixed: reset Index, keep work 123 - soft: keep work And Index,but reset Head 124 - hard: reset Index And Work 125 - 说明: 如果用hard模式去回滚,会丢掉当前commit后的所有commit历史,配合git reflog保命操作。较危险.如果推送远程,需用--force/-f 强制推送。 126 - git revert 127 - simple: 撤销非merge commit 128 - git revert [Hash]: 撤销某次Commit,相当于一个反向的镜像,保留完整的提交历史,较安全。 129 - advance: 撤销merge commit 130 - git revert -m [parent-number] [Hash]: `选择主分支编号,进行revert` 131 - git subModule vs git subTree 132 - git subModule: 用于管理子模块 133 - git subTree: 用于管理子树 134 - git cherry-pick: 将指定范围的commit应用到当前分支。 135 - git cherry-pick [hash]: `转移单个commit` 136 - git cherry-pick [commitHashA commitHashB ...] `转移多个commit` 137 > 注意: git cherry-pick 的 commit 会合到当前分支,生成对应的新的 commit 138 - git cherry-pick A..B `转移A到B之内的所有提交,不包括A,且A必须早于B提交,否则失效,且不会报错` 139 - git cherry-pick A^..B `转移A到B之内的所有提交,包含A` 140 - git cherry-pick -m parent-number `选择主分支编号,进行cherry-pick` 141 142 - git stash(储藏): 用于临时保存工作进度 143 - simple 144 - git stash / git stash save / git stash push -m [message] 145 - git stash list 146 - git stash pop [stash-name] 147 - git stash apply [stash-name] 148 - git stash clear 149 - git stash show [stash-name] / git stash show [stash-name] -p (查看更具体的stash内容) 150 - advance - git stash branch [branchName] [stash-name]: 从储藏中签出一个分支 - git stash -u / --include-untracked: 储藏没有被Git追踪的文件 - git stash -a / --all: 储藏所有的文件,包含ignore的文件 151 > PS: 注意drop与apply的区别。 152 153 ### 三、Git提交规范 (Conventional Commits): 约定式提交:旨在规范你的Commit message信息。 154 155 - 约定式提交 156 - Message 格式 157 158 ```md 159 <type>[optional scope]: <description> 160 161 [optional body] 162 163 [optional footer(s)] 164 ``` 165 166 ```HTTP 167 type只能是以下任意一种: 168 feat:新增功能(feature) 169 fix:修复缺陷(bug fix) 170 docs:文档变更 171 style:代码格式调整(不影响代码运行) 172 refactor:代码重构(既不修复bug也不新增功能) 173 perf:性能优化 174 test:测试相关变更 175 build:构建系统或外部依赖变更 176 ci:持续集成配置变更 177 chore:其他不影响源码的变更 178 revert:回滚某个提交 179 scope 作用范围 180 description 描述信息 181 ``` 182 183 > PS: type/description必填,其余可选,起源于Anglar规范 184 185 - Husky与Git Hooks: 约定代码规范和提交规范 186 187 ```shell 188 189 $ npx husky-init && npm install 190 191 $ npm install --save-dev @commitlint/cli @commitlint/config-conventional 192 193 $ npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"' 194 195 $ git add .husky 196 $ git commit -m "feat: add commitlint" 197 $ git push 198 ``` 199 200 > Conventional Commits: https://www.conventionalcommits.org/en/v1.0.0/ 201 202 ### 四、Git Flow 工作流 203 204  205 206 ### 五、Git GUI工具与插件(VSCode) 207 208 - GUI 工具 209 - SourceTree: 界面干净清爽 210 - GitKraken: 收费 211 - Tortoise Git: 功能全,界面丑陋 212 - gitK /git-gui (Git自带GUI界面) 213 ```shell 214 $ gitk or git-gui 215 ``` 216 - VSCode插件 217 - GitLens: 鼠标悬浮可查看代码提交作者、提交时间 218 - Git Graph: 图形化GUI工具 219 - Git File History:方便查看文件过往历史时刻代码 220 221 ### 六、推荐阅读 222 223 - **Git flight rules**: https://github.com/k88hudson/git-flight-rules/blob/master/README_zh-CN.md 224 - **Git-learning-playground**: https://learngitbranching.js.org/?locale=zh_CN 225 - **Git-doc** https://www.bookstack.cn/read/git-doc-zh/README.md 226 - **Git-tutorials** https://www.atlassian.com/git/tutorials/advanced-overview
aibot88/sec_skill_store/tree/main/skills/github/codermakers-interview-questions-git commit 6ac3f4e26a
Frequently asked questions How do I install the Codermakers Interview Questions Git skill? Run npx skillmds@latest add aibot88/codermakers-interview-questions-git in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
What does the Codermakers Interview Questions Git skill do? Git 高级技巧及应用 It is listed under Coding & Dev Tools on SkillMD.
Is Codermakers Interview Questions Git safe to use? This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: CAUTION, Skill Scanner: PASS. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
Which AI agents work with Codermakers Interview Questions Git? This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Is Codermakers Interview Questions Git free to use? Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Who published Codermakers Interview Questions Git? aibot88 (@aibot88) published this skill. Their other Agent Skills are listed on their SkillMD profile.