|
51 | 51 | [alias] foo = "!">
|
52 | 52 | <meta itemprop="datePublished" content="2024-07-01T05:29:58-04:00">
|
53 | 53 | <meta itemprop="dateModified" content="2024-07-01T05:29:58-04:00">
|
54 |
| - <meta itemprop="wordCount" content="1262"> |
| 54 | + <meta itemprop="wordCount" content="1259"> |
55 | 55 | <meta name="twitter:card" content="summary">
|
56 | 56 | <meta name="twitter:title" content="Better shell aliases in git">
|
57 | 57 | <meta name="twitter:description" content="Better shell aliases in git: adding an external shell script 10 years ago, I read this blog post on GitHub Flow git aliases by Phil Haack. From it, I learned a few really clever tricks. Even though I never much cared for using ‘GitHub Flow’ as a git workflow, I used some of those tricks for my own git aliases. One of those being this basic pattern:
|
@@ -156,9 +156,9 @@ <h1 class="f1 athelas mt3 mb1">Better shell aliases in git</h1>
|
156 | 156 | wip = "!omz_git_commit wip"
|
157 | 157 | </code></pre><p>They also wouldn’t need to reach out and touch people’s gitconfig every time Oh-My-Zsh has a new commit.</p>
|
158 | 158 | <h2 id="the-antipattern">The antipattern</h2>
|
159 |
| -<p>By now you can probably see where this is going - these kinds of complex git aliases become crazy hard to maintain over time. My <code>gitconfig</code> became a mess of functions I could no longer read or understand. Add to that the abomination of mixing code and configuration in one file, and I feel like <code>!f() { echo foobar; }; f</code> has become a true antipattern. For the occasional one-off, fine. But once I got past a certain number of these aliases at a certain complexity, it was time to refactor. So, what’s the better way? Glad you asked.</p> |
| 159 | +<p>By now you can probably see where this is going - these kinds of complex git aliases become crazy hard to maintain over time. My <code>gitconfig</code> became a mess of functions I could no longer read or understand. Add to that the abomination of mixing code and configuration in one file, and I feel like “<code>!f() { echo foobar; }; f</code>” has become a true antipattern. For the occasional one-off, fine. But once I got past a certain number of these aliases at a certain complexity, it was time to refactor. So, what’s the better way? Glad you asked.</p> |
160 | 160 | <h2 id="my-solution">My solution</h2>
|
161 |
| -<p>My solution has been to simply create a shell script external to my gitconfig that my git aliases can call. That script needs to be in the shell’s <code>$PATH</code> for git to find it. With this script I can do more complicated actions without feeling like I have to cram everything into one line. And, it’s easy to see what’s in that script and read all the code. This git extensions script file can be written as a POSIX script, Bash, Zsh, Fish, Oil, Nushell, Xonsh - whatever shell you want. And, you don’t have to convert your git aliases wholesale - you can start just with the ones that reach a certain complexity. Though I do find it easier to have all most of git subcommand handlers in one place.</p> |
| 161 | +<p>My solution has been to simply create a shell script external to my gitconfig that my git aliases can call. That script needs to be in the shell’s <code>$PATH</code> for git to find it. With this script I can do more complicated actions without feeling like I have to cram everything into one line. And, it’s easy to see what’s in that script and read all the code. This script file can be written as a POSIX script, Bash, Zsh, Fish, Oil, Nushell, Xonsh - whatever you want. And, you don’t have to convert your git aliases wholesale - you can start just with the ones that reach a certain complexity. Though I do find it easier to have all most of git subcommand handlers in one place.</p> |
162 | 162 | <h3 id="preparing-to-use-your-script">Preparing to use your script</h3>
|
163 | 163 | <p>For demo purposes, we’ll create a simple POSIX script for our “git extensions” called <code>gitex</code>. We’ll put it in <code>~/bin/gitex</code> and make it executable:</p>
|
164 | 164 | <div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>mkdir -p ~/bin <span style="color:#f92672">&&</span> touch ~/bin/gitex
|
|
0 commit comments