Git Atomic Commit(git原子提交)

Atomic: of or forming a single irreducible unit or component in a larger system.

警告
本文最后更新于 2023-03-10,文中内容可能已过时。

git原子提交可以提高我们的工作效率

什么是原子git提交?

使用原子git提交意味着您的提交尽可能的小。每个提交只做一件事,而且是一件简单的事,可以用一句话来概括。

代码的变化量并不重要。它可以是一个字母,也可以是十万行,但你应该能够用一个简单的短句来描述这个变化。

理想情况下,你也希望你的测试套件在你提交时是绿色的。你的修改可能是 “原子 “的,即尽可能小,但它们也应该是 “完整 “的,这意味着你的测试套件总是贯彻执行。

尽可能的小,但是完整:这就是一个原子git提交。

为什么要写原子git提交?

练习原子git提交有几个很大的好处,我们会简单地介绍一下。但最后一个才是最重要的。它可能会完全改变你的工作方式,将你的生产力提高一个数量级,并使你的工作更加愉快。

理由一:原子性的改变是可逆的改变

我们都知道这个关于软件的简单事实:需求总是在变化。通过编写原子git提交,我们可以通过简单的提交还原来恢复任何变化。这已经极大地提高了你的工作效率。

理由之二:干净的git历史记录

当糟糕的事情发生时,一个干净的git历史意味着痛苦和拯救的区别。这就像给你的房子买保险一样:看起来没什么用,直到发生了火灾。

理由之三:拉取请求更容易审查

你的团队绝对会因此而喜欢你。

理由四:更好的工作流程

这是迄今为止练习原子git提交的最重要原因:它完全改变了你解决问题的方式。

如果你像我一样,在开发一个功能时,你可能有一个倾向,就是……做它。完全地做。

然后你会意识到你没有把一切都想清楚。

你需要改变的东西比你预期的要多。一些边缘情况没有被考虑到。你破坏了一些不相关的测试,它们需要修复。很快,你就在你自己制造的迷宫中结束了。你迷路了。你的头很痛。如果不全神贯注,你就无法取得任何进展。

现在,这是做事情的错误方法。更糟糕的是,你已经知道正确的方法,因为它是如此明显。

完成一项大的、复杂的任务的众所周知的方法:把它切成较小的、可管理的、微小的步骤。每个步骤都有自己的简单问题要解决。这是很明显的建议,你可能已经听过很多次了……。但你在日常工作中是否真的在实践它?

那么,这里有一个很好的实践方法:写原子化的git提交。

通过强行写原子提交,你就能以正确的方式处理工作,将其简化为更小的步骤。毕竟,化繁为简是我们工作的核心。那么,为什么我们不总是有意识地这样做呢?

当然,这个建议可能听起来很明显。但如果我过去的经验证明了什么,那就是显而易见的事情真的值得重复,更重要的是,它值得实践。

让你的工作更简单、更好、更容易管理,最重要的是:让它更容易。采取小步骤。写小的提交。原子提交。你会喜欢它们的。

以上为DeepL翻译。


原文如下:


How atomic Git commits dramatically increased my productivity - and will increase yours too

Atomic: of or forming a single irreducible unit or component in a larger system.

Also available on my blog.

Knowing VS Actually Knowing

I remember when my first mentor told me about Test-Driven Development. A great methodological approach to software engineering. It took me just a few minutes to understand what it was and how it worked.

Then I spent the next six months actively not practicing it. TDD is hard when you start, so you just… don’t.

Half a year later, I was hitting my head against a wall trying to build a new feature. I forced myself for the first time to actually do TDD by the book. The wall that was blocking my progress disappeared instantly.

In a previous article, I explained how many managers and tech leads often know exactly how to make our industry better… yet don’t. There is always a good reason to not do things right. And somehow we still end up surprised when we’re doing things wrong.

This long introduction is here to illustrate a point: you can know what you should do, but you might not know how important it is to actually do it. So many people out there, just like I did before, know how TDD is great… yet still don’t use it.

The simplest concepts can often completely change the way you work… if you would only apply them. Introducing: the atomic git commits.

What’s an atomic git commit?

Working with atomic git commits means your commits are of the smallest possible size. Each commit does one, and only one simple thing, that can be summed up in a simple sentence.

The amount of code change doesn’t matter. It can be a letter or it can be a hundred thousand lines, but you should be able to describe the change with one simple short sentence.

Ideally, you also want your test suite to be in the green when you commit. Your changes might be “atomic”, i.e the smallest possible, but they should also be “complete”, which means your test suite always follow through.

As small as possible, but complete: this is an atomic git commit.

Why should you write atomic git commits?

There are a few great advantages to practicing atomic git commits, and we’ll briefly detail them. But the last one really is the most important. It might completely change the way you approach your work, increase your productivity by an order of magnitude, and make your job much more enjoyable.

Reason number 1: An atomic change is a reversible change

We all know this simple truth about software: the requirements are always changing. By writing atomic git commits, we allow ourselves to revert any changes by a simple commit revert. This already increases your productivity tremendously.

Reason number 2: A clean git history

When shit hit the fan, a clean git history means the difference between pain and salvation. It’s like insuring your house: seems useless, until there’s a fire.

Reason number 3: Pull requests are much easier to review

Your team will absolutely love you for this.

Reason number 4: A much, much better workflow

This is by far the most important reason to practice atomic git commits: it completely alters the way you approach problem-solving.

If you’re like me, you might have a tendency while developing a feature, to just… do it. Entirely.

Then you realize how you did not think everything through.

You need to change more that you expected. Some edge cases aren’t taken into account. You broke some unrelated tests, they need fixing. Soon, you end up in a maze of your own making. You’re lost. Your head hurts. You can’t make any progress without being entirely focused.

Now, this is the wrong way to do things. And worse, you already know the right way, because it’s so obvious.

The well-known method to complete a big, complex task: cut it down into smaller, manageable, tiny steps. Each step -its own simple problem to solve. This is obvious advice that you probably already heard many times… But are you actually practicing it in your daily job?

Well, here’s a great way to actually practice it: write atomic git commits.

By forcefully working in atomic commits, you’re approaching the work the right way, by simplifying it into smaller steps. After all, simplifying complexity is the very core of our job. So why aren’t we always consciously doing it?

Of course this advice might sound obvious. But if my past experience proves anything, it’s that the obvious really bears repeating, and even more importantly, it bears practicing.

Make your work simpler, better, more manageable, and most importantly: make it easier. Take small steps. Write small commits. Atomic commits. You will love them.

Buy Me a Coffee ~~
hiifong 支付宝支付宝
hiifong 微信微信
0%