0

I am trying, with a sh file, to commit my changes, enter my git credentials, and push the changes to the branch I'm working on. I can't get the credentials part to work

#!/bin/sh
cd pathtomyrepo
git add *.py
git commit -m "made changes"
git push origin mybranch
mygitusername
echo Press Enter...
mygitpassword
echo Press Enter... 

note: I had to run this chmod +x pathtomyshscript.sh beforehand to be able to run the sh file.

The error I get is:

error: insufficient permission for adding an object to repository database .git/objects fatal: failed to write commit object

Which I presume mean that my credentials were not passed to git. Any suggestion on how to do it? Is it even possible at all?

Julien Massardier
  • 1,326
  • 1
  • 11
  • 29
  • 1
    I see 2 major mistakes here. 1st, the error is not from push, it's from an earlier local command. To find out which on run `sh -x ./pathtomyshscript.sh` — shell will echo each command before executing. – phd Dec 15 '21 at 12:20
  • 1
    2nd, you cannot pass credentials like this from your shell. When `git push` asks for credentials it stucks, the shell script is not going on and your `echo` commands do not run. There are many different ways to properly pass credentials — credential manager, login/password in HTTPS URL, SSH keypair in SSH URL. – phd Dec 15 '21 at 12:22
  • thanks for the feedback, I modified the code to fix the 1st problem and will look into the solutions you suggest. – Julien Massardier Dec 15 '21 at 12:46

1 Answers1

0

Registering the SSH key may indirectly solve your issue. If you must use HTTPS with the script, then please use a password manager ! Copy-pasting your details inside a script is highly discouraged.

For macOS, you can use the Keychain to get and set your Git password:

GIT_PASSWORD=$(security find-generic-password -w -s '<Service Name>' -a '<account>')

Now, I must warn that blindly add'ing and commit'ing to a repo is not good practice!

Instead, you may look up git alias for shorthands and this thread for git credentials

Mihai-Cristian
  • 420
  • 2
  • 8