如何使用脚本启动具有特定工作目录的 Git Bash 窗口?

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

How do I launch a Git Bash window with particular working directory using a script?

bashshellbatch-filegit-bash

提问by holocronweaver

How can I launch a new Git Bash window with a specified working directory using a script (either Bash or Windows batch)?

如何使用脚本(Bash 或 Windows 批处理)启动具有指定工作目录的新 Git Bash 窗口?

My goal is to launch multiple Git Bash windows from a single script, each set to a different working directory. This way I can quickly get to work after booting the computer instead of having to open Git Bash windows and navigating each one to the correct working directory.

我的目标是从单个脚本启动多个 Git Bash 窗口,每个窗口都设置为不同的工作目录。这样我就可以在启动计算机后快速开始工作,而不必打开 Git Bash 窗口并将每个窗口导航到正确的工作目录。

I am not asking how to change the default working directory, like this questiondoes, but to launch one or more terminal windows with different working directories from a script.

我不是问如何更改默认工作目录,就像这个问题一样,而是从脚本启动一个或多个具有不同工作目录的终端窗口。

采纳答案by holocronweaver

Git Bash uses cmd.exefor its terminal plus extentions from MSYS/MinGW which are provided by sh.exe, a sort of cmd.exewrapper. In Windows you launch a new terminal using the startcommand.

Git Bash 使用cmd.exe它的终端加上 MSYS/MinGW 的扩展,这些扩展由sh.exe,一种cmd.exe包装器提供。在 Windows 中,您可以使用该start命令启动一个新终端。

Thus a shell script which launches a new Git Bash terminal with a specific working directory is:

因此,启动具有特定工作目录的新 Git Bash 终端的 shell 脚本是:

(cd C:/path/to/dir1 && start sh --login) &
(cd D:/path/to/dir2 && start sh --login) &

An equivalent Windows batch script is:

等效的 Windows 批处理脚本是:

C:
cd \path\to\dir1
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login 
D:
cd \path\to\dir2
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login 

To get the same font and window size as the Git Bash launched from the start menu, it is easiest to copy the start menu shortcut settings to the command console defaults (to change defaults, open cmd.exe, left-click the upper left icon, and select Defaults).

要获得与从开始菜单启动的 Git Bash 相同的字体和窗口大小,最简单的方法是将开始菜单快捷方式设置复制到命令控制台默认值(要更改默认值,打开cmd.exe,左键单击左上角图标,然后选择默认值)。

回答by Drew Noakes

Another option is to create a shortcut with the following properties:

另一种选择是创建具有以下属性的快捷方式:

enter image description here

在此处输入图片说明

Targetshould be:

目标应该是:

"%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login

"%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login

Start inis the folder you wish your Git Bash prompt to launch into.

Start in是您希望 Git Bash 提示启动的文件夹。

回答by Mike Kowalski

Try the --cd= option. Assuming your GIT Bash resides in C:\Program Files\Git it would be:

试试 --cd= 选项。假设您的 GIT Bash 驻留在 C:\Program Files\Git 中,它将是:

"C:\Program Files\Git\git-bash.exe" --cd="e:\SomeFolder"

"C:\Program Files\Git\git-bash.exe" --cd="e:\SomeFolder"

If used inside registry key, folder parameter can be provided with %1:

如果在注册表项中使用,文件夹参数可以与 %1 一起提供:

"C:\Program Files\Git\git-bash.exe" --cd="%1"

"C:\Program Files\Git\git-bash.exe" --cd="%1"

回答by Franta

Let yet add up to the answer from @Drew Noakes:

让我们总结一下@Drew Noakes的答案:

Target:

目标:

"C:\Program Files\Git\git-bash.exe" --cd=C:\GitRepo

The cdparam should be one of the options how to specify the working directory.

cdPARAM应该是一个选项如何指定工作目录。

Also notice, that I have not any --loginparam there: Instead, I use another extra app, dedicated just for SSH keys: Pageant (PuTTY authentication agent).

另请注意,我在--login那里没有任何参数:相反,我使用了另一个额外的应用程序,专用于 SSH 密钥:Pageant (PuTTY authentication agent)

Start in:

开始于:

C:\GitRepo

The same possible way, as @Drew Noakes mentioned/shown here sooner, I use it too.

同样可能的方式,正如@Drew Noakes 在这里提到/展示的那样,我也使用它。

Shortcut key:

快捷键:

Ctrl + Alt + B

Such shortcuts are another less known feature in Windows. But there is a restriction: To let the shortcut take effect, it must be placed somewhere on the User's subdirectory: The Desktop is fine.

此类快捷方式是 Windows 中另一个鲜为人知的功能。但是有一个限制:要让快捷方式生效,它必须放在用户子目录的某个地方:桌面很好。

If you do not want it visible, yet still activatable, place this .lnkfile i.e. to the quick launch folder, as that dir is purposed for such shortcuts. (no matter whether displayed on the desktop) #76080#3619355

如果您不希望它可见,但仍可激活,请将此.lnk文件放置到快速启动文件夹中,因为该目录用于此类快捷方式。(无论是否显示在桌面上)#76080 #3619355

"\Application Data\Microsoft\Internet Explorer\Quick Launch\"

回答by monkrus

In addition, Win10 gives you an option to open git bash from your working directory by right-clicking on your folder and selecting GitBash here.

此外,Win10 为您提供了一个选项,可以通过右键单击您的文件夹并在此处选择 GitBash 从您的工作目录打开 git bash。

enter image description here

在此处输入图片说明

回答by lengxuehx

This is the command which can be executed directly in Run dialog box(shortcut is win+R) and also works well saved as a .batscript:

这是可以直接在Run dialog box(快捷键是win+ R)中执行的命令,也可以很好地保存为.bat脚本:

cmd /c (start /d "/path/to/dir" bash --login) && exit

回答by kayleeFrye_onDeck

Windows 10

视窗 10

This is basically @lengxuehx's answer, but updated for Win 10, and it assumes your bash installation is from Git Bash for Windows from git's official downloads.

这基本上是@lengxuehx 的答案,但已针对 Win 10 进行了更新,并且假定您的 bash 安装来自 Git官方下载的Git Bash for Windows 。

cmd /c (start /b "%cd%" "C:\Program Files\GitW\git-bash.exe") && exit

cmd /c (start /b "%cd%" "C:\Program Files\GitW\git-bash.exe") && exit

I ended up using this after I lost my context-menu items for Git Bash as my command to run from the registry settings. In case you're curious about that, I did this:

在我丢失了 Git Bash 的上下文菜单项作为从注册表设置运行的命令后,我最终使用了它。如果你对此感到好奇,我这样做了:

  1. Create a new key called Bashin the shellkey at HKEY_CLASSES_ROOT\Directory\Background\shell
  2. Add a string value to Icon(not a new key!) that is the full path to your git-bash.exe, including the git-bash.exe part. You might need to wrap this in quotes.
  3. Edit the default value of Bashto the text you want to use in the context menu enter image description here
  4. Add a sub-key to Bashcalled command
  5. Modify command's default value to cmd /c (start /b "%cd%" "C:\Program Files\GitW\git-bash.exe") && exitenter image description here
  1. 创建一个新的密钥Bash,在shell密钥中调用HKEY_CLASSES_ROOT\Directory\Background\shell
  2. Icon(不是新键!)添加一个字符串值,它是 git-bash.exe 的完整路径,包括 git-bash.exe 部分。您可能需要将其括在引号中。
  3. 将 的默认值编辑为Bash要在上下文菜单中使用的文本 在此处输入图片说明
  4. 添加子键到Bash被调用command
  5. command的默认值修改为cmd /c (start /b "%cd%" "C:\Program Files\GitW\git-bash.exe") && exit在此处输入图片说明

Then you should be able to close the registry and start using Git Bash from anywhere that's a real directory. For example, This PCis not a real directory.

然后您应该能够关闭注册表并从任何真实目录开始使用 Git Bash。例如,This PC不是真正的目录。

enter image description here

在此处输入图片说明

回答by Marcus Rickert

I'm not familiar with Git Bash but assuming that it is a git shell (such as git-sh) residing in /path/to/my/gitshelland your favorite terminal program is called `myterm' you can script the following:

我不熟悉 Git Bash,但假设它是一个 git shell(例如git-sh),/path/to/my/gitshell并且您最喜欢的终端程序称为“myterm”,您可以编写以下脚本:

(cd dir1; myterm -e /path/to/my/gitshell) &
(cd dir2; myterm -e /path/to/my/gitshell) &
...

Note that the parameter -efor execution may be named differently with your favorite terminal program.

请注意,-e执行参数可能与您喜欢的终端程序不同。

回答by Adauto Bronzatti

I might given a wrong direction, but, I like this trick.

我可能给出了错误的方向,但是,我喜欢这个技巧。

Using Windows Explorer, pointing to any directory you want, type "cmd" in the address bar it will open Windows command prompt in that directory.

使用 Windows 资源管理器,指向您想要的任何目录,在地址栏中键入“cmd”,它将在该目录中打开 Windows 命令提示符。

If you have the "Git" directory in your path, in the same way, you can type "git-bash" in the address bar and a Git Shell will open in that directory.

如果您的路径中有“Git”目录,以同样的方式,您可以在地址栏中键入“git-bash”,Git Shell 将在该目录中打开。