用于自动化 Git 拉取的 Bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47339559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:36:20  来源:igfitidea点击:

Bash script to automate Git pull

gitbashgit-bash

提问by Robert Dam

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...

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

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

#!/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?

上面的不起作用...然后需要输入密码,我不知道该怎么做...我该怎么做?

回答by M03

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 文档,您可以使用以下命令保存您的凭据以备后用 - 而不是每次出现提示时都输入它。

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 程序使用。

你可能不想直接调用这个命令;它旨在被 git 的其他部分用作凭证助手

Interesting to check: Git Credentials

有趣的检查:Git 凭证

回答by RR80

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

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

SSH key allows you to establish a secure connection.

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

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-rsaor ssh-dsa, you can skip the ssh-keygenstep.

在生成 SSH 密钥之前,通过运行cat ~/.ssh/id_rsa.pub. 如果您看到以ssh-rsa或开头的长字符串ssh-dsa,则可以跳过该ssh-keygen步骤。

To generate a new SSH key just open your terminal and use code below. The ssh-keygencommand 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 密钥,只需打开您的终端并使用下面的代码。该ssh-keygen命令会提示您输入用于存储密钥对的位置和文件名以及密码。当提示输入位置和文件名时,您可以按 Enter 以使用默认值。最佳做法是为 SSH 密钥使用密码,但这不是必需的,您可以按 Enter 跳过创建密码。请注意,您在此处选择的密码无法更改或检索。

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.

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

回答by Naga Raju

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

如果你想从 shell 脚本中执行 Git pull,你需要像这样使用它:

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