0

When I do

git push

I get the command prompt like

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

then I enter my username manually like

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

and then I hit Enter and I get prompt for my password

Password for 'https://myusername@github.com':

I want the username to be written automatically instead of manually having to type it all the time. i tried expect but could not use it

can anyone help me to write a shell script for it?plz:)

bk_32
  • 483
  • 1
  • 4
  • 17
Farzad
  • 21
  • 5
  • 6
    One alternative is to create `ssh` keys, where you only need to write a password in order to push ot github. Idk if this is a good alternative to what you want. – mnestorov Aug 29 '19 at 12:20
  • Did you look here: https://stackoverflow.com/questions/6565357/git-push-requires-username-and-password ? – ERemarque Aug 29 '19 at 12:20
  • 2
    @mnestorov That's more than an alternative; I'd say that's the *preferred* method of authentication. IMO, `expect` is a tool of last resort, when there are no other options. – chepner Aug 29 '19 at 13:10
  • Possible duplicate of [Is there a way to cache GitHub credentials for pushing commits?](https://stackoverflow.com/questions/5343068/is-there-a-way-to-cache-github-credentials-for-pushing-commits) – phd Aug 29 '19 at 14:49
  • https://stackoverflow.com/search?q=%5Bgit%5D+skip+password – phd Aug 29 '19 at 14:50

1 Answers1

2

Well here is a very small script that would do what you want

#!/bin/expect
spawn git push origin master
expect "Username for 'https://github.com': "
send "MY_NAME_IN_GITHUB"
interact

This way you just have to type your password. You can bind this script to some git command and it can take in extra parameters so that it changes your branches.

But I would advise you to create yourself ssh keys and use them. Here is a great tutorial how to do that :) ssh_tutorial and connect GH to ssh

mnestorov
  • 4,116
  • 2
  • 14
  • 24
  • I hope it did what you wanted :) you can accept the answer if it did the job – mnestorov Aug 30 '19 at 08:20
  • could you be more specific meaning about how to set properly expect in the machine? – v2lrf Aug 06 '21 at 09:03
  • @v2lrf Are you referring to the `expect` command? – mnestorov Aug 06 '21 at 09:11
  • Yes, I mean that your answer suppose to be aware of how to make running expect and your solution is efficient, so now the problem is no longer the script but more how to use and call properly ```bin/expect``` from a shell script and deal with errors like ```command not found: spawn``` or ```command not found: send``` or ```command not found: interact``` if any tricks you have to help on that could be very chic ;-) – v2lrf Aug 06 '21 at 13:50
  • @v2lrf the `expect` together with the rest of the commands are part of the expect package. You just have to install it with your package manager. – mnestorov Aug 06 '21 at 19:51