Gitadvancedoperation
Gitadvancedoperation is 指那些超出basicGitusing范围 functions, 它们可以helping你更 high 效地managementcode, 解决 complex issues and optimizationworkflow程. Master这些advancedoperation可以让你成 for 一名真正 Git high 手.
1. Git重写history
Gitproviding了 many 种重写history method, includingmodifysubmittinginformation, mergesubmitting, deletesubmittingetc.. 重写history应该谨慎using, 尤其 is in 已经push to 远程仓library circumstances under .
1.1 modify最近 submittinginformation
using git submitting --amend commands可以modify最近一次 submittinginformation.
1.2 merge many 个submitting
using git rebase -i commands可以交互式地merge many 个submitting.
执行该commands after , 会进入交互式编辑器, 你可以选择要merge submitting, 并将它们标记 for squash or fixup.
1.3 重写更早 submitting
using git rebase -i commands也可以重写更早 submitting, through选择 edit 选项.
warning
重写已经push to 远程仓library history会导致仓library不一致, 可能会影响otherteam members. such as果必须重写远程history, 需要using git push -f 强制push, 并notification所 has team members.
2. Git Stash
Git Stash用于临时保存工作区 更改, 以便你可以切换 to otherbranch or processingothertask, 之 after 再restore这些更改.
2.1 保存更改
using git stash commands可以保存当 before 工作区 更改.
2.2 查看Stashlist
using git stash list commands可以查看所 has 保存 stash.
2.3 restoreStash
using git stash apply or git stash pop commands可以restorestash.
2.4 deleteStash
using git stash drop or git stash clear commands可以deletestash.
3. Git子module (Submodule)
Git子module用于将一个Git仓libraryserving as另一个Git仓library 子Table of Contents, 便于management依赖relationships.
3.1 添加子module
using git submodule add commands可以添加子module.
3.2 clonepackage含子module 仓library
using git clone --recurse-submodules commands可以clonepackage含子module 仓library.
3.3 update子module
using git submodule update commands可以update子module.
4. Git子tree (Subtree)
Git子tree is 另一种management依赖relationships 方式, 它将一个仓library in 容merge to 另一个仓libraryin, 而不需要using子module.
4.1 添加子tree
using git subtree add commands可以添加子tree.
4.2 update子tree
using git subtree pull commands可以update子tree.
4.3 push子tree更改
using git subtree push commands可以push子tree 更改.
5. Gitignorefile
Gitignorefile用于告诉Git哪些file or Table of Contents不需要跟踪, 通常 is 临时file, logfile, 编译产物etc..
5.1 creation.gitignorefile
in 仓library根Table of Contentscreation一个 .gitignore file, 并添加需要ignore file or Table of Contents.
5.2 全局.gitignorefile
using git config --global core.excludesfile commands可以设置全局 .gitignorefile.
6. Githook (Hooks)
Githook is in Gitevent (such assubmitting, push, mergeetc.) 发生时自动执行 脚本, 用于automationworkflow程.
6.1 hookclass型
Gitproviding了 many 种hook, including:
- pre-submitting: submitting before 执行
- submitting-msg: submittinginformationverification
- post-submitting: submitting after 执行
- pre-push: push before 执行
- pre-receive: 接收push before 执行 (server端)
6.2 installationhook
Githookstore in 仓library .git/hooks Table of Contentsin, 你可以将脚本file放 in 该Table of Contents under , 并赋予执行permission.
7. Git LFS ( big filestore)
Git LFS用于management big 型file, 将 big filestore in 单独 server on , 而不 is 直接store in Git仓libraryin.
7.1 installationGit LFS
首先需要installationGit LFS客户端.
7.2 跟踪 big file
using git lfs track commands可以跟踪 big file.
7.3 push and pull big file
using常规 Gitcommands即可push and pull big file.
8. Git Bisect
Git Bisect用于through二分find fast 速定位引入bug submitting.
8.1 启动Bisect
using git bisect start commands启动Bisect.
8.2 执行Bisect
Git会自动切换 to in间 submitting, 你需要test该submitting is 否 has bug, 并using git bisect good or git bisect bad 标记.
8.3 结束Bisect
当Git找 to 引入bug submitting after , using git bisect reset commands结束Bisect.
9. Git Reflog
Git Reflog用于记录本地仓libraryin所 has 引用 更改, includingbranch切换, submitting, resetetc.operation, 便于restoreloss submitting.
9.1 查看Reflog
using git reflog commands可以查看Reflog.
9.2 restoreloss submitting
using git checkout or git branch commands可以restoreloss submitting.
10. Git Cherry-pick
Git Cherry-pick用于将specificbranch submittingapplication to 当 before branch.
10.1 挑选单个submitting
using git cherry-pick commands可以挑选单个submitting.
10.2 挑选 many 个submitting
using git cherry-pick commands可以挑选 many 个连续 submitting.
10.3 processingconflict
such as果cherry-pick过程in发生conflict, 需要手动解决conflict, 然 after using git cherry-pick --continue 继续.
实践case: usingGit Bisect定位bug
- fake设你发现当 before branch存 in 一个bug, 而之 before v1.0.0version is 正常
- 启动bisect:
git bisect start - 标记当 before submitting for bad:
git bisect bad - 标记v1.0.0 for good:
git bisect good v1.0.0 - Git会自动切换 to in间 一个submitting
- test该submitting is 否 has bug
- such as果 has bug, run:
git bisect bad - such as果没 has bug, run:
git bisect good - 重复步骤5-8, 直 to Git找 to 引入bug submitting
- 查看引入bug submitting:
git show submitting-hash - 结束bisect:
git bisect reset
互动练习
请completion以 under 练习, 巩固Gitadvancedoperation:
- creation一个test仓library, 并for几次submitting
- using
git submitting --amendmodify最近 submittinginformation - using
git rebase -imerge最近3个submitting - using
git stash保存当 before 工作区 更改 - 切换 to otherbranch, 然 after 再切换回来
- using
git stash poprestore之 before 更改 - creation一个
.gitignorefile, ignorespecificclass型 file - 尝试using
git cherry-pick将一个branch submittingapplication to 另一个branch - using
git reflog查看引用log - 尝试using
git bisect定位一个mock bug
completion练习 after , 你应该able to熟练MasterGitadvancedoperation basicusing!