Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script helper for git commits

Tags:

git

bash

shell

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?

like image 690
Snowman Avatar asked Jul 29 '13 17:07

Snowman


People also ask

What is $() in shell script?

Seems like ${variable} is the same as $variable , while $() is to execute a command.

How do I run a git script in bash?

Step 1: Go to Github repository and in code section copy the URL. Step 2: In the Command prompt, add the URL for your repository where your local repository will be pushed. Step 3: Push the changes in your local repository to GitHub. Here the files have been pushed to the master branch of your repository.

How do I commit in git bash?

To add a Git commit message to your commit, you will use the git commit command followed by the -m flag and then your message in quotes. Adding a Git commit message should look something like this: git commit -m “Add an anchor for the trial end sectionnn.”


1 Answers

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.

like image 166
Emil Sit Avatar answered Oct 24 '22 23:10

Emil Sit