git 修复 msysGit Portable $HOME 位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3454774/
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-19 04:29:08  来源:igfitidea点击:

Fix msysGit Portable $HOME location

gitsshmsysgitportable-applicationshome-directory

提问by dgw

I have successfully installed and configured msysGit Portable on my flash drive, and have used it to pull and push GitHub repos. However, I seem to always have to kludge the SSH support.

我已经在我的闪存驱动器上成功安装和配置了 msysGit Portable,并使用它来拉和推送 GitHub 存储库。但是,我似乎总是不得不对 SSH 支持进行混搭。

Specifically, in order for SSH to find my key files, I have to follow these instructionsto start a second instance of ssh-agentand then ssh-addmy key every time I run git-bash.bat.

具体来说,为了让 SSH 找到我的密钥文件,我必须按照这些说明在每次运行 git-bash.bat 时启动第二个实例,ssh-agent然后启动ssh-add我的密钥。

Using the output of ssh -v [email protected]to debug I see that msysGit defaults to my Windows user directory to look for keys. It can't do that; I need it to look in its own directory on the portable drive.

使用ssh -v [email protected]debug的输出,我看到 msysGit 默认在我的 Windows 用户目录中查找密钥。它不能那样做;我需要它在便携式驱动器上查看它自己的目录。

How can I force $HOME to be the program's own folder?

如何强制 $HOME 成为程序自己的文件夹?

Update for broken Vox link

更新损坏的 Vox 链接

Instructions from this pageare similar to the now-broken link I originally posted. Quoted below. Also here's the webarchive of original Vox article.

此页面的说明类似于我最初发布的现已断开的链接。引用如下。另外这里是原始 Vox 文章的网络档案

However, if you try this and get:

% ssh-add
Could not open a connection to your authentication agent. 

then your session is not running under the ssh-agent. You can get around this by restarting a new shell under the agent by running:

exec ssh-agent bash 

where you can replace bash with the shell of your choice. Once you do this, you should be able to run ssh-add to load your key for that shell.

但是,如果您尝试这样做并得到:

% ssh-add
Could not open a connection to your authentication agent. 

那么您的会话没有在 ssh-agent 下运行。您可以通过运行以下命令在代理下重新启动新 shell 来解决此问题:

exec ssh-agent bash 

您可以在其中用您选择的 shell 替换 bash。完成此操作后,您应该能够运行 ssh-add 来加载该 shell 的密钥。

采纳答案by VonC

The command used to launch git bash is:

用于启动 git bash 的命令是:

C:\Windows\SysWOW64\cmd.exe /c ""C:\Prog\Git.7.1\bin\sh.exe" --login -i"

I just tried the following in a DOS session:

我刚刚在 DOS 会话中尝试了以下操作:

C:\>C:\Windows\SysWOW64\cmd.exe /c ""C:\Prog\Git.7.1\bin\sh.exe" --login -i"
VonC@XXX /c/
$ echo $HOME
/c/Users/VonC

By default, $HOME$%HOMEPATH%, but if I force %HOME%:

默认情况下,$HOME$%HOMEPATH%,但如果我强制 %HOME%:

set HOME=/another/path

and then launch the same bash session:

然后启动相同的 bash 会话:

C:\>C:\Windows\SysWOW64\cmd.exe /c ""C:\Prog\Git.7.1\bin\sh.exe" --login -i"
VonC@XXX /c/
$ echo $HOME
/another/path

So if you wrap the bash call by a script setting the HOME to:

因此,如果您通过将 HOME 设置为以下脚本来包装 bash 调用:

  • %~dp0: the path of the wrapper on your USB key
  • or %~d1\your\path: with %~d1being the drive letter (of your usb key if your wrapper is on it)
  • %~dp0:USB 密钥上包装器的路径
  • %~d1\your\path%~d1作为驱动器号(如果您的包装器在其上,则为您的 USB 密钥)

, you should be able to force HOME to whatever value you need.

,您应该能够将 HOME 强制为您需要的任何值。



Note (November 2011): since then, the OP dgwhas written his own wrapper:

注意(2011 年 11 月):从那时起,OP dgw编写了自己的包装器

git-bash-portable.bat:

git-bash-portable.bat

@echo off
rem Copyright (C): 2010 Voyagerfan5761
rem http://technobabbl.es/

set USERPROFILE=%~dp0
set HOMEDRIVE=%~d0
set HOMEPATH=%~p0
set HOME=%~dp0
set HISTFILE=%USERPROFILE%.bash_history
rem set BASHRC=%USERPROFILE%.bashrc

git-bash.bat

The article "Portable Git for Windows: setting the $HOMEenvironment variable to allow complete portability (including SSL keys and configuration for use with GitHub)" also add useful information.

Windows 的可移植 Git:设置$HOME环境变量以允许完全可移植性(包括 SSL 密钥和用于 GitHub 的配置)”一文也添加了有用的信息。

However, if you install Git on a portable drive, you'll want your settings to travel with the installation—which obviously they won't if it is looking for them in a folder which may not exist on other computers.

So, what we need to do is tell Portable Git to treat a specific location within its own folder as the home folder; that way we can copy the whole Git folder anywhere we like and the settings will travel with it.

但是,如果您将 Git 安装在便携式驱动器上,您会希望您的设置随安装一起移动 - 如果它在其他计算机上可能不存在的文件夹中查找它们,显然他们不会这样做。

因此,我们需要做的是告诉 Portable Git 将其自己文件夹中的特定位置视为主文件夹;这样我们就可以将整个 Git 文件夹复制到我们喜欢的任何地方,并且设置将随之移动。

回答by Holger B?hnke

Setting the home dir

设置主目录

The solution with a git-bash-portable.batwrapper opens another Windows CMD window for me that stays in the background.

带有git-bash-portable.bat包装器的解决方案为我打开另一个留在后台的 Windows CMD 窗口。

Another, more native solution is to adjust /etc/profileand set the HOME var there. Just add the following lines to the end of /etc/profile, myuserbeeing your virtualusername:

另一个更原生的解决方案是在那里调整/etc/profile和设置 HOME 变量。只需将以下行添加到/etc/profile, myuserbee 您的虚拟用户名的末尾:

# end of /etc/profile
export HOME="/home/myuser"
cd

This sets the proper HOME directory and cds into it. Then the startup mechanism, like loading all files from /etc/profile.dworks correctly and you just start git-bash.exewith a doubleclick.

这会将正确的 HOME 目录和 cds 设置到其中。然后启动机制,比如从/etc/profile.d工作正常加载所有文件,你只需git-bash.exe双击开始。

Of course you have to create your home directory for this to work. Start git-bash and create it:

当然,您必须创建您的主目录才能使其工作。启动 git-bash 并创建它:

mkdir -p /home/myuser

Starting or reconnecting to the agent

启动或重新连接到代理

Regarding the agent, it usually has to be reloaded with every git-bash shell opened. A solution to get an independent agent spanning all git-bash windows is to include the following little script ~/.mgsshin the startup. It stores the agent env vars in a file agent.envin the .sshdirectory. Any new shell reads the file, checks if the agent is still running and connects to it. If it is not running it starts the agent and rewrites the agent.envfile. Make sure your .sshdir exists.

关于代理,通常必须在打开每个 git-bash shell 时重新加载它。获得跨越所有 git-bash 窗口的独立代理的解决方案是~/.mgssh在启动中包含以下小脚本。它将代理环境变量存储agent.env.ssh目录中的文件中。任何新 shell 都会读取文件,检查代理是否仍在运行并连接到它。如果它没有运行,它会启动代理并重写agent.env文件。确保您的.ssh目录存在。

# cat ~/.mgssh

agentfile=~/.ssh/agent.env

agent_load_env()
{
  test -f "$agentfile" && . "$agentfile" >| /dev/null;
}

agent_start()
{
  (umask 077; ssh-agent >| "$agentfile")
  . "$agentfile" >| /dev/null;
}

agent_load_env

# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)

if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
    agent_start
fi

# uncomment this, if you want to add a key on agent startup
#if [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
#    ssh-add
#fi

unset agentfile

Now source the .mgsshscript in your .bashrc:

现在.mgssh在你的脚本中获取脚本.bashrc

# cat .bashrc
. ~/.mgssh

# ... more .bashrc content

Found this on GitHub:

在 GitHub 上找到了这个:

https://help.github.com/articles/working-with-ssh-key-passphrases/#platform-windows

https://help.github.com/articles/working-with-ssh-key-passphrases/#platform-windows

Killing the agent before stick removal

在棒移除之前杀死代理

Usually, before you remove your usbstick you ask Windows to ejectthe stick, by right clicking it in the explorer or using the little systray icon. This will not work, if your agent is still up and running. Make sure to kill the agent before closing the last shell upon stick removal:

通常,在移除 U 盘之前,您会要求 Windows弹出U 盘,方法是在资源管理器中右键单击它或使用小系统托盘图标。如果您的代理仍在运行,这将不起作用。确保在移除棒时关闭最后一个外壳之前杀死代理:

$ ssh-agent -k

unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
echo Agent pid 8472 killed;

Remark: Usually you would use eval $(ssh-agent -k)to unset the env vars as well, but as you do this just before closing the shell it's irrelevant. The above startup script .mgsshtakes care of cleaning up the ~/.ssh/agent.envfile so that does not have to be done either.

备注:通常您也会使用eval $(ssh-agent -k)unset env vars,但是当您在关闭 shell 之前执行此操作时,它无关紧要。上面的启动脚本.mgssh负责清理~/.ssh/agent.env文件,因此也不必执行此操作。