5 个 Git 技巧助您提升工作流程

2025-06-05

5 个 Git 技巧助您提升工作流程

无论你经验多么丰富,Git 总能给你带来惊喜。它拥有许多巧妙的技巧,能够让你的日常编码工作更加顺畅。

在本文中,您将找到 5 个额外的技巧,帮助您将 Git 技能提升到一个新的水平(如果您错过了,请查看我们之前的帖子“前 5 个 Git 技巧和窍门”)。

提示 #1

在命令中可视化

你克隆了一个仓库,为了更好地理解它,你想查看提交历史记录。如果能以可视化的方式展示所有发生过的事情,岂不是很棒?

  $ git log --all --oneline --graph
Enter fullscreen mode Exit fullscreen mode

如果您需要可视化 Git 历史记录,git log --graph那么比简单的 更有帮助git log。默认情况下,git log它会显示完整的提交哈希值、作者、日期和消息,每个都在新行显示,这有时会让您难以清楚地了解发生了什么。有很多方法可以自定义它,但如果您只想简要概述您的提交历史记录,则应该尝试上面的命令。

--all # will include commits from all branches 
Enter fullscreen mode Exit fullscreen mode
--oneline # will show information on one line
Enter fullscreen mode Exit fullscreen mode
--graph # will draw a text-based graphical representation of the commit history
Enter fullscreen mode Exit fullscreen mode

git-log-zoom.png

提示 #2

查看另一个分支的文件

您正在某个分支上工作feature-one,并且想要快速查看该feature-two分支中的一个文件。如果不需要切换分支就能快速查看,那岂不是很酷?

$ git show <branch_name>:<file_name>
Enter fullscreen mode Exit fullscreen mode

使用此命令,您可以查看另一个分支的文件内容。运行该命令后,您将能够在终端中查看所选文件的内容。



💡 您还可以直接在 GitLive 中查看来自其他分支的更改,甚至是您队友未提交的代码,如果需要,还可以挑选它们以将这些更改应用到您的本地文件中。


提示#3

找出罪魁祸首

您处理的文件发生了一些更改,但您不完全确定为什么会发生这些更改,理想情况下您希望向进行更改的人寻求帮助。

$ git blame <FILE_NAME>
Enter fullscreen mode Exit fullscreen mode

您可以使用此命令逐行查看谁对特定文件进行了哪些更改。

git-blame-zoom.png

根据您想要显示的内容,您可以传递不同的标志。如果您想检查文件的特定部分,可以使用 $git blame -L <range>来将输出限制在选定的行范围内。

其他有用的标志:

$ git blame -w  # ignores white space modifications
Enter fullscreen mode Exit fullscreen mode
$ git blame -M  # ignores moving and copying text within the same file and shows the original author instead
Enter fullscreen mode Exit fullscreen mode
$ git blame -C  # ignores moving and copying text into other files and shows the original author instead
Enter fullscreen mode Exit fullscreen mode

 

提示#4

跨时间文本搜索

您的代码编辑器很可能允许您轻松地在项目中搜索文本,但通常它也可以方便地搜索文件的先前版本。

$ git grep STRING $(git rev-list --all)
Enter fullscreen mode Exit fullscreen mode

Git Grep 命令允许你快速搜索已跟踪文件中的给定字符串或正则表达式。默认情况下,它会忽略未跟踪的文件,但你可以--untracked在命令中添加标志来搜索未跟踪的文件。

让我们看一些有用的标志:

$ git grep STRING # searches through the files in your working directory (current state)
Enter fullscreen mode Exit fullscreen mode
$ git grep STRING $(git rev-list --all) # searches through all the commits (entire history of the project)
Enter fullscreen mode Exit fullscreen mode
$ git grep STRING HEAD~1 # search only in the commit prior to the current head
Enter fullscreen mode Exit fullscreen mode
$ git grep STRING <commit1> <commit2> <commit3> # search only in the specific commits (list as many commits as you need)
Enter fullscreen mode Exit fullscreen mode
$ git grep -ni STRING  # Case insensitive search. Will list the matches along with line numbers
Enter fullscreen mode Exit fullscreen mode
$ git grep -c STRING  # Shows the number of lines that match, instead of listing all the results
Enter fullscreen mode Exit fullscreen mode
$ git grep STRING *.js # search only the files with the specific extension (ignore matches from other files)
Enter fullscreen mode Exit fullscreen mode

 

提示 #5

团队合作是关键:赞扬提交的共同作者

你一直在做的这个功能非常难,而且你知道如果没有安娜的帮助,你不可能完成它。她的贡献非常宝贵,我们应该给予她应有的赞扬。

Co-authored-by NAME <email>

$ git commit -m "Prepare release.
>
>
Co-authored-by: name1 <name1@example.com>
Co-authored-by: name2 <name2@example.com>"
Enter fullscreen mode Exit fullscreen mode

这种支持多位作者的简便方法由 GitHub 引入,旨在改善团队内部协作并促进社交编码。GitHub 和 GitLab 均支持此语法,这是一种同时表彰提交代码的两位开发者的好方法。

要将一个或多个共同作者添加到提交中,您需要知道他们的姓名和电子邮件地址(如果他们决定将电子邮件地址保密,则应向他们索要 GitHub 提供的免回复电子邮件地址,该地址可在 GitHub 的电子邮件设置页面找到)。您需要将他们添加到消息中(每人占一行),并置于结束引号之前。

如果您遵循此语法,您将在 GitHub 上看到共同撰写的提交(如果您在拉取请求中选择了“压缩并合并”选项,也会看到此提交)。它还可以包含在个人资料贡献图表和存储库的统计信息中。

团队承诺2.png

感谢阅读!希望你喜欢我们精选的 5 个 Git 技巧,助你提升工作流程。如果你喜欢,不妨看看我们的文章“五大 Git 技巧与诀窍”。欢迎在评论区分享你最喜欢的技巧。祝你 Git 愉快!

文章来源:https://dev.to/gitlive/5-git-tips-to-level-up-your-workflow-24lo
PREV
5 种方法来撤销 Git 中的错误
NEXT
💰付费 UGC 终极指南 💸 💸 获取更多内容