Advanced git features you may not know about
Martin Heinz shares some of his favorite git features: word diff, auto-correct, plugins, and commit signing.
Martin Heinz shares some of his favorite git features: word diff, auto-correct, plugins, and commit signing.
Git is the perfect technology to maximize cheat sheet value. It has a large surface area of commands and requires some esoteric combos to get things done. (Why is there still not a git undo
?)
Jonas Lundberg:
We all do it. Up to many times a day and yet it’s rare that I meet someone that has given it a second thought. No, it’s not secretly snacking chocolate from your top office drawer.
It’s how you write and structure your commits. Possibly while snacking chocolate.
What follows is a piece marrying atomic commits (as in small commits with one focus) with Donald Knuth’s literate programming. It ends with some research on whether or not this practice commonplace on the 100 most popular GitHub repos.
Remember Patrick DeVivo’s super cool AskGit project where you can query your git repo’s history with SQL? Well, now you can kick the tires without installing a thing by using AskGit’s new web interface!
Here’s an example query where we learn that I do most of my coding (or committing, at least) on Mondays and Tuesdays while Adam and Gerhard lean towards Friday.
It was covered in fantastic detail on the Changelog #414 but now it’s real. Gitter now works natively with Matrix.
In which I detail A SQL query that helps you identify files in a codebase that have “churned” in the past year. In other words, list the files that have been changed by the most number of commits in the last year.
SELECT file,
COUNT(*)
FROM stats
JOIN commits
ON stats.commit_id = commits.id
WHERE commits.author_when > DATE('now', '-12 month')
AND commits.parent_count < 2 -- ignore merge commits
GROUP BY file
ORDER BY COUNT(*) DESC
LIMIT 50
Git is actually sooo hard. Not just to learn, but also to use consistently. And I say that as a person who used it for probably over ten years. Here’s my thoughts on the matter.
Daniel Stenberg:
Every now and then I get questions on how to work with git in a smooth way when developing, bug-fixing or extending curl – or how I do it. After all, I work on open source full time which means I have very frequent interactions with git (and GitHub). Simply put, I work with git all day long. Ordinary days, I issue git commands several hundred times.
I have a very simple approach and way of working with git in curl. This is how it works.
A nice, visual way of learning the most common git commands and how they do what they do.
bit
is an experimental modernized git CLI built on top of git that provides happy defaults.
This is not a tutorial on using Git! To follow along I advise that you have working knowledge of Git. If you’re a newcomer to Git, this tutorial is probably not the best place to start your Git journey. I suggest coming back here after you’ve used Git a bit and you’re comfortable with making commits, branching, merging, pushing and pulling.
There’s a lot of information that you are ignoring from your VCS. SQL is a great way to access it.
Following Git 2.28’s highly sought after ability to configure init.defaultBranch
comes GitHub’s support at the platform level.
You can now set the default branch name for newly-created repositories under your username. This setting does not impact any of your existing repositories. Existing repositories will continue to have the same default branch they have now.
But even if you do nothing…
On October 1, 2020, if you haven’t changed the default branch for new repositories for your user, organization, or enterprise, it will automatically change from
master
tomain
.
gix is a command-line interface (CLI) to access git repositories. It’s written to optimize the
user-experience, and perform as good or better than the canonical implementation.Furthermore it provides an easy and safe to use API in the form of various small crates for implementing your own tools in a breeze.
The author describes this as “idiomatic, modern, lean, fast, safe, & pure” but that was too many superlatives to put in the headline. It does look nice, though. I dig the libraries + CLI that leverages them approach. Demo video on Asciinema.
Built in Go, askgit
is an open source CLI and coming soon web interface (linked above). With this tool in your toolbox, you can mine your repo for info like commit count by author on each day of the week:
SELECT
count(*) AS commits,
count(CASE WHEN strftime('%w',author_when)='0' THEN 1 END) AS sunday,
count(CASE WHEN strftime('%w',author_when)='1' THEN 1 END) AS monday,
count(CASE WHEN strftime('%w',author_when)='2' THEN 1 END) AS tuesday,
count(CASE WHEN strftime('%w',author_when)='3' THEN 1 END) AS wednesday,
count(CASE WHEN strftime('%w',author_when)='4' THEN 1 END) AS thursday,
count(CASE WHEN strftime('%w',author_when)='5' THEN 1 END) AS friday,
count(CASE WHEN strftime('%w',author_when)='6' THEN 1 END) AS saturday,
author_email
FROM commits GROUP BY author_email ORDER BY commits
Leading off the updates for Git 2.28 is the highly sought after ability to configure init.defaultBranch
so folks can move from master
to main
as their default branch name.
From Taylor Blau on the GitHub blog:
When you initialize a new Git repository from scratch with
git init
, Git has always created an initial first branch with the namemaster
. In Git 2.28, a new configuration option,init.defaultBranch
is being introduced to replace the hard-coded term. (For more background on this change, this statement from the Software Freedom Conservancy is an excellent place to look).Starting in Git 2.28,
git init
will instead look to the value ofinit.defaultBranch
when creating the first branch in a new repository. If that value is unset,init.defaultBranch
defaults tomaster
…
Also check out github/renaming to learn more about the complementary changes GitHub is making. GitLab and Bitbucket are making similar changes.
gitqlite
is a tool for running SQL queries on git repositories. It implements SQLite virtual tables and uses go-git. It’s meant for ad-hoc querying of git repositories on disk through a common interface (SQL), as an alternative to patching together various shell commands.
Mine your repo’s history for goodies. Here’s how to get commit count by author email:
SELECT author_email, count(*) FROM commits GROUP BY author_email ORDER BY count(*) DESC
From Kabir Nazir on the AltCampus blog:
One of the things a lot of newbie developers overlook often is the format of their commit messages. Properly formatted commit messages can do so much more than just looking neat…
Use the imperative mood(present tense) when framing messages … Think of each commit in your code as a change that is being applied to your codebase.
This makes heavy use of fzf, which is a command-line fuzzy finder. You can stage and unstage files fuzzily found by the tool and commit interactively.
Max Mulatz:
This is a call for solidarity and a love letter to good commit messages. Read this to find out why it’s worth to care about commit messages and how to get better at writing them.
gitpert
measures the “pertinence” of git authors as a time-decayed measure of LOC added and removed to a repository (or a set of files in a repository). It’s meant to help identify who the most relevant contributors are based on commit recency, frequency and impact.
Cool tool, as long as we don’t forget about non-code contributors.
Delta provides language syntax-highlighting, within-line insertion/deletion detection, and restructured diff output for git on the command line. All the syntax-highlighting color themes that are available with bat are available with delta. Here’s what
git show
looks like when git is configured to use delta as its pager:
Written in C++ and powered by Qt 5. #notelectron
David Thompson:
I like Git commit messages. Used well, I think they’re one of the most powerful tools available to document a codebase over its lifetime. I’d like to illustrate that by showing you my favourite ever Git commit.
6-ish paragraphs of explanation for a single whitespace change in the code.
The New Stack takes us on a fun trip down memory lane:
Fifteen years ago a number of the Linux kernel developers tossed their hands in the air and gave up on their version control system, BitKeeper. Why? The man who held the copyright for BitKeeper, Larry McVoy, withdrew free use of his product on claims that one of the kernel devs had reverse engineered one of the BitKeeper protocols.
Linux creator Linus Torvalds sought out a replacement to house the Linux kernel code. After careful consideration, Torvalds realized none of the available options were efficient enough to meet his needs: