1

I have this function to make make it simpeler for me to push to git. It works fine with pushing without arguments, however when I pass a parameter to the function I get -bash: parameter: command not found. For as far as I know, I am calling the function correctly.

function gitPush {
  if ($1)
    then
    if ($2)
     then
     git push $2 $1
    else
     git push origin $1
    fi
  else
    git push origin master
  fi
}

When I call it using gitPush it works fine, however gitPush branch does not work.

I would like to know what I am doing wrong and how to fix it.

I don't know if it affects the execution of the function (expect not), but I am using Putty

2 Answers 2

4

($1) isn't doing what you think it does - it is attempting to execute the command stored in the first argument, in a subshell.

To test for the existence of an argument, you can use the following:

if [[ -n $1 ]]

Or to simply count the arguments, you can use the following:

if [[ $# -eq 1 ]]
Sign up to request clarification or add additional context in comments.

Comments

3

I guess you might do this quite a lot shorter with:

function gitPush {
    local remote=${2:-origin}
    local branch=${1:-master}
    git push "${remote}" "${branch}"
}

But all-in-all this is probably not something you would want to use anyway.

in your .gitconfig you could add:

[push]
default = tracking

That way wou will always push to the tracked remote of the current branch if you do not supply arguments.

3 Comments

Thank you for showing me the shorter way of writing my function. Though it is not the answer to the question what I was doing wrong I think I will use this option to rewrite the function I have. I'll also take a look into my .gitconfig file
@Jelmergu: well, you should not have tagged this "git" in the first place as it is a basic shell scripting error; git had nothing to do with that. ;-) But since you did, I thought I'd answer anyway .;-)
aha, I thought that since I was using a function of git it should have been tagged anyway. Either way I am happy that I did tag it with git. I now have my answer and a better way to do what I wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.