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?
– Wilker
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)
– leofontes
@Wilker error added
– Adriano Luz
Bash? Power shell? CMD?
– Jéf Bueno
@jbueno am using bash in windows environment
– Adriano Luz
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.– Pliavi
Try to change $2 for $1
– Wilker
@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– Adriano Luz
Using
${1}
nay${2}
, as told by @Wilker, only$1
should also work.– Pliavi
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.
– Wilker
I’m on the street now, the phone is horrible. Otherwise I would try to help in a better way.
– Wilker
Just a correction up there. Message from commit empty* and not empty error message.
– Wilker
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"
[master c084230] teste0011
 1 file changed, 1 insertion(+), 1 deletion(-)
error: src refspec teste0011 does not match any.
error: failed to push some refs to 'https://github.com/[omitido]/curso_git.git'
– Adriano Luz
maybe you have to pull and rebase before
– ldeoliveira
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?
– Wilker
I have more than one branch yes, but test0011 is not a branch, but the message that should be created for the commit
– Adriano Luz
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.
– Wilker