1

I'm entirely new to bash scripting, and I'm trying to write a script which adds, commits and pushes into the repository

commit_message="$1"
git add . -A
git commit -m "$commit_message"
git push

This adds all the edited/new files to my repo, is there a way to pass the required file names as arguments which executing this script? I got this script from google, but if there is any other way I can do it, please let me know.

4
  • 1
    Just leave the add part out, and run that part manually. git add <files> / commit_and_push "message goes here". I'm not sure that scripting this is very beneficial. Commented Mar 19, 2019 at 18:43
  • What is it you want to achieve? Save time? Commented Mar 19, 2019 at 19:07
  • You can run the script and pass the commit message as argument like ./script.sh "sample message" Commented Mar 19, 2019 at 19:14
  • @RomainValeri it is more inclined towards learning about bash with the things I do on regular basis. Commented Mar 19, 2019 at 20:53

1 Answer 1

4

I use a function for convenience. It works for my coding style, which for me means always working in a clean directory at the repo root and accessing all files with a relative path. YMMV.

qp() {
    [[ -z "$1" ]] && echo "Please enter a commit message:";
    typeset msg="$( [[ -n "$1" ]] && echo "$*" || echo $(head -1) )";
    date;
    git pull;
    git add .;
    git commit -m "$msg";
    git push;
    date
}

Call it like -

qp add a commit message

Note that it flattens all arguments into a single msg, and if it gets none it prompts for one.

$: qp
Please enter a commit message:
foo bar baz
Tue, Mar 19, 2019  3:25:24 PM
Already up-to-date.
On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working tree clean
Everything up-to-date
Tue, Mar 19, 2019  3:25:31 PM

What you asked for:

Rewrite it to take a file list and always ask for the message, like this:

qp() {
    echo "Please enter a commit message:";
    typeset msg="$( head -1 )";
    date;
    git pull;
    git add "$@";
    git commit -m "$msg";
    git push;
    date
}

You can just put the function code into a script with or without the function as you prefer.

  • NOTE: you might prefer cat to head -1 to allow multiple lines, but you will have to terminate your message with a CTRL_D or some such.

Then run it as

qp file1 file2 fileN

and it will ask for the commit message - OR, make the first argument the commit message, like this:

qp() {
    typeset msg="$1";
    shift;
    date;
    git pull;
    git add "$@";
    git commit -m "$msg";
    git push;
    date
}

Just make sure you "quote" that first commit message argument. ;)

qp "here's my commit message" file1 file2 fileN
Sign up to request clarification or add additional context in comments.

Comments

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.