How do I create an alias to run multiple commands with git?

Asked

Viewed 174 times

6

I wonder how to create an alias for git that runs the following commands:

  • git add (all modified files)
  • git commit (including a message)
  • git push

I tried the following gitconfig setting:

[alias]
   upl = !git add -A && git commit -m "$2" && git push origin master

I would like to use the alias passing a parameter to the message

git upl "cabecalho alterado"

However it gives error when committing, it seems that this way of passing the parameter of the message to alias is not correct

Error message received

cpd@INFORMATICA-01 MINGW64 /c/wamp/www/alura_git/curso_git (master)
$ git upl "teste"
error: switch `m' requires a value usage: git commit [<options>] [--] <pathspec>...
  • Which error is returned?

  • If you want to add all the changed files, can you change git add -A to git commit -am not? (That’s what I use, not the answer to the question but would improve your alias)

  • @Wilker error added

  • Bash? Power shell? CMD?

  • @jbueno am using bash in windows environment

  • so it seems the problem is in the && after the $2 (without the push after the commit works), I don’t know why, but you can change your $2 for ${1} that will work.

  • Try to change $2 for $1

  • @Pliavi tried to upl = !git add -A && git commit -m ${2} && git push origin master and returned: error: switch `m' requires a value usage: git commit [<options>] [--] <pathspec> ... is the same mistake I’ve been making

  • Using ${1} nay ${2}, as told by @Wilker, only $1 should also work.

  • 1

    This error message seems to be related to trying to pass an empty error message to the commit. The value -m is for specified commit message. The value $2 takes the second argument. In your case, you need to take the first one. That would be $1.

  • I’m on the street now, the phone is horrible. Otherwise I would try to help in a better way.

  • Just a correction up there. Message from commit empty* and not empty error message.

  • I tried to change $2 for $1, which indicates the commit worked, but it seems that there is a problem when synchronizing with the remote repository: $ git upl "teste0011"&#xA;[master c084230] teste0011&#xA; 1 file changed, 1 insertion(+), 1 deletion(-)&#xA;error: src refspec teste0011 does not match any.&#xA;error: failed to push some refs to 'https://github.com/[omitido]/curso_git.git'

  • maybe you have to pull and rebase before

  • Regarding the new bug. It seems to be a problem with Brach, it says it does not have the teste0011 branch. You have more than one branch in this repository?

  • I have more than one branch yes, but test0011 is not a branch, but the message that should be created for the commit

  • Have you tested this alias for a single branch? Is your local branch on the master branch? here and here refer to any branch related problem.

Show 12 more comments

2 answers

2


I was able to solve the problem using a Function and creating the alias by the console itself

git config --global alias.upl '!func(){ git add -A && git commit -m "$1" && git push -q; }; func'

0

The Error

cpd@INFORMATICA-01 MINGW64 /c/wamp/www/alura_git/curso_git (master) $
git upl "teste" error: switch `m' requires a value usage: git commit
[<options>] [--] <pathspec>...

is related to trying to pass a empty commit message for the commit. The parameter -m specifies the commit message. When passing the value $2 in the stretch

...commit -m"$2"...

the second argument is passed as a parameter for -m which in this case does not exist. In your case, you need to take the first argument of

upl "cabecalho alterado"

for this, you should use the $1, being like this

[alias]
   upl = !git add -A && git commit -m "$1" && git push origin master
  • I tested and returns the same error as your last comment on the question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.