The Wave/Particle Duality of Git Commits

栏目: IT技术 · 发布时间: 5年前

内容简介:A lot of my friends get confused when they encounter more advanced Git topics such asIn order to understand what's going on, it's first necessary to take a quick detour into quantum physics—tongue planted firmly in cheek.If you're not already passingly fam

A lot of my friends get confused when they encounter more advanced Git topics such as git rebase . This misunderstanding typically arises during the first merge conflict that interrupts the rebase process. The central point of confusion is usually how Git can reapply one commit onto another. My friends are confused about why a snapshot could have a merge conflict. Shouldn't it be obvious, they think, what the final state should be?

In order to understand what's going on, it's first necessary to take a quick detour into quantum physics—tongue planted firmly in cheek.

Wave/particle duality

If you're not already passingly familiar with this particular flavor of apparent quantum nonsense, I am afraid you're about to get a lot more confused. It turns out that particles such as photons (and electrons, etc) have properties of both classical particles and waves. This leads to experimental results such as the well-known double-slit experiment . If photons are fired through a slit onto a piece of film, they create a band where they passed through the slit, acting as if photons are particles. However, photons that pass through two side-by-side slits interfere with themselves to create an interference pattern on a piece of film, as if photons are waves.

The Wave/Particle Duality of Git Commits

The small extra ripples are the interference from the second slit. (photo CC BY-SA 3.0, by Jordgette)

The upshot is that it is important to realize that photons are neither particles nor waves, but rathersomething strange that behaves like both.

What do photons have to do with Git?

I'm glad you asked.

It's easy to see that many Git operations treat commits as snapshots. Every Git user knows how to use checkout :

$ git checkout 42bd1ef5 && ls -lh
HEAD is now at 42bd1ef Adjust padding for social links
total 48K
drwxr-xr-x 2 georgev georgev 4.0K Jun  1  2019 archetypes
drwxr-xr-x 3 georgev georgev 4.0K Jun  1  2019 assets
drwxr-xr-x 2 georgev georgev 4.0K Jun  1  2019 images
drwxr-xr-x 7 georgev georgev 4.0K Aug 25 13:27 layouts
-rw-r--r-- 1 georgev georgev 9.3K Jan  4 12:21 README.md
drwxr-xr-x 3 georgev georgev 4.0K Aug 15 01:01 static
-rw-r--r-- 1 georgev georgev  712 Jun  1  2019 theme.toml

But other commands, such as show , output a unified diff—that is, a patch:

$ git show 42bd1ef5
commit 42bd1ef5c5541815ad0ae4eba2c695387228e719
Author: George Hilliard <thirtythreeforty@gmail.com>
Date:   Tue Dec 31 10:43:45 2019 -0600

    Adjust padding for social links

diff --git a/assets/scss/hyde-hyde/_sidebar.scss b/assets/scss/hyde-hyde/_sidebar.scss
index 0686b42..6b34856 100644
--- a/assets/scss/hyde-hyde/_sidebar.scss
+++ b/assets/scss/hyde-hyde/_sidebar.scss
@@ -54,7 +54,7 @@
 .social {
   text-align: center;
   a {
-    padding: 0 4px;
+    padding: 0 7px;
     @include link-no-decoration();
   }
 }

The same commit hash is present in both commands. There's no “ --as-patch ” or “ --as-snapshot ” flags to indicate a difference. I am in fact referring to the same object . What is going on?

Commits, it seems, are neither patches nor snapshots until they are observed. Rather, they are something strangethat can be treated as either a patch or a snapshot , just as a photon has properties of both a particle and a wave.

When Git treats a commit as a patch, it appears as the difference between its snapshot and that of its parent. When Git treats a commit as a snapshot, it appears as the tree of files that were in the staging area when the commit was made.

This duality of Git commits is what enables operations such as rebase.Git commands such as rebase , cherry-pick , format-patch , etc, all treat a Git commit as a patch, rather than as a snapshot. When viewed this way, Git's rebase operation suddenly makes a whole lot more sense: Git takes a string of commits, interprets them as patches, and applies them to the commit you specify.

The Wave/Particle Duality of Git Commits

A simple rebase, where C4 's diff has been applied to C3 . (from Git docs)

If this process goes smoothly, this creates a new string of commits, with the destination snapshot modified according to the original commits-as-patches. And so this is also why it's possible for rebase to create “merge conflicts:” it's the patches, not that snapshots, that won't apply cleanly.

Is this a silly way to think of commits? Probably. But it has been helpful to at least one other person when I explained it this way. I hope it helps you too.

You cansubscribe to updatesvia RSS or email.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

马云:未来已来

马云:未来已来

阿里巴巴集团 / 红旗出版社 / 2017-4-1 / CNY 49.00

阿里巴巴集团:全球主要的互联网公司之一,由马云带领其他17个创始人,于1999年在中国杭州创立。阿里巴巴集团经营多元化的互联网业务,以“让天下没有难做的生意”为使命,致力于为创业者和消费者提供全球化的商业平台,打造开放、协同、繁荣的电子商务生态系统。自成立以来,阿里巴巴集团建立了领先的消费者电子商务、网上支付、B2B网上交易市场及云计算业务,并积极开拓无线应用、手机操作系统和互联网电视等领域。一起来看看 《马云:未来已来》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具