Shell script helper for git commits

13,322

Solution 1

You must quote the variable in your script.

#!/bin/bash -e
commit_message="$1"
git add . -A
git commit -m "$commit_message"
git push

I also set "-e" so that if there are any errors, the script will exit without processing subsequent commands.

As to your second question, the . in the script should refer to your current working directory, as you intend. However the -A is causing it to add all files that have been modiied in the repo.

Solution 2

You can create alias with argument. Something like:

[alias]
  cap = "!git add . && git commit -m '$1' && git push origin"

Solution 3

with and Alias I couldn`t put variables in the middle of the sentence, but you can create a function and put it on your .bashrc like this

commit(){
  git add --all . && git commit -m '$1' && git push origin master
}

Solution 4

Been there, done that: Git Flow.

You can also create aliases in the git configuration file too. This is much better than writing shell scripts since these will be extensions of the git command itself.

Also, don't forget:

$ git commit --all

which will commit all files you added or edited with your commit.

Share:
13,322
Snowman
Author by

Snowman

Updated on June 24, 2022

Comments

  • Snowman
    Snowman almost 2 years

    I'm trying to write a simple shell script that simplifies the git commit process.

    Instead of

    git add . -A
    git commit -m "message"
    git push
    

    I want to do commit.sh "my commit message"

    Here's what I have:

    #!/bin/bash
    commit_message="$1"
    git add . -A
    git commit -m $commit_message
    git push
    

    There's two problems with this:

    1. When the commit message includes spaces, like "my commit message", I get the following output:

      error: pathspec 'commit' did not match any file(s) known to git.

      error: pathspec 'message' did not match any file(s) known to git.

      So the only part of the commit message it uses is the "my" and the other parts "commit message" are left out.

    2. I think git add . references the location of the shell script, not the current project directory. How do I make it so that git add . references where I currently am in the terminal?

  • Marco
    Marco almost 11 years
    1) bash is not required for this script, sh would do the job just fine 2) bash is not necessarily found in /bin, it might be e.g. in /usr/local/bin
  • Emil Sit
    Emil Sit almost 11 years
    I was merely copying what the OP wrote; you're right of course and I would normally use /bin/sh myself.
  • Stephen Rauch
    Stephen Rauch over 6 years
    @iankit, how can you review this as No Action Needed? It is desperately in need of an edit.
  • Gilles Gouaillardet
    Gilles Gouaillardet over 6 years
    how does this improve the already accepted answer ? you are missing quotes around $comment. fwiw, if you comment=$* your script will be way easier to use (e.g. no need to quote the commit message when invoking commit.sh)
  • bitwiki
    bitwiki over 6 years
    Tks a lot I'll modify it in my project