Skip to content

Commit 448e8fa

Browse files
committed
Sort links
1 parent b851ea6 commit 448e8fa

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

content/posts/2024-07-03-better-git-shell-aliases.md

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ date = 2024-07-03T09:00:00-04:00
55

66
# Better git shell aliases: using an external shell script
77

8-
10 years ago, I read [this blog post on GitHub Flow git
9-
aliases](https://haacked.com/archive/2014/07/28/github-flow-aliases/)
10-
by Phil Haack. From it, I learned a few really clever tricks. Even
11-
though I never much cared for using 'GitHub Flow' as a git workflow, I
12-
used some of those tricks for my own git aliases. One of those being
13-
this basic pattern:
8+
10 years ago, I read [this blog post][github-flow-aliases] on GitHub
9+
Flow git aliases by Phil Haack. From it, I learned a few really clever
10+
tricks. Even though I never much cared for using 'GitHub Flow' as a
11+
git workflow, I used some of those tricks for my own git aliases. One
12+
of those being this basic pattern:
1413

1514
```
1615
[alias]
@@ -21,20 +20,17 @@ This lovely little mess of an alias embeds a one-line shell function
2120
tersely named "`f`" directly into a git command. [From the git
2221
manual](https://git-scm.com/docs/git-config): "if the alias is
2322
prefixed with an exclamation point, it will be treated as a shell
24-
command". [Other sources around the same
25-
time](https://www.atlassian.com/blog/git/advanced-git-aliases) also
26-
began promoting that same trick. It lets you handle all the other
23+
command". [Other sources around the same time][advanced-git-aliases]
24+
also began promoting that same trick. It lets you handle all the other
2725
arguments passed in, so that typing `git foo bar baz` will print the
2826
string "foobar: bar baz".
2927

3028
That trick opens the door to let you create all sorts of clever
31-
aliases like [`git
32-
browse`](https://haacked.com/archive/2017/01/04/git-alias-open-url),
33-
which navigates to the remote URL of your repo in a web browser. The
34-
'browse' git alias Phil published in that link is very Windows/Git
35-
Bash specific, and it doesn't work for repos cloned with the
36-
`[email protected]:my/repo` form, so I had to modify it in my config to
37-
become:
29+
aliases like [`git browse`][git-alias-open-url], which navigates to
30+
the remote URL of your repo in a web browser. The 'browse' git alias
31+
Phil published in that link is very Windows/Git Bash specific, and it
32+
doesn't work for repos cloned with the `[email protected]:my/repo` form,
33+
so I had to modify it in my config to become:
3834

3935
```
4036
[alias]
@@ -114,9 +110,8 @@ to refactor.
114110
There are a couple different solutions here. First, instead of git
115111
aliases, you could simply switch to regular shell aliases (or
116112
functions). This is a perfectly acceptable option, and there are
117-
[already
118-
discussions](https://www.reddit.com/r/git/comments/9drimq/aliases_git_or_bash/)
119-
you can find on that topic.
113+
[already discussions][reddit_aliases_git_or_bash] you can find on that
114+
topic.
120115

121116
However, my solution was to improve my existing git aliases by simply
122117
creating an external shell script and changing my gitconfig aliases to
@@ -237,12 +232,11 @@ scripting journey.
237232

238233
### A special note for alternative shell users
239234

240-
If you are a [Fish shell](https://fishshell.com/) user, it can be
241-
especially daunting (or at the least, annoying) to deal in POSIX shell
242-
syntax. If you want to write your `gitex` script in Fish (or another
243-
scripting language), you can easily do that by changing the shebang
244-
from `#!/bin/sh` to `#!/usr/bin/env fish` and then writing your script
245-
in that language.
235+
If you are a [Fish shell][fish] user, it can be especially daunting
236+
(or at the least, annoying) to deal in POSIX shell syntax. If you want
237+
to write your `gitex` script in Fish (or another scripting language),
238+
you can easily do that by changing the shebang from `#!/bin/sh` to
239+
`#!/usr/bin/env fish` and then writing your script in that language.
246240

247241
For Fish users, there's another alternative. You can, in fact, write
248242
plain old Fish functions and assign them to git aliases! While POSIX
@@ -251,7 +245,7 @@ probably know all those pros/cons and can weigh whether any of that
251245
really matters to you - that's a whole different article. Let's assume
252246
that you, for whatever reason, prefer to write your scripts in a
253247
"sensible language" because you want to ["never write esac
254-
again"](https://fishshell.com/).
248+
again"][fish].
255249

256250
It's as simple as defining Fish functions (ex: `gitex_foo`,
257251
`gitex_bar`, etc), and then add this to your `gitconfig`:
@@ -265,12 +259,10 @@ It's as simple as defining Fish functions (ex: `gitex_foo`,
265259

266260
## In conclusion
267261

268-
You can [check out my dotfiles](https://github.com/mattmc3/dotfiles/)
269-
if you want to see [my `gitex`
270-
implementation](https://github.com/mattmc3/dotfiles/blob/main/bin/gitex).
271-
It has some helpful extras like supporting kebab-case-aliases, as well
272-
as my favorite alias `git cloner`, which enhances `git clone` with
273-
some extras:
262+
You can [check out my dotfiles][dotfiles] if you want to see [my
263+
`gitex` implementation][gitex]. It has some helpful extras like
264+
supporting kebab-case-aliases, as well as my favorite alias `git
265+
cloner`, which enhances `git clone` with some extras:
274266

275267
- It lets you clone using repo short names (ohmyzsh/ohmyzsh)
276268
- It assumes you want to clone a default location (`~/repos` which is
@@ -289,5 +281,12 @@ clone command modified to:
289281

290282
Happy scripting!
291283

292-
[omz]: https://github.com/ohmyzsh/ohmyzsh
284+
[advanced-git-aliases]: https://www.atlassian.com/blog/git/advanced-git-aliases
285+
[dotfiles]: https://github.com/mattmc3/dotfiles/
286+
[fish]: https://fishshell.com/
287+
[git-alias-open-url]: https://haacked.com/archive/2017/01/04/git-alias-open-url
293288
[git-commit]: https://github.com/ohmyzsh/ohmyzsh/blob/master/plugins/git-commit/git-commit.plugin.zsh
289+
[gitex]: https://github.com/mattmc3/dotfiles/blob/main/bin/gitex
290+
[github-flow-aliases]: https://haacked.com/archive/2014/07/28/github-flow-aliases/
291+
[omz]: https://github.com/ohmyzsh/ohmyzsh
292+
[reddit_aliases_git_or_bash]: https://www.reddit.com/r/git/comments/9drimq/aliases_git_or_bash/

0 commit comments

Comments
 (0)