Bash script to automate Git pull

30,057

Solution 1

Fortunately and according to the core Git documentation, you can use the following command to save your credentials for later on use - instead of typing it each time when prompted.

git config credential.helper store

Using this helper will store your passwords unencrypted on disk, protected only by filesystem permissions. This command stores credentials indefinitely on disk for use by future Git programs.

You probably don’t want to invoke this command directly; it is meant to be used as a credential helper by other parts of git

Interesting to check: Git Credentials

Solution 2

If you want to do Git pull from a shell script, you need to use it like this:

git pull https://username:password@git_hostname.com/my/repository

Solution 3

You can also generate ssh keys using ssh-keygen and use the ssh:// method to git pull.

SSH key allows you to establish a secure connection.

Before generating an SSH key, check if your system already has one by running cat ~/.ssh/id_rsa.pub. If you see a long string starting with ssh-rsa or ssh-dsa, you can skip the ssh-keygen step.

To generate a new SSH key just open your terminal and use code below. The ssh-keygen command prompts you for a location and filename to store the key pair and for a password. When prompted for the location and filename you can press enter to use the default. It is a best practice to use a password for an SSH key but it is not required and you can skip creating a password by pressing enter. Note that the password you choose here can't be altered or retrieved.

ssh-keygen -t rsa -C "$your_email"

Use the code below to show your public key.

cat ~/.ssh/id_rsa.pub

Copy-paste the key to your user profile. Please copy the complete key starting with ssh- and ending with your username and host.

Share:
30,057

Related videos on Youtube

Robert Dam
Author by

Robert Dam

Updated on October 01, 2020

Comments

  • Robert Dam
    Robert Dam over 3 years

    I want to automatically pull from Git. For some reasons it is not possible to automate the password thing in the server I am using. So it needs to be done by a Bash file. Now my Bash coding is not that good. Then it is possible to enter the passphrase for the SSH key. But I have really no clue what to do...

    #!/bin/bash cd . public_html/auto_deploy/foldername && git fetch --all && git checkout --force "origin/master"

    The above is not working... And then it needs to enter the passphrase, and I have no idea what to do then... How can I do this?

    • ggdx
      ggdx over 6 years
      Define "not working", could you print to stdout to see what bit is failing?
    • Robert Dam
      Robert Dam over 6 years
      It is prompting nothing, I would expect something to enter my password, so the bash script can enter the passphrase
    • Lasse V. Karlsen
      Lasse V. Karlsen over 6 years
      If you're going to embed your password into a script, why not simply ask git to persist the credentials and enter them once? Google for git credential.helper
  • Robert Dam
    Robert Dam over 6 years
    This is only for open repositories, this repository is private and needs to be pulled by the git@ url ssh
  • Robert Dam
    Robert Dam over 6 years
    And I still need to change the directory first.
  • Robert Dam
    Robert Dam over 6 years
    With this solution it is solved, I got managed to get it working with the above code.
  • luciano.bustos
    luciano.bustos over 3 years
    do you thing this work with personal access tokens??