• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Solution / git alias tab completion for functions

git alias tab completion for functions

Last updated on February 6, 2022 by Sal Ferrarello

When creating a Git alias that points to a function, sometimes Git provides the wrong tab completion by default (e.g. filename completion instead of branch name completion). This is how we can tell Git, which type of completion to use.

Git Alias to a Function

Here is an example Git alias to a function that we could add to our .gitconfig

[alias]
echo = "!f() { \
  echo $1;     \
}; f"

by default hitting tab for completion options after git echo will give us a list of filenames

e.g.

$ git echo
CHANGELOG.md LICENSE README.md

but if we want a list of branches instead we can tell Git to use the same completion used for git branch (i.e. branch names) by adding the line

: git branch ; \

so we get

[alias]
echo = "!f() {   \
  : git branch ; \
  echo $1;       \
}; f"

Now our completion is branch names (and tags)

$ git echo
1.0.0                  develop
1.1.0                  main
FETCH_HEAD             origin/develop
HEAD                   origin/main
ORIG_HEAD

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit

Filed Under: Dev Tips, Programming, Solution Tagged With: Git, git alias

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in