windows 如何从 DOS 命令行启动 Git Bash?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17302977/
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
How To Launch Git Bash from DOS Command Line?
提问by Eric Hepperle - CodeSlayer2010
I've got what I'm hoping is a simple question, but I haven't been able to find the answer yet. I would like to launch Git Bash from a DOS batch file. Here is what I tried so far:
我得到了我希望是一个简单的问题,但我还没有找到答案。我想从 DOS 批处理文件启动 Git Bash。这是我到目前为止尝试过的:
- Launched Git Bash from Win 7 Start button
- Used CTRL+ALT+DEL to identify the process as "sh.exe"
Launched sh.exe from batch file using start command
start sh.exe
- 从 Win 7 开始按钮启动 Git Bash
- 使用 CTRL+ALT+DEL 将进程标识为“sh.exe”
使用 start 命令从批处理文件启动 sh.exe
start sh.exe
However, this does not launch the full Git Bash environment. Git Bash usually has "MINGW32" in the title bar, but sh.exe has a full path to ... Git\bin\sh.exe
. It feels to me like there are some overlays or dependencies that I'm not aware of possibly, that also need to be loaded (pulled in? imported?).
但是,这不会启动完整的 Git Bash 环境。Git Bash 通常在标题栏中有“MINGW32”,但 sh.exe 有一个完整的路径到 ... Git\bin\sh.exe
。我觉得有一些我可能不知道的覆盖或依赖项,它们也需要加载(拉入?导入?)。
This was one of the top results I found through searching the web, but it doesn't make complete sense to me and I'm not sure if it applies exactly to my situation:
这是我通过搜索网络找到的最佳结果之一,但对我来说并不完全有意义,我不确定它是否完全适用于我的情况:
Running git from Windows Cmd line: Where are key files?
从 Windows Cmd 行运行 git:关键文件在哪里?
I'm a beginner in the world of DOS batch scripting.
我是 DOS 批处理脚本世界的初学者。
回答by Endoro
If you want to launch from a batch file:
如果要从批处理文件启动:
for x86
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login
for x64
start "" "%PROGRAMFILES%\Git\bin\sh.exe" --login
对于 x86
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login
对于 x64
start "" "%PROGRAMFILES%\Git\bin\sh.exe" --login
回答by Klas Mellbourn
I'm not sure exactly what you mean by "full Git Bash environment", but I get the nice prompt if I do
我不确定你所说的“完整的 Git Bash 环境”是什么意思,但如果我这样做了,我会得到很好的提示
"C:\Program Files\Git\bin\sh.exe" --login
In PowerShell
在 PowerShell 中
& 'C:\Program Files\Git\bin\sh.exe' --login
The --login
switchmakes the shell execute the login shell startup files.
该--login
开关使 shell 执行登录 shell启动文件。
回答by kujiy
I prefer to use git-bash.exe instead of sh.exe.
我更喜欢使用 git-bash.exe 而不是 sh.exe。
start "" "%ProgramFiles%\Git\git-bash.exe" -c "tail -f /c/Windows/win.ini"
You can stop closing the window when call /usr/bin/bash --login -i
in the end;
您可以在通话/usr/bin/bash --login -i
结束时停止关闭窗口;
start "" "%ProgramFiles%\Git\git-bash.exe" -c "echo 1 && echo 2 && /usr/bin/bash --login -i"
Note: I'm not sure this is a good way :)
注意:我不确定这是一个好方法:)
回答by old-monk
I prefer, putting git in environment variable and just calling
我更喜欢,将 git 放在环境变量中,然后调用
c:\Users\[myname]>sh
or
c:\Users\[myname]>bash
Steps to create Environment variable (Win7)
创建环境变量的步骤(Win7)
- From the desktop, right click the Computer icon.
- Choose Properties from the context menu.
- Click the Advanced system settings link.
- Click Environment Variables.
In the section User variables, hit button NEW, put variable name as
GIT_HOME
, value as (folder-where-you-installed-git).- for me it is was
c:\tools\git
, others maybe haveC:\Program Files\Git
- for me it is was
find the
PATH
environment variable and select it. Click Edit. (If the PATH environment variable does not exist, click New).- In the Edit window, add a new value
%GIT_HOME%
and%GIT_HOME%\bin
. Click OK. Close all remaining windows by clicking OK. - [Make sure you close the CMD which you want use for git]
- open newCommand prompt, and just type
sh
orbash
orgit-bash
- 在桌面上,右键单击计算机图标。
- 从上下文菜单中选择属性。
- 单击高级系统设置链接。
- 单击环境变量。
在用户变量部分,点击按钮 NEW,将变量名称设为
GIT_HOME
,值设为 (folder-where-you-installed-git)。- 对我来说是
c:\tools\git
,其他人可能有C:\Program Files\Git
- 对我来说是
找到
PATH
环境变量并选择它。单击编辑。(如果 PATH 环境变量不存在,请单击新建)。- 在编辑窗口中,添加一个新值
%GIT_HOME%
和%GIT_HOME%\bin
。单击确定。单击确定关闭所有剩余的窗口。 - [确保关闭要用于 git 的 CMD]
- 打开新的命令提示,只需键入
sh
或bash
或git-bash
回答by user3616334
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login -i
Git bash will get open.
Git bash 将打开。
回答by RuntimeException
https://stackoverflow.com/a/33368029/15789
https://stackoverflow.com/a/33368029/15789
I have posted an answer here.
我在这里发布了一个答案。
Open a Windows command window, and execute this script. If there is a change in your working directory, it will open a bash terminal in your working directory, and display the current git status. It keeps the bash window open, by calling exec bash.
打开 Windows 命令窗口,然后执行此脚本。如果您的工作目录发生变化,它将在您的工作目录中打开一个 bash 终端,并显示当前的 git 状态。它通过调用 exec bash 保持 bash 窗口打开。
If you have multiple projects you may create copies of this script with different project folder, and call it from a main batch script.
如果您有多个项目,您可以使用不同的项目文件夹创建此脚本的副本,并从主批处理脚本中调用它。
回答by dpate
I used the info above to help create a more permanent solution. The following will create the alias sh that you can use to open Git Bash:
我使用上面的信息来帮助创建一个更持久的解决方案。以下将创建可用于打开 Git Bash 的别名 sh:
echo @start "" "%PROGRAMFILES%\Git\bin\sh.exe" --login > %systemroot%\sh.bat
回答by ThaJay
The answer by Endoro has aged and I'm unable to comment;
Endoro 的回答已经陈旧,我无法发表评论;
# if you want to launch from a batch file or the command line:
start "" "%ProgramFiles%\Git\bin\sh.exe" --login