I have written a script that clones a repo, creates a new branch, performs some changes, and pushes the changes. I am unsure what will happen in case of an error during branch creation. Because if there is some failure there is a possibility that the changes can get commit to the default branch. Here is an MRE of the script:
while read REPO
   do
      git clone {repo-url}
      cd $REPO
      if [ -d "$REPO-test" ]; then
        git checkout -b $BRANCH
          $command  // This command makes changes to some files in the repository. It actually creates a new file
          git add $file
          git commit ...
          git push ....
      fi
    done < $REPOS
I thought of using the exit code to test like:
if [ $? -eq 0 ] 
then 
  echo "Success" 
else 
  echo "Failure"
fi
So do this right after the git checkout -b $BRANCH and don't proceed further in case of error / the else clause.
I am not sure if that will make it error-proof or if there is a better way to achieve this in shell script or better commands to use in git
 
     
    