New in Git 3: closures

Original author: Junio ​​Hamano
  • Transfer

Git is a popular version control system. In it, the atomic change of one or more files is called a commit, and several consecutive commits are combined in a branch. Branches are used to implement new ideas (features).



It happens that the idea turns out to be a dead end, the developer turns off the wrong way, and there is a need to roll back to the original version, for this you need to forget about the new branch and switch to the main dev or master , and then continue working as if nothing had happened. In this case, the "shoot" will hang forever, like the desire to remove it. But how to delete if it is part of the story? This process shows the efforts of the workaholic programmer, albeit in vain. It’s easier to report to the authorities, because an unsuccessful result is also a result!


I hasten to please: the Git developers in version 3 will introduce a new command to close such stray branches. Let me remind you that the current current version is 2.21.0 .


How to use this team, what does it give and what do IT companies think? The article answers these and other questions.


Description


Now you can close an unsuccessful branch with one of the previous commits. The arcs of the fault are colored yellow in the figures below.




Here the commit 4is the last one for an unsuccessful feature. He was closed with 1, and then returned to the master and went the other way, from the commit 5.


You can also lock the commit into itself, thus creating loops :




You can lock yourself into any commit - smart Git will calculate the difference and combine everything correctly:




How to use?


The mergeclosure functionality is not included in the command, since for the first case the branch will be fast-forged , and for the second it will not do anything ( git already up to date).


In order not to change the old behavior, the developers decided to enter a command to close:


git closure -s $source_commit -d $dest_commit -m $message

The first argument -s $source_commitsets the hash of the commit from which to extend the “loop”, and the second, optional -d $dest_commit, sets the commit to which the loop needs to be closed. If it is absent, then the closure occurs in the current check-out branch. The parameter -m $messagesets the closure message, type failed feature, revert to origin. However, a parameter is also available --allow-empty-messagethat allows commits without message text. By default, Git allows exactly one closure for a couple of commits. An option is available to circumvent this limitation --allow-multiple-closures.



After the command is executed, the git itself will calculate the changes, and in the final commit, double diff will be visible: from the base and trailing branches. In general, this is an n-dimensional diff, that is, you can perform closure as many times as you like. closure-commit is similar to merge-commit with the difference that it stores several messages, not one.


Unfortunately, the existing GUIs for working with Git do not yet fully support closures. The preview version of GitExtensions builds curves like a merge instead of a beautiful arc. Pay attention to the new fields Closure messageand Closure diff:



It is worth noting that the team closurealways changes the story (still, now Git is a full-fledged time machine!), So now you can push branches only with the option --force, or with the help of the safe one --force-with-lease.


Rebases for branches with loops are also available, however, the logic for recounting commits in them is complicated.


Also, the option autoallows you to automatically close all old branches. In this case, the closing commit is the one from which the branching started. Using plugins for the Git IDE, closures can be run periodically. In GitExtensions, a similar Delete obsolete branches plugin removes obsolete branches.


Opinion of IT companies


Large IT companies: Google, Facebook, Apple, DeepMind, Positive Technologies, and especially Microsoft, are looking forward to closures, because now it will be possible to formalize the life cycle of branches, including non-contiguous ones.


One of Microsoft's top managers, Michael Richter, writes :


The new feature of the git will certainly reduce the chaos in the world of Open Source development and not only. Our repositories have a lot of hanging branches. For example, there are more than 200 of them in vscode , and there are more than 300 of them in TypeScript ! And this is not just Microsoft's problem. Closures not only improve the organization, but also allow you to track the programmer’s reasoning, sometimes completely incomprehensible even to colleagues :) Closures reminded me of the film "Back to the Future" - where the characters traveled to the past and the future. I love this film, I reviewed it several times. And I think that I will love the git because of this even more :)

On a note


If earlier the commit graph was a directed acyclic graph (DAG), then closures extend it to a generalized directed graph . With Git, it will be possible to describe regular expressions in which the states are commits, and the alphabet is the set of all messages. But this smacks of the hub "abnormal programming", and therefore is beyond the scope of the article. However, if you are interested in this, check out the article that describes how you can store family trees inside Git.

Only registered users can participate in the survey. Please come in.

What do you think about closures?

  • 36% Cool feature, looking forward to! Git has long lacked the ability to move in time. 257
  • 12.4% It is doubtful how old tools will work with this? 89
  • 28.7% This is the fruit of a sick imagination of the authors, the world has gone crazy! 205
  • 22.7% In parallel, since I am from a parallel universe. 162

Also popular now: