0

I want to create an alias that is composed of 2 commands. Could not find anything online as most questions are about an alias that equals to 2 commands, eg:

alias command="command1 && command2"

What I want:

alias git push="git add . && git commit -m 'auto ups' && git push"

Why using this instead of naming my alias gp (or whatever 1 worded alias)

  1. I want to keep the same git push experience
  2. Want to learn about bash

Is that possible?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
NIGHTSCROLLER
  • 89
  • 1
  • 5
  • see: `PAGER='less +/^\ *aliases' man bash` – Jetchisel Nov 24 '22 at 05:43
  • 1
    Not with shell aliases, but with [Git aliases](https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases); though you can't overwrite an existing Git command, I believe. – Benjamin W. Nov 24 '22 at 06:20
  • What is a _git push experience_? Sounds like the name of a novel joy ride or entertainment park. BTW if you want a command to **unconditionally** run two commands in a row, it is `alias foo=command1;command2`. Your approach executes _command2_ only if _command1_ returns a exit-code 0. – user1934428 Nov 24 '22 at 07:14
  • 1
    `alias git push =` does not work, since an alias name can not contain spaces. Naming it `git_push` or `git.push` would work. – user1934428 Nov 24 '22 at 07:15
  • @user1934428 "git push experience" refers to not having to change the command to something else. Also, you are right for the && but in this context the point is simply to run whatever chain of command you want with a 2 word alias command.. – NIGHTSCROLLER Nov 24 '22 at 07:52
  • Then you can achieve this of course with the approach given by _VonC_ in his answer. I surely don't want to recommend redefining a standard command, because you always have to keep in mind, that your version of the command is behaving differently from what everyone expects. This could bite you someday.... – user1934428 Nov 24 '22 at 08:11
  • The way I'd put it is that you've given yourself a totally different "experience": the standard "experience" is that you add, then commit. At some other time, you push. You don't do all three at once, and when you get better at Git, you won't *want* to do all three at once. Still, you *can* do this sort of thing, it's just harder than using a bash or Git alias. – torek Nov 24 '22 at 12:28

1 Answers1

0

As illustrated here, you would need to define a git wrapper.
It should be set in your $PATH before the /usr/local/bin/git itself.
Or you can reference that script through an alias git=git-wrapper in your ~/.bash_profile.

In that wrapper, on push, you can then call a script which would chain any git command you need.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250