7

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?

KyleMit
  • 30,350
  • 66
  • 462
  • 664
artu-hnrq
  • 1,343
  • 1
  • 8
  • 30

2 Answers2

15

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

KyleMit
  • 30,350
  • 66
  • 462
  • 664
artu-hnrq
  • 1,343
  • 1
  • 8
  • 30
2

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.

try-catch-finally
  • 7,436
  • 6
  • 46
  • 67
  • 1
    Another approach to avoid extensive aliases in the config file is to include an alias file, or even several, in the config, e.g. `[include] \ path = mygitaliases.git; \ path = morealiases.git; ` – user151841 Aug 17 '22 at 03:13