<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:base="https://illuminatedcomputing.com/">
  <id>https://illuminatedcomputing.com/</id>
  <title>Illuminated Computing</title>
  <updated>2023-11-06T00:00:00Z</updated>
  <link rel="alternate" href="https://illuminatedcomputing.com/" type="text/html"/>
  <link rel="self" href="https://illuminatedcomputing.com/tags/git/atom.xml" type="application/atom+xml"/>
  <author>
    <name>Paul A. Jungwirth</name>
    <uri>https://illuminatedcommputing.com/</uri>
  </author>
  <entry>
    <id>tag:illuminatedcomputing.com,2023-11-06:/posts/2023/11/git-for-postgres-hacking/</id>
    <title type="html">Git for Postgres Hacking</title>
    <published>2023-11-06T00:00:00Z</published>
    <updated>2023-11-06T00:00:00Z</updated>
    <link rel="alternate" href="https://illuminatedcomputing.com/posts/2023/11/git-for-postgres-hacking/" type="text/html"/>
    <content type="html">
&lt;p&gt;In Postgres development it’s normal for patch attempts to require many revisions and last a long time. I just sent in &lt;a href="https://www.postgresql.org/message-id/2d9740ad-3bb7-473c-8441-344351caa8ee%40illuminatedcomputing.com"&gt;v17&lt;/a&gt; of my &lt;a href="https://commitfest.postgresql.org/44/4308/"&gt;SQL:2011 application time patch&lt;/a&gt;. The commitfest entry dates back to summer of 2021, but it’s really a continuation of &lt;a href="https://www.postgresql.org/message-id/flat/20200930073908.GQ1996%40paquier.xyz#c63d0a97f3c7bee005a1840164f00688"&gt;this thread from 2018&lt;/a&gt;. And it’s not yet done.&lt;/p&gt;

&lt;p&gt;My &lt;a href="https://commitfest.postgresql.org/31/2112/"&gt;work on multiranges&lt;/a&gt; is a similar story: 1.5 years from first patch to committed.&lt;/p&gt;

&lt;p&gt;Today I saw &lt;a href="https://jvns.ca/blog/2023/11/06/rebasing-what-can-go-wrong-/"&gt;this post&lt;/a&gt; by Julia Evans about problems people have with git rebase (&lt;a href="https://news.ycombinator.com/item?id=38164046"&gt;also see the hn discussion&lt;/a&gt;), and it reminded me of my struggles handling long-lived branches.&lt;/p&gt;

&lt;p&gt;In my early days with git I avoided rebasing, because I wanted the history to be authentic. Nowaday I rebase pretty freely, both to move my commits on top of the latest &lt;code&gt;master&lt;/code&gt; branch work and to interactively clean things up so the commits show logical progress (with generous commit messages explaining the motivation and broad design decisions: the “why”).&lt;/p&gt;

&lt;p&gt;But in my paid client work, PRs get merged pretty fast. There is nothing like the multi-year wait of Postgres hacking. Often I’ve wished for more history there. It’s not my day job, so it’s hard to remember fine details about something from months or years ago. And I’ve changed direction a couple times, and sometimes I want a way to consult that old history.&lt;/p&gt;

&lt;p&gt;But with Postgres you don’t have any choice but to rebase. You send your patch files to a mailing list, and if they don’t apply cleanly no one will look at them. I’ve spent hours and hours rebasing patches because the underlying systems changed before they could get committed.&lt;/p&gt;

&lt;p&gt;With multiranges this was tough, but at least it was just one patch file. Application time is a series of five patches, which over time have changed order and evolved from four. When it’s time to send a new version, I run &lt;code&gt;git format-patch&lt;/code&gt;, which turns each commit into a &lt;code&gt;.patch&lt;/code&gt; file. So I need to wind up with five well-groomed commits rebased on the latest &lt;code&gt;master&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;My personal copy of the postgres repo on github has &lt;a href="https://github.com/pjungwir/postgresql/branches/all"&gt;a bunch of silly-named branches&lt;/a&gt; for stashing work when I want to change direction, so the history isn’t totally lost. But for a long time I had no system. It feels like when you see a spreadsheet named &lt;code&gt;Annual Report - Copy of Jan 7.bak - final - FINAL.xls&lt;/code&gt;. After all these years it’s unmanageable. (Okay at least I know not to name any Postgres submission “final”! ;-)&lt;/p&gt;

&lt;p&gt;I think I finally found a way to keep history that works for me. On my main &lt;a href="https://github.com/pjungwir/postgresql/commits/valid-time"&gt;&lt;code&gt;valid-time&lt;/code&gt; branch&lt;/a&gt; I keep a series of commits for each small change. I rebase to move them up and down, so that they will squash cleanly into the five commits I need at the end. You can see that I have one main commit for each of the five patches, but each is followed by many commits named &lt;code&gt;fixup pks: fixed this&lt;/code&gt; or &lt;code&gt;fixup fks: feedback from so-and-so&lt;/code&gt;. I rebase on &lt;code&gt;master&lt;/code&gt; every so often. I force-push all the time, since no one else uses the repo. (I do work on both a laptop and a desktop though, so I have to remember to &lt;code&gt;git fetch &amp;amp;&amp;amp; git reset --hard origin/valid-time&lt;/code&gt;.)&lt;/p&gt;

&lt;p&gt;When I’m ready to submit new patches, I take a snapshot with &lt;code&gt;git checkout -b valid-time-v17-pre-squash&lt;/code&gt; and “make a backup” with &lt;code&gt;git push -u&lt;/code&gt;. Then I make a branch to squash things (&lt;code&gt;git checkout -b valid-time-v17&lt;/code&gt;). I do a &lt;code&gt;git rebase -i HEAD~60&lt;/code&gt;, press &lt;code&gt;*&lt;/code&gt; on &lt;code&gt;pick&lt;/code&gt;, type &lt;code&gt;cw fixup&lt;/code&gt;, then &lt;code&gt;n.n.n.n.n.n.&lt;/code&gt;, etc. ’til I have just the five commits. Then I have a script to do a clean build + test on each commit, since I want things to work at every point. While that’s running I write the email about the new patch, and hopefully send it in.&lt;/p&gt;

&lt;p&gt;So now I’m capturing the fine-grained history that went into each submission, and that won’t change no matter how aggressively I rebase the current work. I’m pretty happy with this flow. I wish I had started years ago.&lt;/p&gt;

&lt;p&gt;One git feature I could &lt;em&gt;almost&lt;/em&gt; use is &lt;code&gt;git rebase -i --autosquash&lt;/code&gt;. (Here are some &lt;a href="https://thoughtbot.com/blog/autosquashing-git-commits"&gt;articles&lt;/a&gt; &lt;a href="https://andrewlock.net/smoother-rebases-with-auto-squashing-git-commits/"&gt;about&lt;/a&gt; &lt;a href="https://git-scm.com/docs/git-rebase"&gt;it&lt;/a&gt;.) If your commit messages are named &lt;code&gt;fixup! foo&lt;/code&gt;, then git will automatically set those commits to &lt;code&gt;fixup&lt;/code&gt;, not &lt;code&gt;pick&lt;/code&gt;, and it will move them to just below whatever commit matches &lt;code&gt;foo&lt;/code&gt;. I follow this pattern but with &lt;code&gt;fixup&lt;/code&gt; not &lt;code&gt;fixup!&lt;/code&gt;, to keep it all manual. At first I just didn’t trust it (or myself).&lt;/p&gt;

&lt;p&gt;Now I’m ready to move to this workflow, but I’m not sure how to “match” one of my five main commits. I want a meaningful title (i.e. the first line of the commit message) for each little commit, so I use short abbreviations for the patch they target, e.g. &lt;code&gt;fixup pks: Add documentation for pg_constraint.contemporal column&lt;/code&gt;. Git doesn’t know that it should match &lt;code&gt;pks&lt;/code&gt; to &lt;code&gt;Add temporal PRIMARY KEY and UNIQUE constraints&lt;/code&gt; and ignore everything after the colon. If there were a way to preserve tags after a rebase I think I could tag the main commit as &lt;code&gt;pks&lt;/code&gt; and it &lt;em&gt;might&lt;/em&gt; work (but maybe not with the extra stuff after the colon).&lt;/p&gt;

&lt;p&gt;You can have git generate the new commit message for you with &lt;code&gt;git commit --fixup $sha&lt;/code&gt;, but it just copies the whole title verbatim, which is not what I want. Also who wants to remember &lt;code&gt;$sha&lt;/code&gt; for those five parent commits? And finally, I want to move these commits into place immediately, so I can build &amp;amp; test against each patch as I work. Git can’t move them for me without squashing them.&lt;/p&gt;

&lt;p&gt;The Thoughtbot article linked above says you can use a regex, e.g. &lt;code&gt;git commit --fixup :/pks&lt;/code&gt;, but: (1) The regex is used immediately to find the parent, but it gets replaced with that parent’s title. It doesn’t stay in your commit message. (2) If you give an additional commit message, it goes two lines below the &lt;code&gt;fixup!&lt;/code&gt; line, so it’s not in the commit title. This only solves having to remember &lt;code&gt;$sha&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;What I really want is &lt;code&gt;fixup! ^: blah blah blah&lt;/code&gt; where &lt;code&gt;^&lt;/code&gt; means “the closest non-squashed parent”, &lt;strong&gt;and&lt;/strong&gt; the &lt;code&gt;^&lt;/code&gt; is resolved at rebase time, not commit time, &lt;strong&gt;and&lt;/strong&gt; everything after the colon is not used for matching. (If it needs to be a regex then &lt;code&gt;:/.&lt;/code&gt; is sufficient too.)&lt;/p&gt;

&lt;p&gt;Anyway I’m using my manual process for now, since with vim I can change 60 &lt;code&gt;pick&lt;/code&gt;s to &lt;code&gt;fixup&lt;/code&gt; in a few seconds. I’m not willing to lose meaningful titles to save a few seconds with &lt;code&gt;fixup!&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Nonetheless it would be nice to have one less step I have to remember. Involuntarily I keep thinking about how I can make this feature work for me. If someone has a suggestion, please do let me know.&lt;/p&gt;

&lt;p&gt;Another approach is “stacked commits”. I went as far as installing &lt;a href="https://github.com/arxanas/git-branchless"&gt;git branchless&lt;/a&gt; and reading the docs &lt;a href="https://jg.gg/2018/09/29/stacked-diffs-versus-pull-requests/"&gt;and&lt;/a&gt; &lt;a href="https://kastiglione.github.io/git/2020/09/11/git-stacked-commits.html"&gt;some&lt;/a&gt; &lt;a href="https://andrewlock.net/working-with-stacked-branches-in-git-is-easier-with-update-refs/"&gt;articles&lt;/a&gt;, but to be honest I never went beyond a few tests, and I haven’t thought about it for a few months. It’s in the back of my head to give it a more honest effort.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <id>tag:illuminatedcomputing.com,2012-10-22:/posts/2012/10/git-line-highlighting/</id>
    <title type="html">Highlighting Lines in Github</title>
    <published>2012-10-22T00:00:00Z</published>
    <updated>2012-10-22T00:00:00Z</updated>
    <link rel="alternate" href="https://illuminatedcomputing.com/posts/2012/10/git-line-highlighting/" type="text/html"/>
    <content type="html">
&lt;p&gt;Did you know that when you link to a file on Github, you can use the “fragment” (the part after the &lt;code&gt;#&lt;/code&gt;) to link directly to a specific line, or even highlight a range of lines? For example, to show the code for Capistrano’s &lt;code&gt;deploy:assets:precompile&lt;/code&gt; task:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/capistrano/capistrano/blob/dbdf5a13ed2885610896563cbfd7df14e1c283c1/lib/capistrano/recipes/deploy/assets.rb#L31-43"&gt;https://github.com/capistrano/capistrano/blob/dbdf5a13ed2885610896563cbfd7df14e1c283c1/lib/capistrano/recipes/deploy/assets.rb#L31-43&lt;/a&gt;&lt;/p&gt;
</content>
  </entry>
  <entry>
    <id>tag:illuminatedcomputing.com,2012-07-18:/posts/2012/07/git-compare-two-branches/</id>
    <title type="html">Git: Compare two branches</title>
    <published>2012-07-18T00:00:00Z</published>
    <updated>2012-07-18T00:00:00Z</updated>
    <link rel="alternate" href="https://illuminatedcomputing.com/posts/2012/07/git-compare-two-branches/" type="text/html"/>
    <content type="html">
&lt;p&gt;I don’t know if this is common knowledge, but with git it’s very easy (and handy) to see which commits are unmerged between two branches. Suppose you do work on the &lt;code&gt;master&lt;/code&gt; branch, then merge that into &lt;code&gt;staging&lt;/code&gt; and &lt;code&gt;production&lt;/code&gt; before a new release. To see what is going to go into the next staging release, you just pass a range to &lt;code&gt;git log&lt;/code&gt; like this:&lt;/p&gt;

&lt;div class="CodeRay"&gt;&lt;div class="code"&gt;&lt;pre&gt;&lt;code class="language-bash"&gt;git log staging..master&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That will show you the changes in &lt;code&gt;master&lt;/code&gt; that have not yet been merged into &lt;code&gt;staging&lt;/code&gt;. In general, the pattern is &lt;code&gt;git log &amp;lt;since&amp;gt;..&amp;lt;until&amp;gt;&lt;/code&gt;. My intuition says the order is backwards here, since you’re giving the destination branch first, but it makes sense if you think about it. The destination branch is on an earlier commit than the source branch, so it’s going to come first.&lt;/p&gt;

&lt;p&gt;You can use the same approach to get a diff:&lt;/p&gt;

&lt;div class="CodeRay"&gt;&lt;div class="code"&gt;&lt;pre&gt;&lt;code class="language-bash"&gt;git diff staging..master&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I think this is really handy when reviewing code before a merge!&lt;/p&gt;
</content>
  </entry>
  <entry>
    <id>tag:illuminatedcomputing.com,2011-03-15:/posts/2011/03/what-git-add-does/</id>
    <title type="html">What git add does</title>
    <published>2011-03-15T00:00:00Z</published>
    <updated>2011-03-15T00:00:00Z</updated>
    <link rel="alternate" href="https://illuminatedcomputing.com/posts/2011/03/what-git-add-does/" type="text/html"/>
    <content type="html">
&lt;p&gt;I’ve used git on a few small projects now—not real work, just quick scripts for things—and so far I’ve mostly treated it like Subversion. I knew about the staging area, but it was a little blurry, and the need to type &lt;code&gt;commit -a&lt;/code&gt; on files I’d already added was just a speck of annoyance. But suddenly it struck me that git’s &lt;code&gt;add&lt;/code&gt; is completely different from svn’s &lt;code&gt;add&lt;/code&gt;. On svn, &lt;code&gt;add&lt;/code&gt; puts your file under version control. On git, &lt;code&gt;add&lt;/code&gt; promotes your file (whether new, changed, or deleted) to the staging area. This distinction is somewhat obscured by &lt;code&gt;commit -a&lt;/code&gt;, which only operates on files that have at least one commit—or, it might seem, on files that are not yet under version control because you’ve never used &lt;code&gt;add&lt;/code&gt; on them.&lt;/p&gt;

&lt;p&gt;I’m sure this note is no news to regular git users, and maybe it’s incomprehensible to others, but for me it’s one of those minor epiphanies, and I thought I’d document it for a change.&lt;/p&gt;
</content>
  </entry>
</feed>

