Search code examples
gitbashcommandexecute

Making a Git Bash "Shell" Script to execute already existing commands


i would like to make a "script" to perform some commands for me in git bash. i would then start git bash and type git myScriptName and hit enter, the it would perform:

    cd myProjectFolderName (ENTER)
    git init (ENTER)
    git add -A (ENTER)
    git commit -m 'letMeWriteSomeThingHereAndIHitEnterAndItEndsCommentWith' (ENTER)
    git push myRemoteName myBranch (ENTER)

and then do nothing, i would also like to do the same with:

    cd myProjectFolderName (ENTER)
    git init (ENTER)
    git pull myRemoteName myBranch (ENTER)

and then do nothing.

Thanks a bunch for any help regarding this, a plus would be if someone even went ahead and made the script :) Thanks


Solution

  • You could start with this:

    gitCommands.sh:

    function go_on {
      echo -ne "$1 [Y, n]\r"
      read answer
      if [ "$answer" = "n" ]; then
        echo "exit"
        exit 0
      fi
    }
    
    function call {
      go_on "$1"
      $1
      echo ""
    }
    
    call "cd myProjectFolderName"
    
    echo "Type commit message"
    read commit_message
    call "git commit -m $commit_message"
    

    The function call executes the function 'go_on'. This echos the string parameter (for example cd myProjectFolderName [Y, n]) on the commandline, after that it waits for your input. If you type Y or simply press enter, the script goes on (with executing this command). If you type "n", the script stops.