search
HomeDevelopment ToolsgitShare an important Git tip to protect core code!

前段时间完成了一个核心代码保护的功能,目标在关键代码被修改及时同步给其他人,避免没经过 review 就上线导致问题,提示的效果图如下:

Share an important Git tip to protect core code!

在实现的过程中,用到一些平时使用不多的 Git 技巧,这篇文章来总结一下。

如何获取当前提交用户信息

这个比较简单,通过 git config user.name 即可:

04318deMacBook-Pro % git config user.name
zhangshixin

git config 保存了很多配置信息,其中常用的有自定义快捷键、用户信息、项目地址、分支信息等:

504318deMacBook-Pro % git config -l

//快捷键 begin >>> 我们可以定义自己的 git 快捷键
alias.st=status          
alias.co=checkout
alias.cb=checkout
alias.p=pull
alias.pr=pull
alias.pu=push
alias.cm=commit
alias.br=branch
alias.cm=commit
alias.undo=reset
alias.rbc=rebase
alias.save=stash
alias.pop=stash
//快捷键 end <<< 我们可以定义自己的 git 快捷键

//用户名称和邮箱 begin >>>
user.name=zhangshixin
user.email=shixin.zhang@xxx.com
//用户名称和邮箱 end <<<

//项目和分支信息 begin >>>
remote.origin.url=git@gitlab.xxx:android/xxx.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
branch.Canary.remote=origin
branch.Canary.merge=refs/heads/Canary
//项目和分支信息 end <<<

pull.rebase=true    //默认 pull 是 merge 还是 rebase

如何获取当前分支

为了减少提示频率,最好只检测核心的分支的提交(包括 merge commit)。如何获取当前分支呢?有一个简单的方式:

git symbolic-ref --short HEAD

这句命令主要包括两个关键字:symbolic-refHEAD

symbolic-ref 可以读取、修改和删除符号引用。

什么是符号引用呢?它表示一个以 refs 开头的文件(比如 refs/heads/develop),这个文件保存着本地每个分支当前所处 commit。

我们可以打开 git 项目的 .git 文件夹,在其中的 refs/heads 文件夹中会保存各个分支当前所指向的 commit:

Share an important Git tip to protect core code!

HEAD 指的是 .git/HEAD,就是一个文件,保存着当前指向的符号引用:

Share an important Git tip to protect core code!

因此 git symbolic-ref --short HEAD 的含义就是读取 .git/HEAD 文件的内容,我这里就是 refs/heads/develop 文件,因此就得出当前分支是 develop 分支。

如何获取本地未 push 的所有 commit

有时候我们会在本地提交多次后再 push,因此在拦截 push 时,需要获取到当前要 push 的所有 commit 信息,然后获取每个 commit 修改的文件。

获取要 push 信息可以通过 git log @{u}.. --oneline:

504318deMacBook-Pro ShixinDemo % git log @{u}.. --oneline
4e4655b (HEAD -> master) 拦截跳转
f947180 修改文件

git log 非常强大,它可以有这些使用场景:

  1. 获取本地和远端的 commit 差异
  2. 获取指定时间内的提交记录,可以具体到谁、什么时候、修改了哪些
  3. 获取具体某次提交修改的文件

上面我们使用的参数 @{u}.. 就是表示获取本地和远端的 commit 差异,然后 --oneline 表示不打印具体信息,只打印 short commit id 和 commit message。

如果要获取指定时间内的提交记录,可以这样:

git log --pretty="%an(%cd) %h - %s" --since="2022-09-01" --no-merges --name-status

命令执行结果:

504318deMacBook-Pro ShixinDemo % git log --pretty="%an(%cd) %h - %s" --since="2022-09-01" --no-merges --name-status
zhangshixin(Fri Dec 16 22:34:49 2022 +0800) 4e4655b - 拦截跳转

M       app/src/main/java/com/example/heicdemo/MainActivity.kt
zhangshixin(Fri Dec 16 22:34:30 2022 +0800) f947180 - 修改文件

M       .idea/gradle.xml
M       .idea/misc.xml
D       .idea/runConfigurations.xml
A       android10_dem_heic_output.heic
A       app/src/main/assets/android10_dem_heic_output.heic
R100    app/src/main/res/drawable/mushroom.jpg  app/src/main/assets/mushroom.jpg
A       app/src/main/assets/mushroom.webp
M       app/src/main/java/com/example/heicdemo/MainActivity.kt
A       app/src/main/res/drawable/mushroom.webp
M       app/src/main/res/layout/activity_main.xml

pretty 的参数用于指定打印的内容和格式;since 参数用于指定查看时间范围;no-merges 表示过滤掉 merge 时生成的额外 commit;name-status 表示展示出文件的修改状态(M 表示修改;D 表示删除;A 表示增加;R 表示重命名)。

如何获取每个 commit 修改的文件

知道 commit ID 后,可以通过 git show --pretty="" --name-only $commitId 获取这个 commit 影响的信息:

04318deMacBook-Pro ShixinDemo % git show --pretty="" --name-only 4e4655b  
app/src/main/java/com/example/shixindemo/MainActivity.kt

git show 可以用来查看 commit 的 commit message 和修改的文件、文件具体内容等信息。上面的代码中我们使用了 name-only 参数表示只要查看修改的文件即可。

总结

这篇文章介绍了通过拦截 git push 时,获取当前用户、当前分支、未 push 的 commit 和修改的文件等命令,通过组合这些命令,就可以实现一个核心代码保护功能了!

The above is the detailed content of Share an important Git tip to protect core code!. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:juejin. If there is any infringement, please contact admin@php.cn delete
Git and GitHub: What's the Relationship?Git and GitHub: What's the Relationship?Apr 14, 2025 am 12:10 AM

Git and GitHub are different tools: Git is software for version control, and GitHub is an online platform based on Git. 1.Git allows you to track file changes and collaborative development. 2. GitHub provides code hosting and collaboration tools to enhance team development efficiency.

GitHub: The Platform for Developers and ProjectsGitHub: The Platform for Developers and ProjectsApr 13, 2025 am 12:01 AM

The core features of GitHub include version control, branch management, code review, issue tracking and project management. 1. Version control and branch management are based on Git, allowing tracking of code changes and experimental development. 2. Code review is implemented through PullRequest to improve code quality and team collaboration. 3. Issues tracking and project management are carried out through Issues and the project management board to improve project transparency and traceability.

GitHub in Action: Examples and Use CasesGitHub in Action: Examples and Use CasesApr 12, 2025 am 12:16 AM

GitHub is a powerful tool to improve the efficiency and quality of software development. 1) Version control: manage code changes through Git. 2) PullRequests: Conduct code review and improve code quality. 3) Issues: Track bugs and project progress. 4) GitHubActions: Automate the construction, testing and deployment process.

Git vs. GitHub: Version Control and Code HostingGit vs. GitHub: Version Control and Code HostingApr 11, 2025 am 11:33 AM

Git is a version control system, and GitHub is a Git-based code hosting platform. Git is used to manage code versions and supports local operations; GitHub provides online collaboration tools such as Issue tracking and PullRequest.

What is Git in simple words?What is Git in simple words?Apr 09, 2025 am 12:12 AM

Git is an open source distributed version control system that helps developers track file changes, work together and manage code versions. Its core functions include: 1) record code modifications, 2) fallback to previous versions, 3) collaborative development, and 4) create and manage branches for parallel development.

Is Git the same as GitHub?Is Git the same as GitHub?Apr 08, 2025 am 12:13 AM

Git and GitHub are not the same thing. Git is a version control system, and GitHub is a Git-based code hosting platform. Git is used to manage code versions, and GitHub provides an online collaboration environment.

How to use GitHub for HTML?How to use GitHub for HTML?Apr 07, 2025 am 12:13 AM

The reason for using GitHub to manage HTML projects is that it provides a platform for version control, collaborative development and presentation of works. The specific steps include: 1. Create and initialize the Git repository, 2. Add and submit HTML files, 3. Push to GitHub, 4. Use GitHubPages to deploy web pages, 5. Use GitHubActions to automate building and deployment. In addition, GitHub also supports code review, Issue and PullRequest features to help optimize and collaborate on HTML projects.

Should I start with Git or GitHub?Should I start with Git or GitHub?Apr 06, 2025 am 12:09 AM

Starting from Git is more suitable for a deep understanding of version control principles, and starting from GitHub is more suitable for focusing on collaboration and code hosting. 1.Git is a distributed version control system that helps manage code version history. 2. GitHub is an online platform based on Git, providing code hosting and collaboration capabilities.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools