别只知道 clone、push、pull!被低估的 Git 进阶命令与高效用法

注:本文为 “Git 进阶命令与高效用法” 相关合辑。
英文引文,机翻未校。
中文引文,略作重排。
如有内容异常,请看原文。


5 Git Commands That Don’t Get Enough Hype

5 个被低估的 Git 实用命令

These Git commands will drastically improve your workflow.
这些 Git 命令将显著优化你的工作流程。

Written by Sara A. Metwalli

UPDATED BY
Rose Velazquez | Aug 29, 2022

As data scientists and developers, we have to deal with version control at one point or another. We might not fully understand all Git commands or fully memorize them, but we need to know how to use them to produce high-quality code. When programmers talk about Git, more often than not we’re thinking of specific Git commands: clone, push, pull, and commit.
作为 数据科学家 与 开发人员,我们迟早都要接触 版本控制。我们未必能完全理解或记住所有 Git 命令,但必须掌握其用法以编写高质量代码。当 程序员 谈论 Git 时,通常只会想到几个特定命令:clone、push、pull 与 commit。

Yes, these Git commands are popular because they deal with the most common concerns needed when dealing with Git. But what about the rest?
这些 Git 命令之所以流行,是因为它们解决了使用 Git 时最常见的问题。但其他命令呢?

Git has different command categories — 13 to be precise — and each one deals with a specific aspect of version control. For example, init and clone are under the “create a project” category, while push and pull are in the “updating the project” category, and commit is used for basic snapshotting.
Git 包含多种命令类别,精确来说共有 13 类,每一类对应 版本控制 的某一环节。例如,init 与 clone 属于「创建项目」类别,push 与 pull 属于「更新项目」类别,commit 用于基础快照记录。

Let’s look at 5 Git commands that don’t get the hype they deserve — commands for inspecting, debugging and performing administrative tasks.
下面介绍 5 个被低估的 Git 命令,它们可用于代码检视、调试与仓库管理任务。

5 Important Git Commands

5 个重要的 Git 命令

1. git diff

Do you want to check different commits or analyze the difference between commits and a working tree? Then git diff is the command for you! (If you’re not familiar with the concept, the working tree is the directory associated with your repository on your system.) To analyze the status of a repo, we often use git diff command in addition to git status and git log.
如果你需要查看不同提交,或分析提交与工作区之间的差异,git diff 就是对应的命令。(工作区即系统中与仓库关联的本地目录。)分析仓库状态时,除 git statusgit log 外,常会使用 git diff

In general, we use git diff to get the difference between two things. These two things can be one of six scenarios. git diff can:
通常使用 git diff 比较两组内容之间的差异,可支持以下 6 种场景:

  • show changes within a local repo, which we’ll see if any changes occur within the repo’s file directory.
    显示本地仓库内的变更,可查看仓库目录中文件是否发生改动。
  • demonstrate the difference between local and remote repos. So, if you made changes on your local device and some on the git repo, git diff can help you identify exactly what changed.
    展示本地仓库与远程仓库之间的差异,可精确定位本地与远程端的不同改动。
  • identify differences between two commits of the repo.
    识别仓库中两次提交之间的差异。
  • show the difference between two specific files in two or more commits by showing the changes’ line numbers.
    显示两次或多次提交中指定文件的差异,并标注改动行号。
  • show the difference between two local or remote branches.
    显示两个本地或远程分支之间的差异。
  • demonstrate the difference between two tags of the repo. Tags are often used to refer to working versions of the repo. For example, you can use git diff to identify the differences between version 1.0.0 and version 1.1.0 of your application.
    展示仓库中两个标签之间的差异。标签通常用于标记可用版本,例如可使用 git diff 对比应用的 1.0.0 版本与 1.1.0 版本。

2. git filter-branch

We use this command to rewrite our repo’s history by applying custom filters to each revision of the repo. The custom filters can change the working tree or the commits’ information, but it can’t change the commit times or merge information.
该命令通过对仓库每一次提交应用自定义过滤器来重写仓库历史。自定义过滤器可修改工作区或提交信息,但不会修改提交时间与合并信息。

The general syntax for this command is git filter-branch branch_name. There are seven options for filters in this command so you can rewrite history for different aspects of the branch.
命令基本格式为 git filter-branch branch_name。该命令提供 7 种过滤器,可从不同维度重写分支历史。

  • subdirectory-filter: This filter only checks out a specific subdirectory of the branch.
    subdirectory-filter:仅检出分支中指定的子目录。
  • env-filter: We often use this filter to rewrite the environment information of a specific commit. For example, rewrite the author’s name, email or time of the commit.
    env-filter:用于重写指定提交的环境信息,例如修改作者姓名、邮箱或提交时间。
  • tree-filter: This filter option is very powerful; you can use it to check out all commits to the branch. This means it can change, remove, add, or even move files.
    tree-filter:功能较强,可检出分支上所有提交,支持修改、删除、添加或移动文件。
  • index-filter: Similar to the tree-filter, but it doesn’t check out the entire tree, only the indices of it. This means it’s much faster, especially for large repos.
    index-filter:与 tree-filter 类似,但不检出完整目录树,仅操作索引,执行速度更快,尤其适用于大型仓库。
  • parent-filter: This option changes a commit’s parents’ list.
    parent-filter:修改某次提交的父提交列表。
  • msg-filter: If you only want to change the commit messages, this filter is the way to go.
    msg-filter:仅用于修改提交信息。
  • tag-name-filter: If you want to edit the tags of your commits, use this command.
    tag-name-filter:用于编辑提交对应的标签。

3. git bisect

This is probably one of the most important Git commands. Bugs can kill your application and sometimes debugging a repo is not an easy task so use git bisect to find bugs in a repo.
这是 Git 中最重要的命令之一。缺陷会导致应用异常,而仓库调试往往并不简单,git bisect 可用于定位仓库中的缺陷。

The entire idea behind git bisect is to perform a binary search in the commits history to find a particular regression bug — a problem resulting from an unrelated change in the code.
git bisect 的思路是在提交历史中执行二分查找,定位特定的 回归缺陷,即由代码中无关改动引入的问题。

git bisect walks you through all recent commits, asking you if they are good or bad — depending on whether or not the regression bug is present in the commit. Doing this narrows down the options when you’re looking for the broken commit.
git bisect 会遍历近期提交,由你判断每一次提交是否存在回归缺陷,逐步缩小范围,最终定位引入问题的提交。

https://www.youtube.com/embed/HVsySz-h9r4

Git Tutorial for Beginners: Command-Line Fundamentals | Video: Corey Schafer
Git 新手教程:命令行基础 | 视频:Corey Schafer

4. git grep

Trying to find something in your repo? Want to search all your branches for a specific file? git grep is here to help you do this with ease. We use git grep to search for a pattern in a working tree.
想要在仓库中查找内容,或在所有分支中搜索指定文件?git grep 可以轻松完成。该命令用于在工作区中搜索指定模式。

You can use git grep to search for either exact words or regex in the repo. There are various options you can use with this command. Assume we are looking for the doc in the repo; we can use one of these options:
git grep 支持精确匹配与正则表达式搜索,附带多种可选参数。假设在仓库中搜索 doc,可使用以下选项:

  • Search by line number git grep -n doc.
    显示行号 git grep -n doc
  • Search only file names git grep -l doc.
    仅显示匹配文件名 git grep -l doc
  • Search using a regex pattern git grep "f[^\s]\w".
    正则表达式搜索 git grep "f[^\s]\w"
  • Specify how many matches in files git grep -c doc.
    统计文件内匹配次数 git grep -c doc

We can also use git grep to search for multiple words and/or relations. Moreover, git grep can search in a specific commit, branch, or even find all the occurrences between two commits or tags in the repo.
git grep 还可搜索多个关键词及其逻辑关系,支持在指定提交、分支中检索,甚至可查找两次提交或标签之间的所有匹配内容。

5. git blame

We use the git blame command to display the authorial information for each commit. It helps us track bugs and find which commits produced the error. On a higher level, git blame inspects specific points in repo history, obtains information on who last committed, and what they really changed.
git blame 用于显示每一行代码对应的提交作者信息,有助于追踪缺陷并定位引入错误的提交。从更宏观的角度,git blame 可检视仓库历史中的指定位置,获取最后一次修改的作者及其改动内容。

git blame displays the last author that modified a line; you can even specify exact line numbers, get the commits that affected that line, and learn who performed them.
git blame 会显示每行代码最后一次修改的作者,还可指定行号,查看影响该行的提交及其提交人。

Some people get confused between git blame and git log. Although they may sound similar, if you just need to display the commits performed, what they changed, and when they were done, it’s troublesome to use get blame to achieve that. In this case, git log is the better option. However, if you only want to display the metadata of the person who performed the commit, then git blame should be your command of choice.
很多人会混淆 git blamegit log。二者看似相近,但如果只需要查看提交内容、改动与时间,使用 git blame 会非常繁琐,此时更适合用 git log。如果只需要查看提交人的元数据,则应选择 git blame

Dealing with Git and version control is a lifelong learning experience. The more you use it, the more you learn and the more proficient you’ll become.
Git 与版本控制的使用是一个持续学习的过程,使用越多,理解越深,熟练度越高。

When getting started with Git, the standard commands always take the lead, overshadowing important commands that can make your life a lot easier. Commands such as grep, blame, and bisect can help you debug your commits, while diff and filter-branch will help you organize and inspect your repository.
初学 Git 时,常用基础命令往往占据主导地位,而一些能大幅提升效率的重要命令却被忽视。grep、blame、bisect 可辅助提交调试,diff 与 filter-branch 可用于仓库整理与检视。

Becoming fluent in these commands will help make your work faster, more efficient and (most importantly) more accurate.
熟练掌握这些命令,能让工作更快速、高效,同时也更加精准。


被低估的 Git 进阶命令与高效用法

「冷门但实用的 Git 命令」典型场景及命令用法。

一、git cherry-pick:跨分支提交移植

git cherry-pick 可将指定提交直接应用到当前分支,适用于跨分支复用少量提交。

常用用法

git cherry-pick commit_id
  • 连续多个提交
    git cherry-pick commit_start..commit_end
    
  • 保留原提交信息
    git cherry-pick -x commit_id
    

冲突解决完成后,可执行 git cherry-pick --continue 继续流程。

二、git reflog:引用日志与操作恢复

git reflog 记录本地仓库所有 HEAD 与分支移动记录,可用于恢复误删提交、误回退版本等操作。

典型场景

  • 误执行 git reset --hard 后找回丢失提交
  • 误删分支后恢复历史提交
  • 合并冲突后回退到操作前状态
git reflog
git reset --hard reflog_id

三、git stash:工作区临时暂存

git stash 可将未提交的改动临时保存,用于快速切换分支或处理紧急任务。

常用操作

  • 暂存当前工作区与暂存区
    git stash
    
  • 查看所有暂存记录
    git stash list
    
  • 恢复最近一次暂存
    git stash pop
    
  • 删除指定暂存记录
    git stash drop stash@{index}
    

四、git worktree:多分支并行工作

git worktree 允许在同一仓库下创建多个工作目录,每个目录对应不同分支,实现多分支同时开发。

常用操作

  • 添加新工作树并指定分支
    git worktree add path branch_name
    
  • 列出所有工作树
    git worktree list
    
  • 删除无用工作树
    git worktree remove path
    

五、git rebase -i:交互式提交整理

git rebase -i 以交互方式合并、修改、删除、重排历史提交,用于优化本地提交历史。

支持操作

  • 合并多个连续提交
  • 修改历史提交信息
  • 调整提交顺序
  • 丢弃无用提交

执行后按照界面提示修改指令即可完成提交历史整理。

六、git submodule:外部仓库嵌套管理

git submodule 用于在一个 Git 仓库中嵌套管理其他独立 Git 仓库,适用于模块化项目、第三方依赖管理。

常用操作

  • 添加外部子仓库
    git submodule add repository_url path
    
  • 初始化并拉取所有子模块
    git submodule update --init --recursive
    
  • 更新所有子模块到最新版本
    git submodule update --remote
    

七、git switch 与 git restore:现代安全操作

git switchgit restore 为 Git 2.23 之后引入的命令,职责更清晰,可降低误操作风险。

常用用法

  • 切换已有分支
    git switch branch_name
    
  • 创建并切换到新分支
    git switch -c new_branch
    
  • 恢复文件到最近提交状态
    git restore filename
    
  • 从暂存区取消文件
    git restore --staged filename
    

结语

上述命令在工程化开发、问题排查、历史维护中具备相应的使用价值。熟练使用这类工具可以提升开发规范性、调试效率与代码质量,形成更稳定的版本控制工作流。


reference

打开链接下载源码: https://pan.quark.cn/s/a4b39357ea24 HFSS,其全称为High Frequency Structure Simulator,是由Ansys公司研发的一款高级三维电磁场仿真软件,主要应用于射频、微波以及光学领域内的设计工作性能分析。当前压缩包内提供的是一个基于HFSS软件构建的偶极子天线模型,并且包含了该模型的仿真数据,我们将对这一模型及其关联的学术知识进行细致的探讨。偶极子天线属于天线设计中最基础的类型之一,其结构由两个大小相等且布局对称的导体单元构成,整体形状类似于汉字“工”。在2.4GHz的频率条件下,此类天线被广泛部署于Wi-Fi、蓝牙等无线通信系统的构建中。HFSS软件能够对偶极子天线的电气特性进行高精度模拟,涵盖辐射模式、增益水平、方向图形态、输入阻抗以及S参数等多个核心指标。 S参数(即Scattering Parameters),是用于评估天线或微波器件输入端输出端之间相互影响程度的关键参数。S参数详细刻画了信号流经网络设备时的反射传输状态,其中S11(输入反射系数)和S21(传输系数)是最为常用的两种表征方式。借助HFSS软件执行S参数仿真,可以获取天线在多种频率下的反射传输特性表现,从而协助设计人员对天线的阻抗匹配程度和运行效率进行有效评估。在此模型中,S参数仿真工作业已完成,因此我们可以直接审视2.4GHz频率下的阻抗匹配状况,以验证天线在该工作频段内能否展现出理想的性能。 在"Project1_1.aedt""Project1.aedt"这两个提供的文件中,储存了HFSS项目的完整信息。这些文件内含了天线的几何构造细节、材料物理属性、边界约束条件、求解器配置参数以及仿真获取的结果...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值