3

I have a Push command, that runs this script:

FindGits |
{
    while read gitFolder; do
        parent=$(dirname $gitFolder);
        if [[ `git -C $parent status --porcelain` ]]; then
            echo;
            (
                echo $parent;
                git -C $parent status --porcelain
                git -C $parent add .
                git -C $parent commit -m "committed by script" 1>/dev/null 2>/dev/null
                if [[ $? == 128 ]]; then
                    git -C $parent commit -m "commited by script" 1>/dev/null 2>/dev/null
                    git -C $parent push
                    if [[ $? == 1 ]]; then
                        git -C $parent pull
                        git -C $parent push
                    fi
                else 
                    git -C $parent push
                    if [[ $? == 1 ]]; then
                        git -C $parent pull
                        git -C $parent push
                    fi
                fi
            ) &
        fi
    done
    wait
}

However, when I run it, git asks for username:

Username for 'https://github.com':

Here are the tests I have done, to make sure git has access to GitHub:

  • ssh -T [email protected] => successful
  • sudo -i + ssh -T [email protected] => successful
  • I go into a git repository, and I change something, and I manaully add + commit + push => successful
  • I go to my home, and add + commit + push from another repository using -C another_git_repo_path => successfull

Then why it does not work when I run it from inside a script?

2
  • Do all your git repositories use the ssh method? For example, all of them have in .git/config something like: url = [email protected]:.... instead of url = https://github.com/... Commented Feb 5, 2023 at 5:43
  • 1
    @EdgarMagallon, that was exactly the point. Thank you. Please send your comment as an answer so that I can accept it. Commented Feb 5, 2023 at 9:53

1 Answer 1

1

If you are using ssh authentication for working with git repositories (and make pull,push, etc.) you have to config every repository to use ssh authentication instead of https, otherwise the git command will keep asking for your user and password.

For changing the repository authentication to ssh you can use something like:

  1. git remote set-url origin [email protected]:UserName/Repo.git
  2. Or in .git/config file you can change he line url = https://github.com/ to url = [email protected]:....:
$> cat .git/config

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = [email protected]:UserName/Repo.git # edited line
        fetch = +refs/heads/*:refs/remotes/origin/*

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.