Bash 自动化脚本 Git 拉取

Bash script to automate Git pull

我想从Git自动拉取。由于某些原因,无法在我使用的服务器中自动执行密码操作。所以需要通过一个Bash文件来完成。现在我的 Bash 编码不太好。然后可以输入 SSH 密钥的密码。但是我真的不知道该怎么办...

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

上面的不行...然后需要输入密码,我不知道该怎么办...我该怎么办?

如果您想从 shell 脚本中执行 Git 拉取操作,您需要像这样使用它:

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

幸运的是,根据核心 Git documentation,您可以使用以下命令保存您的凭据以备后用 - 而不是每次出现提示时都输入它。

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

有趣的检查:Git Credentials

您还可以使用 ssh-keygen 生成 ssh 密钥,并使用 ssh:// 方法 git pull

SSH 密钥允许您建立安全连接。

在生成 SSH 密钥之前,请通过 运行 cat ~/.ssh/id_rsa.pub 检查您的系统是否已经有一个。如果您看到以 ssh-rsassh-dsa 开头的长字符串,您可以跳过 ssh-keygen 步骤。

要生成新的 SSH 密钥,只需打开您的终端并使用下面的代码。 ssh-keygen 命令提示您输入存储密钥对的位置和文件名以及密码。当提示输入位置和文件名时,您可以按 enter 键使用默认值。最好为 SSH 密钥使用密码,但这不是必需的,您可以通过按 enter 跳过创建密码。请注意,您在此处选择的密码无法更改或找回。

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

使用下面的代码显示您的 public 密钥。

cat ~/.ssh/id_rsa.pub

将密钥复制并粘贴到您的用户配置文件中。请复制以 ssh- 开头并以您的用户名和主机结尾的完整密钥。