0

I'm trying to create a script to do this:

git add "file"
git commit -m "Comment"

My idea is to run:

gac "file" "Comment"

I know I can do something similar but for all files, with:

echo 'alias gac="/path/to/gitaddcommit.sh"' >> ~/.bash_profile

And the .sh would be:

!/bin/bash
git add .
echo “Enter commit message: “
git commit -am “$commitMessage”

2 Answers 2

1

Well you need two things :

  1. A bin folder where you can put every sh script you want to use everywhere.
  2. More knowledge about shell scripting and how you can get argv (in your ex: 'file' 'Comment')

So first go to your /home/<username> then mkdir bin && cd bin && pwd then copy the pwd and add it into your PATH env variable inside your .bashrc

path example: PATH='/bin/:/sbin/:/home//bin

Then source ~/.bashrc you can now use every sh script inside you bin folder everywhere.

Cool so first problem done ! you don't have to do echo alias gac="/path/to/gitaddcommit.sh"' >> ~/.bash_profile anymore.

Now second problem here a post that can help you post And let me show you for your example :

cd ~/bin && vi gac.sh

Now the script :

#!/bin/sh
if [ "$#" -ne 2 ]; then
  echo "Usage: ./gac FILENAME COMMIT_MESSAGE" >&2
  exit 1
fi
git add "$1"
git commit -am "$2"

First we check the number or arg then git add and commit. Simple and fast maybe checking if arg one is a file might be a good idea too. PS: i'm going to re write my post ahah

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I already have the alias to my sh file (I have like 40 scripts that use daily, but none of them were mine), I'm trying create now the scripts that I said. Thanks, I will check your answer
1

Here's what I have in my .bashrc:

ga () 
{ 
    if test "$1" != "-f" && git rev-parse HEAD > /dev/null 2>&1 && ! git diff-index --quiet HEAD; then
        echo 'Repo is dirty.  -f to force' 1>&2;
        return 1;
    fi;
    git add "$@";
    list=$(git diff --name-only --cached | tr \\n \ );
    git commit -m "Add $list"
}

The commit message is autogenerated, but you could easily modify it to prompt the user or take it from somewhere else.

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.