@@ -5,12 +5,11 @@ date = 2024-07-03T09:00:00-04:00
5
5
6
6
# Better git shell aliases: using an external shell script
7
7
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:
14
13
15
14
```
16
15
[alias]
@@ -21,20 +20,17 @@ This lovely little mess of an alias embeds a one-line shell function
21
20
tersely named "` f ` " directly into a git command. [ From the git
22
21
manual] ( https://git-scm.com/docs/git-config ) : "if the alias is
23
22
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
27
25
arguments passed in, so that typing ` git foo bar baz ` will print the
28
26
string "foobar: bar baz".
29
27
30
28
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:
38
34
39
35
```
40
36
[alias]
@@ -114,9 +110,8 @@ to refactor.
114
110
There are a couple different solutions here. First, instead of git
115
111
aliases, you could simply switch to regular shell aliases (or
116
112
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.
120
115
121
116
However, my solution was to improve my existing git aliases by simply
122
117
creating an external shell script and changing my gitconfig aliases to
@@ -237,12 +232,11 @@ scripting journey.
237
232
238
233
### A special note for alternative shell users
239
234
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.
246
240
247
241
For Fish users, there's another alternative. You can, in fact, write
248
242
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
251
245
really matters to you - that's a whole different article. Let's assume
252
246
that you, for whatever reason, prefer to write your scripts in a
253
247
"sensible language" because you want to [ "never write esac
254
- again"] ( https://fishshell.com/ ) .
248
+ again"] [ fish ] .
255
249
256
250
It's as simple as defining Fish functions (ex: ` gitex_foo ` ,
257
251
` 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`,
265
259
266
260
## In conclusion
267
261
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:
274
266
275
267
- It lets you clone using repo short names (ohmyzsh/ohmyzsh)
276
268
- It assumes you want to clone a default location (` ~/repos ` which is
@@ -289,5 +281,12 @@ clone command modified to:
289
281
290
282
Happy scripting!
291
283
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
293
288
[ 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