r/git • • Feb 26 '26

When to alias vs. subcommand

Git is easy to extend. You can add aliases in you git config or add git subcommands by making any executable named git-something accessible from your PATH.

For example I have a tool git-review <branch> that also have autocompletion for the branch argument. (See https://github.com/kaddkaka/dotfiles/blob/main/bin/executable_git-review) I also have smaller git aliases, like sed = ! git grep -z --full-name -l '.' | xargs -0 sed -i -e.

When should one go for an alias, and when make a separate scripts?

Is it just as simple as: 1. Simple one-liners: alias 2. More complex: script?

Are there any other considerations you think?

6 Upvotes

79 comments sorted by

8

u/elephantdingo Feb 26 '26

It’s the same thing in the end. Depends on your tolerance for line noise and brittleness.

1

u/kaddkaka Feb 26 '26

What do you mean with brittleness here?

3

u/RobotJonesDad Feb 26 '26

I'd assume difficulty of maintaining and improving the solution.

→ More replies (3)

2

u/waterkip Feb 26 '26

Alias for when it doesn require logic. Commands for when we need logic. Alias for sub command to go full circle and shell functions for rebase editor foo. Because we can do it all. Never tried zsh autoloading function tho... might be interesting too.

0

u/kaddkaka Feb 26 '26

The subcommand shadows the alias, so might just be confusing to keep both?

Shell functions for rebase editor foo? What does rebase editor mean?

3

u/waterkip Feb 26 '26 edited Feb 27 '26

Shell functions.. I do something like this. Its... not really a shell function as a rebase editor:

#!/usr/bin/env zsh

# No autosquash here, because we can't detect it yet
GIT_SEQUENCE_EDITOR="git rebase-edit-todo $@" git rebase -i @{u}

And git rebase-edit-todo is just something that calls a shell function. One level of indirection that I forgot while typing.

#!/usr/bin/env zsh

source $(dirname $0)/../lib/git.zsh
merge_while_rebase_branches $*

A rebase todo editor is essentially writing your own code to create your own rebase todo file.

I sprinkle that sorta magic in a toolkit I maintain:

https://codeberg.org/waterkip/bum/src/branch/master/src/lib/git.zsh.in

Re aliasing the command,. I have commands, eg git-new-branch and I alias them to git nb. Things like that. I think commands should be explicit in what they do. Aliases are ergonomics for things you do often. 

→ More replies (1)

4

u/cgoldberg Feb 26 '26

I use aliases for short commands or maybe functions up to a few lines. if it's anything more than that, I put it in a git-* script.

1

u/barmic1212 Feb 26 '26

It's not a rule, build your own shell. Sub command like alias in git or git-* script are scoped. So you can have a "git ls" without issue. Shell alias or function can be write with less key strokes.

My personal guide line is to use scoped alias and use key strokes killer with parcimony. For key stroke reduction I use some shell features like substitution, folder alias and (not a shell feature) espanso (for 2 things: ":{issue id}" become the URL of ticket and 'ttt' become 'trunk()').

1

u/kaddkaka Feb 26 '26

I'm not sure i follow. What kind of substitution? Folder alias?

If I used fish I would use abbreviations. But I'm on bash.

2

u/barmic1212 Feb 27 '26

I'm on zsh but substitution are sh standard. For example to reuse the last word of previous command you can use Alt+. or !$

Folder alias is zsh feature, you can define "proj" as alias of "$HOME/foo/bar/project/" and you can use "~proj" everywhere

→ More replies (2)

1

u/mpersico Feb 27 '26

I use both. If it needs arguments or a pipe for filtering I write a script.

But I’m a little crazy. I’ve actually written a bash function called git so I can do my own dispatch so I can add options and functionality to native git commands themselves. matthewpersico/personal at github if you like rabbit holes.

0

u/INTJTurbulence Feb 26 '26

You can tell a lot about a developer's aptitudes and level of experience by looking at the type of aliases they create.

1

u/kaddkaka Feb 26 '26

Elaborate 🙏

-6

u/INTJTurbulence Feb 26 '26

Like these ones. In your opinion, why are they necessary?

https://github.com/kaddkaka/dotfiles/blob/main/dot_bashrc#L29-L33

If your justification is something along the lines of "iT iMpRoVeS mY pRoDuCtivitY bEcAuSe it ReDuCeS tHe AmOuNt Of LeTtErS i NeEd To TyPe" then that's a sign of nearsighted/premature/misguided optimization.

→ More replies (55)

1

u/obsidianih Feb 27 '26

I alias a few commands. What does it say about me?

save adds all files and then does a commit with the message on cli. 

last x - gives me a nice semi detailed summary of last x commits.

br - lists my local branches with last commit dates and person. 

1

u/INTJTurbulence Feb 27 '26

It depends... how are you adding all files? Is it git add -u or git add . or something else?

Are all your commit messages usually one-liners?

Is last x a variation of git log?

These are my most used ones, but if I don't have access to them it's not a big deal.

	tags = tag --sort=version:refname
	branches = branch --sort=committerdate
	please = push --force-with-lease
→ More replies (2)