artu-hnrq
artu-hnrq

Reputation: 1487

Is possible to write a multi-line alias in .gitconfig?

I know that is possible to use && (and) statement to go running multiple commands for a same alias. However for long combinations it loses in readability. For example:

save = !git status && git add -A && git commit -m \"$1\" && git push --force && git log && :

Is there a multi-line way to write it?

Maybe wrapping it with {} for example?

Upvotes: 11

Views: 3313

Answers (2)

artu-hnrq
artu-hnrq

Reputation: 1487

You can use a line escape (\) to break lines like this:

[alias]
   save = !git status \
        && git add -A \
        && git commit -m \"$1\" \
        && git push -f \
        && git log -1 \
        && : # Used to distinguish last command from arguments

You can also put multiple statements inside a function like this:

[alias]
   save = "!f() { \
           git status; \
           git add -A; \
           git commit -m "$1"; \
           git push -f; \
           git log -1;  \
        }; \ 
        f;  \
        unset f" 

See Also: Git Alias - Multiple Commands and Parameters

Upvotes: 19

try-catch-finally
try-catch-finally

Reputation: 7624

I'd refrain from writing such extensive aliases in the config file. You can also add new commands by adding an executable file named git-newcommand to your PATH. This could be a Bash script, Python script or even a binary as long as it's executable and named with the prefix "git-".

In case of scripts you've to add the proper Hashbang:

#!/usr/bin/env python

Export the PATH, for example in your home:

export PATH="${PATH}:${HOME}/bin"

This is more modular, portable and easier debuggable.

Upvotes: 2

Related Questions