4

I'm trying to make a bash function to automatically add everything untracked or modified in my working tree, commit it, and push.

This is what it looks like:

gcp() {
    git add -- .;
    git commit -m \"$1\";
    git push origin $2;
}

However, when I test it out, I get:

$ gcp "test this" master
error: pathspec 'this"' did not match any file(s) known to git.

How do I get the quoting around the variables to behave correctly?

1 Answers1

6

You should not escape the quotes. Also I suggest quoting all arguments. Maybe your branch has a space in it, who knows? And you only need the semicolons if you put more statements in a single line.

gcp() {
    git add -- .
    git commit -m "$1"
    git push origin "$2"
}
petersohn
  • 11,292
  • 13
  • 61
  • 98
  • Branch names actually can't contain spaces (or most other shell metacharacters) as far as I know but quoting is still the safe bet. – Etan Reisner Jul 19 '15 at 21:12
  • Is it good practice to terminate lines with `;` or is it generally considered unnecessary? I mean, clearly, it's not needed, but, for instance, JavaScript doesn't always need `;` at the end of each line, but linters like JSHint recommend you always put them. –  Jul 19 '15 at 21:55
  • 1
    @ArielDavis Semicolons are unnecessary - see this SO post: http://stackoverflow.com/a/7507068/1574942. Also, here's a vaguely interesting online shell linter (which doesn't flag the semicolons either way): http://www.shellcheck.net/ – Mike D Jul 20 '15 at 02:16