How to input password to git pull command? - linux

I have written scripts for Windows and Linux to essentially set up a new users workspace with all the git repositories from our server.
I would like the user to enter the password for our server once, store it in a local variable, pass that variable to each git pull command, then erase the password variable and exit.
How can I input the password when the git pull command requests it? Both for Windows batch file and a Linux shell script.
Here is code from the Linux script:
#!/bin/bash
echo "Enter password: "
read pswd
clear #No screen peaking
#This is repeated for each repo
location=folderName
mkdir $location
cd $location
git init
git remote add origin git#<server>:$location.git
git pull origin master
#Above prompts for password & is where I want to automatically input $pswd
I've tried various things recommended on SO and elsewhere, such as piping, reading from .txt file, etc. I would prefer to not need anything more than plain old windows cmd and Linux terminal commands. And as this script is just for set up purposes, I do not need to securely store the password permanently with something like ssh agent.
I'm running Windows 7 and Ubuntu 12.10, but this script is meant for setting up new users, so it should ideally work on most distributions.

Synopsis:
git pull "https://<username>:<password>#github.com/<github_account>/<repository_name>.git" <branch_name>
Example:
git pull "https://admin:12345#github.com/Jet/myProject.git" master
Note: This works for me on a bash script

I would really recommend to not try and manage that password step, and delegate that (both on Linux and Windows) to git credential helper.
See:
"Git http - securely remember credentials"
"How to use git with gnome-keyring integration"
The user will enter the password only once per session.

Read the remote url from git and then insert the ID and password (PW) to the url might work.
For example try the following:
cd ${REPOSITORY_DIR}
origin=$(git remote get-url origin)
origin_with_pass=${origin/"//"/"//${USER_ID}:${USER_PW}#"}
git pull ${origin_with_pass} master

Related

Git not storing credentials when sshed into server

I have a ubuntu VM which I SSH into from a windows machine to develop on. I have my windows pub key on the linux VM so I can ssh without password.
On the linux machine I have set to store my git credentials. When I do any git command on the VM directly, it is able to use the stored credentials and carries out the given task. However, if I ssh into the VM from windows, and try to do any git command, it never stores the credentials and each time I have to reenter the password.
What is causing this and is there a way to fix it?
Compare the output of git config --show-scope --show-origin credential.helper when:
you are logged in directly on the Linux server
you are logged in through SSH from Windows
This assumes that, in both instances, you are using an HTTPS URL from your Linux server when using git clone/push/pull to a remote server.
If the Git on Linux is not recent enough, use simply git config --global credential.helper.

Git pull/clone with username and password in AWS Code Commit

I Need to do a git pull using https url as a oneline command . this command I need to integrate in a bash script . But all the time it is asking the usernmae and password .
The repository is in AWS codecommit
Try this:
git clone https://username:password#git-codecommit.us-east-1.amazonaws.com/v1../repos../..
This way worked for me for CodeCommit (AWS) repository
Check this link: Enter user password in command
As is described perfectly in that post, you basically have three options:
Store the password in .netrc file (with 600 permissions). Make sure you clone the repo specifying the username in the url.
Clone the repo with https://user:pass#domain/repo . Take into account that your password will be visible in several places...
Use the credential helper.
As an update, AWS has released their remote git remote codecommit. With proper IAM setup, you can do oneline pulls without even passing username and passwords. This is now the recommended method by AWS. It can be setup on your local or on a container that's running in an AWS Pipeline for example.
i.e. git clone codecommit://HelloWorldRepo myLocalHelloWorldRepo
And then you can git pull as normal.
Full documentation is here:
https://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-git-remote-codecommit.html

Git requires username and password for git push, git pull using HTTPS method

So, when I use HTTPS method for cloning git repository and do some changes and push or pull to git it will always be prompting me for username and password. what's the solution for that?
Apart from changing to SSH, you can also keep using HTTPS, if you don't mind to put your password in clear text. Put this in your ~/.netrc and it won't ask for your username/password (at least on Linux and Mac)
Make one .netrc file in your home directory.
machine github.com
login <user>
password <password
The solution to your problem is this git command which is kind of remember me of GIT.
git config credential.helper store
Read this for details:
GIT credentials store

How do I use the right key for SSH, connecting from Git Bash on Windows to remote server?

I'm trying to push my project on git to my remote Linux server from my local Windows PC.
I have no issues connecting to my server through SSH when I use PuTTY. But PuTTY is only for the remote server, and doesn't let me look in my own local pc - so I can't access my git repository to push from.
Instead I've been trying with Git Bash, where I go to the repository(master) of my git project, and then attempt "git push production master". Receiving "Permission denied (publickey)" in response in Git Bash.
I have loaded by private key from my user/.ssh folder on local pc, using PuTTYgen, copy/pasted the public key it shows into my Github account's SSH-keys.
This is the same key that I use for connecting with PuTTY(which works).
If I use "git remote -v" in Git Bash, I receive what is the correct url for the server:
"
production ssh://notacop#mafiauniversedata.com/var/repo/site.git (fetch)
production ssh://notacop#mafiauniversedata.com/var/repo/site.git (push)
"
"notacop" is the admin user I made instead of root, which is also the one that I use in PuTTY.
If I attempt "ssh notacop#mafiauniversedata.com" in Git Bash I also receive the "Permission denied (publickey)" response. So seemingly the issue is related to the publickey that Git Bash uses, as it can't connect at all.
My user/.ssh folder contains:
digitalocean_private_key (is the one PuTTY uses/the one generated by PuTTYgen) + digitalocean_public_key
github_rsa + github_rsa.pub
id_rsa + id_rsa.pub
known_hosts
I feel like I've set things up in accordance with the general guidelines, and it does work in PuTTY, so the issue must be related to Git Bash and how Git Bash establishes ssh connection.
But I'm fairly clueless about how to fix the issue, and haven't been able to find a solution from searching. My guess is that it's something fairly simple, but I've been stuck on this for a couple weeks now, not getting anywhere because of it.
Any answers that can lead me in the right direction will be highly appreciated!
If anything about my question is unclear, or you need more information to answer, please let me know.
I don't know why Git Bash's own ssh doesn't seem to work for you,
but I have a better recommendation: make Git Bash use PuTTY, by setting the GIT_SSH environment variable to the path of plink.exe (that's not a typo, it's not putty.exe, but another executable that comes with the zip of PuTTY tools).
The main advantage and reason to do this is so that Git Bash can benefit from pagent.exe, the key manager of PuTTY.
So that you can enter your passphrase once,
when adding your private key to Pagent,
and then you don't need to re-enter it every time you perform remote operations in Git Bash.

how to append a string after a command?

Each time I run a command like this
ssh user#myhost.com
I've to type in the ssh password.
Each time I run a command like this
git push origin master
(pushing code to github)
I've to type in the github password.
Is there any bash thing to write these password inline with the command?
I mean something like
git push origin master < 'mypass'
or
git push origin master | 'mypass'
Use public key authentication for git hub.
For ssh-ing to the host add your key to the host's authorized_keys file(default location is the /home/<user>/.ssh directory of the user you are logging in as).
If the daemon is configured correctly it will simple use your key to authenticate before it prompts for a password.

Resources