0

This answer teaches how to do custom bash commands inside git aliases, and this article (One weird trick for powerful Git aliases) teaches it beautifully. However, it doesn't seem to work when I alias internal git commands. How can I replace internal git commands with custom scripts?

Ex: I have a custom python script: custom_script.py

print("hello")

In my project_name/.git/config file I add this alias:

[alias]
    statuss = "!f() { \
        python3 custom_script.py && git status; \
    }; f"

I then run git statuss and it works perfectly! I see "hello" printed out, followed by the "git status" return messages.

HOWEVER, if I rename my alias name from statuss to status, it no longer works.

[alias]
    status = "!f() { \
        python3 custom_script.py && git status; \
    }; f"

How can I do this so that by simply calling git status, it first calls my "custom_script.py" and then runs git status?

Notes:

  • Ideally, this would work in Windows, Mac, or Linux, but Linux is what I care about most, so a Linux-only solution would be ok.
  • Ideally, I'd like to do this via a git alias, but if I need to use a bash alias or other Linux/computer trick, that's better than nothing.
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • 2
    The short version is "you can't". The long version is "you can by cheating: make `git` be *your own* command, which looks at the command being used, and if it's one of yours, don't run `git foo`, run `git aliased-foo` which is your alias" or similar. (Pass other commands through so that you only have to do this for your aliases.) – torek Aug 31 '18 at 22:22
  • Explanation of @torek (one way to do so): Write a scipt `$HOME/bin/git` with a `case "$1" in ...` and put your $HOME/bin in front of your PATH. – Walter A Sep 01 '18 at 09:52
  • Related question I recently found since asking my question: https://stackoverflow.com/questions/5916565/define-git-alias-with-the-same-name-to-shadow-original-command – Gabriel Staples Sep 04 '18 at 17:52

2 Answers2

2

Git doesn't allow you to override internal commands in aliases, because doing so would break any scripting that uses those internal commands that expected them to function as normal. This is especially important because some Git commands are shell scripts, and overriding those commands could break Git's shell scripts.

You can override this by writing a shell script called git which you place into a directory in $PATH, and that is the easiest way to accomplish it. However, be aware that the first argument to git need not be a command: git takes a large number of options which precede the command, such as -c and -C, and your script would need to parse those in order to avoid breaking any other scripts that might call git (which might, for example, include your editor).

So while this is possible, it's very tricky, and any correct solution would be fairly lengthy (which is why I haven't attempted it here). Generally, the recommended solution is to use an alias which doesn't mirror a builtin name, which is much simpler.

However, if you want this only for interactive use, it's possible to create a script in your $PATH called, say, git-wrapper, and do something like this inside:

#!/bin/sh

if [ "$1" = status ]
then
    python3 custom_script.py
fi
exec git "$@"

You can then run alias git=git-wrapper in your ~/.bash_profile or ~/.zshrc (but not ~/.bashrc or ~/.zshenv). This will affect only cases where you specifically write git status, but not any scripting uses. This might be good enough for you, or not.

bk2204
  • 64,793
  • 6
  • 84
  • 100
0

You can use the following command: git config --global alias.co checkout and that's all.

Link: https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases

Diego Virgüez
  • 154
  • 2
  • 12