Git 2.17 is now available (original) (raw)

The open source Git project just released Git 2.17.0, with features and bugfixes from over 60 contributors. Here’s our look at some of the most interesting new features from the last few versions of Git.

Coloring moved code

When you’re reviewing a commit that moves some code, its diff will show a big chunk of lines being deleted in one place and a big chunk of lines appearing elsewhere. The commit message may claim that this was pure code movement, but how can you verify that? And if some lines needed to be changed, how can you easily see which lines?

Git v2.15 offers a new --color-moved option which colors groups of moved lines differently. Use this option for any command that generates a diff: git diff, git log -p, git show, and so on.

Diff shown with --color-moved

The blue lines show the blocks that were moved verbatim, while the red and green lines show the corrected typo. Note that these aren’t the default colors; we’ve just used them to highlight this simple example. By default, old and new blocks get their own colors, and non-adjacent groups of lines are colored independently. This helps when there are several moved blocks in a single diff. You can configure the colors to match your preferences.

[source]

Speeding up status with watchman

One reason that Git is fast is avoiding unnecessary disk reads. For example, when you run git status, Git uses the stat() system call to check whether each file’s size or timestamp has changed since the last time Git checked. If not, it assumes that its contents haven’t changed and avoids reading the file contents at all.

This helps a lot, but it still requires Git to call stat() once for every file in your working copy. If you have a lot of files, that work adds up. This can be further exacerbated if you’re on an operating systems where stat() is more expensive.

Git v2.16 adds a new fsmonitor hook that relies on the operating system to tell us what has changed. Doing so allows us to avoid the repeated stat() calls, making the work proportional to the number of changed files, not the total size of the repository.

The hook provides a generic interface into which you can plug any tool, but most people will want to use the watchman tool, which runs on
Linux, macOS, and Windows. Git ships with a sample hook for using watchman.

[source]

Finding objects in history

Have you ever gotten hold of an object hash for a blob or tree like c1626490c2 and had no idea what path it corresponds to, or in which commits it could be found? Perhaps somebody else mentioned the object, or a Git command lke fsck complained about it.

There’s no single defined answer for these questions. Because Git commits are snapshots of the tree, an object may appear in many commits: it may get introduced in commit A, then persist in commit B that only touches another file, and then finally go away in commit C. Likewise, an object may appear at multiple paths if it’s renamed or copied.

Getting this information used to require a pipeline of arcane commands. But now you can use --find-object to select the commits you want and display them however you like.

Using --find-object to locate a commit

Here we’ve used --raw to easily show that these commits are the ones where the object comes and goes (and that Git omitted any in the middle which didn’t affect it). You might be more likely to use -p to show the full diff.

[source]

Grepping with function context

When you search for a string with git grep, it’s often helpful to see the surrounding context. You can use -C 2 to tell Git to show you two lines of context on either side, but it’s hard to know the right number in advance without ending up with too much or too little context.

For many years, git grep has had a --function-context option (and its shorter alias -W) to show you the whole function surrounding each match. This uses language-specific patterns if you have the appropriate gitattributes defined, or it can fall back to some generic heuristics.

Since Git v2.16, it can now also find and show comments before the start of a function, which provides even more context for your matches.

grep -W shows comments above function context

Without -W, we’re left with very little information about how the function is used. With it, we can see the whole function, including its accompanying comment.

[source]

Tidbits

And the rest

That’s just a sampling of changes from the last few versions. Read the full release notes for 2.17, or find the release notes for previous versions in the Git repository.

Written by

Jeff King

Explore more from GitHub

Docs

Docs

Everything you need to master GitHub, all in one place.

Go to Docs

GitHub

GitHub

Build what’s next on GitHub, the place for anyone from anywhere to build anything.

Start building

Customer stories

Customer stories

Meet the companies and engineering teams that build with GitHub.

Learn more

Enterprise content

Enterprise content

Executive insights, curated just for you

Get started