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.