简体   繁体   中英

Shell script helper for git commits

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?

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.

You can create alias with argument . Something like:

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

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
}

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.

My Solution ,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

usage

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

A while back I had similar idea and came with below file after doing some google searches for syntax.

Below script also adds a default message in case you do not care about commit message and reads current branch to push.

#!/bin/bash

# get the argument message
message="$1"

# If no commit message is passed, use current date time in the commit message
if [[ -z "${message// }" ]]
    then
        message=$(date '+%Y-%m-%d %H:%M:%S')
fi

# stage all changes
git add .
echo "====staged all git files"

# add commit
git commit -m "$message"
echo "====added the commit with message: '$message'"

# get current branch and push
current_branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
git push origin "$current_branch"
echo "====pushed changes to '$current_branch' branch"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM