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.

# modify最近 submittinginformation git submitting --amend # 直接modifysubmittinginformation, 不进入编辑器 git submitting --amend -m "New submitting message"

1.2 merge many 个submitting

using git rebase -i commands可以交互式地merge many 个submitting.

# merge最近3个submitting git rebase -i HEAD~3

执行该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 工作区 更改.

# 保存当 before 工作区 更改 git stash # 保存当 before 工作区 更改, 并添加comment git stash save "Work in progress" # 保存当 before 工作区 and stage区 所 has 更改 git stash -a # 保存当 before 工作区 and stage区 specificfile git stash push -m "Work in progress" file1.txt file2.txt

2.2 查看Stashlist

using git stash list commands可以查看所 has 保存 stash.

# 查看stashlist git stash list

2.3 restoreStash

using git stash apply or git stash pop commands可以restorestash.

# restore最近 stash, 但不delete它 git stash apply # restorespecific stash, 但不delete它 git stash apply stash@{2} # restore最近 stash, 并delete它 git stash pop # restorespecific stash, 并delete它 git stash pop stash@{2}

2.4 deleteStash

using git stash drop or git stash clear commands可以deletestash.

# delete最近 stash git stash drop # deletespecific stash git stash drop stash@{2} # delete所 has stash git stash clear

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.

# 添加子module git submodule add https://github.com/username/submodule.git

3.2 clonepackage含子module 仓library

using git clone --recurse-submodules commands可以clonepackage含子module 仓library.

# clonepackage含子module 仓library git clone --recurse-submodules https://github.com/username/repository.git # such as果已经clone了仓library, 可以初始化子module git submodule init git submodule update

3.3 update子module

using git submodule update commands可以update子module.

# update所 has 子module to 最 new version git submodule update --remote # updatespecific子module git submodule update --remote submodule-name

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.

# 添加子tree git subtree add --prefix=subtree-folder https://github.com/username/subtree.git main --squash

4.2 update子tree

using git subtree pull commands可以update子tree.

# update子tree git subtree pull --prefix=subtree-folder https://github.com/username/subtree.git main --squash

4.3 push子tree更改

using git subtree push commands可以push子tree 更改.

# push子tree更改 git subtree push --prefix=subtree-folder https://github.com/username/subtree.git main

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.

# ignore所 has .logfile *.log # ignorenode_modulesTable of Contents node_modules/ # ignore.DS_Storefile (MacOS) .DS_Store # ignorespecificfile .env # ignorebuildTable of Contents, 但保留build/.gitkeep build/* !build/.gitkeep

5.2 全局.gitignorefile

using git config --global core.excludesfile commands可以设置全局 .gitignorefile.

# 设置全局.gitignorefile git config --global core.excludesfile ~/.gitignore_global

6. Githook (Hooks)

Githook is in Gitevent (such assubmitting, push, mergeetc.) 发生时自动执行 脚本, 用于automationworkflow程.

6.1 hookclass型

Gitproviding了 many 种hook, including:

6.2 installationhook

Githookstore in 仓library .git/hooks Table of Contentsin, 你可以将脚本file放 in 该Table of Contents under , 并赋予执行permission.

# copyexamplehook cp .git/hooks/pre-submitting.sample .git/hooks/pre-submitting # 赋予执行permission chmod +x .git/hooks/pre-submitting

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客户端.

# in macOS on installation brew install git-lfs # in Linux on installation sudo apt-get install git-lfs # 初始化Git LFS git lfs install

7.2 跟踪 big file

using git lfs track commands可以跟踪 big file.

# 跟踪所 has .jpgfile git lfs track "*.jpg" # 跟踪specificTable of Contents under file git lfs track "assets/*" # 查看跟踪 fileclass型 git lfs track

7.3 push and pull big file

using常规 Gitcommands即可push and pull big file.

# 添加并submitting big file git add large-file.jpg git submitting -m "Add large file" # push big file git push

8. Git Bisect

Git Bisect用于through二分find fast 速定位引入bug submitting.

8.1 启动Bisect

using git bisect start commands启动Bisect.

# 启动Bisect git bisect start # 标记当 before submitting for has bug git bisect bad # 标记某个submitting for 无bug git bisect good v1.0.0

8.2 执行Bisect

Git会自动切换 to in间 submitting, 你需要test该submitting is 否 has bug, 并using git bisect good or git bisect bad 标记.

# test当 before submitting, such as果无bug git bisect good # test当 before submitting, such as果 has bug git bisect bad

8.3 结束Bisect

当Git找 to 引入bug submitting after , using git bisect reset commands结束Bisect.

# 结束Bisect, 回 to 原来 branch git bisect reset

9. Git Reflog

Git Reflog用于记录本地仓libraryin所 has 引用 更改, includingbranch切换, submitting, resetetc.operation, 便于restoreloss submitting.

9.1 查看Reflog

using git reflog commands可以查看Reflog.

# 查看Reflog git reflog # 查看specificbranch Reflog git reflog show branch-name

9.2 restoreloss submitting

using git checkout or git branch commands可以restoreloss submitting.

# 查看Reflog, 找 to loss submitting git reflog # 基于loss submittingcreation new branch git branch recover-branch submitting-hash # 直接checkoutloss submitting git checkout submitting-hash

10. Git Cherry-pick

Git Cherry-pick用于将specificbranch submittingapplication to 当 before branch.

10.1 挑选单个submitting

using git cherry-pick commands可以挑选单个submitting.

# 挑选单个submitting git cherry-pick submitting-hash

10.2 挑选 many 个submitting

using git cherry-pick commands可以挑选 many 个连续 submitting.

# 挑选 many 个连续submitting ( left 开 right 闭区间) git cherry-pick start-hash..end-hash # 挑选 many 个连续submitting (闭区间) git cherry-pick start-hash^..end-hash

10.3 processingconflict

such as果cherry-pick过程in发生conflict, 需要手动解决conflict, 然 after using git cherry-pick --continue 继续.

# 解决conflict after 继续 git add . git cherry-pick --continue # 取消cherry-pick git cherry-pick --abort

实践case: usingGit Bisect定位bug

  1. fake设你发现当 before branch存 in 一个bug, 而之 before v1.0.0version is 正常
  2. 启动bisect: git bisect start
  3. 标记当 before submitting for bad: git bisect bad
  4. 标记v1.0.0 for good: git bisect good v1.0.0
  5. Git会自动切换 to in间 一个submitting
  6. test该submitting is 否 has bug
  7. such as果 has bug, run: git bisect bad
  8. such as果没 has bug, run: git bisect good
  9. 重复步骤5-8, 直 to Git找 to 引入bug submitting
  10. 查看引入bug submitting: git show submitting-hash
  11. 结束bisect: git bisect reset

互动练习

请completion以 under 练习, 巩固Gitadvancedoperation:

  1. creation一个test仓library, 并for几次submitting
  2. using git submitting --amend modify最近 submittinginformation
  3. using git rebase -i merge最近3个submitting
  4. using git stash 保存当 before 工作区 更改
  5. 切换 to otherbranch, 然 after 再切换回来
  6. using git stash pop restore之 before 更改
  7. creation一个 .gitignore file, ignorespecificclass型 file
  8. 尝试using git cherry-pick 将一个branch submittingapplication to 另一个branch
  9. using git reflog 查看引用log
  10. 尝试using git bisect 定位一个mock bug

completion练习 after , 你应该able to熟练MasterGitadvancedoperation basicusing!