The shell helper script for git commits

I am trying to write a simple shell script that simplifies the process of fixing git.

Instead

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

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

Here is what I have:

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

There are two problems there:

  • When a commit message contains spaces, such as "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.

    Thus, the only part of the commit message that he uses is “mine,” and the rest of the “commit message” is not taken into account.

  • I think git add . refers to the location of the shell script, not the current project directory. How to make git add . links, where am I in the terminal now?

+4
source share
5 answers

You must specify the variable in the 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.

Regarding your second question,. The script should reference your current working directory as you plan. However, -A forces it to add all files that have been modified in the repo.

+14
source

You can create an alias with an argument . Sort of:

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

with and Alias ​​I cannot put variables in the middle of a 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 } 
+2
source

There, done, Git Stream .

You can also create aliases in the git configuration file. This is much better than writing shell scripts, as they will be extensions to the git command itself.

Also, do not forget:

 $ git commit --all 

which will transfer all files that you added or edited using commit.

+1
source

My decision, FYI

 #!/bin/sh comment=$1 git add ./* git commit -m $comment echo " commit finished,push to origin master ? " read commit case $commit in y|Y|YES|yes) git push ;; n|NO|N|no) exit 0 esac 

Using

 ./commit.sh your comment message ,type yes if you want to push to master 
0
source

Source: https://habr.com/ru/post/1494105/


All Articles