0

I tried to automate the task of adding, committing and pushing files to GitHub, by entering all commands every time, so I tried making a Shell Script for it It was working well, but then I tried to ensure that the repo exits at remote i.e. Github, using curl and workaround for finding that using This answer I found on stack overflow.

My code is as following

#!/bin/sh
# Shell Script to add all files, commit them with a commit message from user and then push them to remote GitHub repo
echo "*** Automating Git Add, Commit and Push ***"

#Ask for Username
echo "Enter your GitHub username: "
read username

#Ask for User Github's personal access token
echo "Enter your GitHub personal access token: "
read token

# Ask for repository name
echo "Enter repository name"
read repoName
# Check if repository exists at GitHub
curl https://api.github.com/repos/<username>/<repoName>.git
# If repository exits then

if  [ $? -eq 0 ];then
    cd $repoName
    # Display unstaged files
    git status
    # If there are any uncommited and unstatged files, ask user to commit them
    if [ "$(git status --porcelain)" ]; then
        echo "There are uncommited and unstatged files. Commit them before pushing"
        echo "Enter commit message"
        read commitMessage
        git add .
        git commit -m "$commitMessage"
        git push https://github.com/{username}/{repoName}.git
        echo "Files pushed to GitHub"
        # else push all commited and staged files to remote repo
    else
        git add .
        git commit -m "No changes"
        git push https://github.com/{username}/{repoName}.git
        echo "Files pushed to GitHub"
    fi
    # else display error message

    echo "There are no files to push"

else
    echo "Repository does not exist"
fi

# End of script


But it gives the following error

subhan@my-laptop:~$ ./gitPush.sh 
*** Automating Git Add, Commit and Push ***
Enter your GitHub username: 
SubhanRaj
Enter your GitHub personal access token: 
#Hiding my PAT
Enter repository name
Programming
./gitPush.sh: 17: cannot open username: No such file
Repository does not exist

On line 17 it gives erro and code exits.

  • Neither `` nor `{username}` will expand a shell variable -- I'm pretty sure you want `${username}`. Also, strings containing variable references should almost always be double-quoted ([shellcheck.net](https://www.shellcheck.net) is good at spotting common mistakes like this). Also also, `curl` will not return an error status if the repo is not found (since it will have successfully downloaded a "Not Found" message). – Gordon Davisson Feb 21 '22 at 05:36
  • I tried your suggestion, and also made some changes, by using Personal Access token to set remote origin as per [this](https://stackoverflow.com/questions/60757334/git-push-from-visual-studio-code-no-anonymous-write-access-authentication-fai) and it works now – Subhan Raj Feb 21 '22 at 06:16

1 Answers1

-1

I made all changes to the code, and now it works smoothly, I can now do all git workflow using a single command.

The link to the script is here

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 21 '22 at 09:06