I'm using git aliases from zsh plugins: https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/git

So it has git aliases like:

gst # git status
ga # git add
gc "commit" # git commit -v "commit"

...and I'm also using git bare repo to backup all my dotfiles: https://github.com/Anthonyive/dotfiles/blob/0706bc81daa3aeb7899b506cd89d4ab78fc7b176/USAGE.md

In particular, the git bare repo technique aliases the git command to dotfiles:

alias dotfiles='git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'
alias d='dotfiles'

So then how do I map the all the git alias command to d? Like:

dst # similar to gst, but uses the dotfiles alias
da # similar to ga
dc "commit" # similar to gc "commit"

Mapping them one by one seems very tedious...

2 Answers 2


The associative array aliases contains all alias definitions.

for name in "${(@k)aliases}"; do
  if [[ $name == g* && $aliases[$name] == 'git '* ]]; then
    alias d${name#g}="dotfiles ${aliases[$name]#git }"

Alternatively, you could change the d alias to a function that expects a following git command, but first expands shell aliases and removes any leading git.

alias d='d ' # expand aliases after d
function d {
  if [[ $1 == "git" ]]; then shift; fi
  dotfiles "$@"

Then d gst will run dotfiles status, d gc myfile will run dotfiles commit myfile, d ls-tree will run dotfiles ls-tree, etc. Completion is doable but not easily.

  • Thank you. That works perfectly! Btw, you have a little typo where you missed a '{' in front of alias in the if statement. May 24, 2021 at 22:00

To replace all calls to git with git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME, you can use a function:

git() {
  command git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME "$@"

However, you don’t actually have to do that for this case. Instead of passing those option flags to git, you can just specify them through environment variables instead:

export GIT_DIR=$HOME/.dotfiles/

See https://www.git-scm.com/docs/git#Documentation/git.txt---git-dirltpathgt

As an aside, instead of maintaining and trying to remember a large amount of aliases, you could instead just use zsh-autocomplete’s history completion mode. (Disclaimer: I’m the maintainer of this plugin.)

enter image description here

  • You seem to be the developer of zsh-autocomplete. If that's the case, please disclose that when advertising it.
    – muru
    May 23, 2021 at 17:50
  • Fair enough. Added. May 23, 2021 at 18:06
  • 1
    If I understand correctly, this overwrites all my git commands? What I want is to map all dotfiles to git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME, then alias commands like dotfiles status to dst. However, I want to keep git as is. May 23, 2021 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.