Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 385
artu-hnrq Avatar asked Feb 20 '26 22:02

artu-hnrq


2 Answers

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

like image 147
artu-hnrq Avatar answered Feb 27 '26 09:02

artu-hnrq


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.

like image 26
try-catch-finally Avatar answered Feb 27 '26 08:02

try-catch-finally



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!