Windows 上的命令行 Git

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

Command-line Git on Windows

windowsgitcommand-line

提问by Ayusman

I have installed msysGit 1.7.10 on my Windows 7 machine. What I need to know is if I can still use Git from command line?

我已经在我的 Windows 7 机器上安装了 msysGit 1.7.10。我需要知道的是我是否仍然可以从命令行使用 Git?

When I try the gitcommand in the command line right now I see:

当我git现在在命令行中尝试该命令时,我看到:

'git' is not recognized as an internal or external command, operable program or batch file.

'git' 不是内部或外部命令,也不是可运行的程序或批处理文件。

Is there any way I can enable the Git command line?

有什么办法可以启用 Git 命令行吗?

采纳答案by Samy Dindane

As @birryree said, add msysgit's binary to your PATH, or use Git Bash (installed with msysgit as far as I remember) which is better than Windows' console and similar to the Unix one.

正如@birryree 所说,将 msysgit 的二进制文件添加到您的 PATH 中,或者使用 Git Bash(据我记得是与 msysgit 一起安装的),它比 Windows 的控制台更好,并且类似于 Unix 控制台。

回答by

I had the same issue and resolved it by adding the /bindirectory location to the PATH Environment Variable.

我遇到了同样的问题,并通过将/bin目录位置添加到 PATH 环境变量来解决它。

  1. Search for the file location where Git was installed, mine is C:\Users\(My UserName)\AppData\Local\GitHub. It may also be C:\Program Files (x86)\Git

  2. Once you have the location of Git you should see a /binsub-folder. It may be in a PortableGit folder (mine is PortableGit_015aa71ef18c047ce8509ffb2f9e4bb0e3e73f13). Copy this path.

  3. Go to Control Panel > System > System Protection > Advanced > Environment Variables

  4. Choose PATH, click edit and paste the bin path there. If there are already any values in your PATH paste your Git path at the end separated with a semi-colon.

  1. 搜索安装 Git 的文件位置,我的是C:\Users\(My UserName)\AppData\Local\GitHub. 也可能是C:\Program Files (x86)\Git

  2. 获得 Git 的位置后,您应该会看到一个/bin子文件夹。它可能在 PortableGit 文件夹中(我的是 PortableGit_015aa71ef18c047ce8509ffb2f9e4bb0e3e73f13)。复制此路径。

  3. 转到控制面板 > 系统 > 系统保护 > 高级 > 环境变量

  4. 选择 PATH,单击编辑并将 bin 路径粘贴到那里。如果您的 PATH 中已经有任何值,请在末尾粘贴您的 Git 路径,并用分号分隔。

Now you can access Git command from CMD.

现在您可以从 CMD 访问 Git 命令。

回答by James Lawruk

These instructions worked for a Windows 8 with a msysgit/TortoiseGit installation, but should be applicable for other types of git installations on Windows.

这些说明适用于安装了 msysgit/TortoiseGit 的 Windows 8,但应该适用于 Windows 上的其他类型的 git 安装。

  • Go to Control Panel\System and Security\System
  • Click on Advanced System Settingson the left which opens System Properties.
  • Click on the Advanced Tab
  • Click on the Environment Variablesbutton at the bottom of the dialog box.
  • Edit the System Variablecalled PATH.
  • Append these two paths to the list of existing paths already present in the system variable. The tricky part was two paths were required. These paths may vary for your PC. ;C:\msysgit\bin\;C:\msysgit\mingw\bin\
  • Close the CMD prompt window if it is open already. CMD needs to restart to get the updated Path variable.
  • Try typing gitin the command line, you should see a list of the git commands scroll down the screen.
  • 进入控制面板\系统和安全\系统
  • 单击左侧的高级系统设置,打开系统属性。
  • 单击高级选项卡
  • 单击对话框底部的环境变量按钮。
  • 编辑名为PATH系统变量
  • 将这两个路径附加到系统变量中已经存在的现有路径列表中。棘手的部分是需要两条路径。这些路径可能因您的 PC 而异。;C:\msysgit\bin\;C:\msysgit\mingw\bin\
  • 如果 CMD 提示窗口已经打开,请关闭它。CMD 需要重新启动以获取更新的 Path 变量。
  • 尝试在命令行中输入git,您应该会看到向下滚动屏幕的 git 命令列表。

回答by Error

In windows 8.1, setting the PATH Environment Variable to Git's bindirectory didn't work for me. Instead, I had to use the cmddirectory C:\Program Files (x86)\Git\cmd.

在 windows 8.1 中,将 PATH 环境变量设置为 Git 的bin目录对我不起作用。相反,我不得不使用cmd目录C:\Program Files (x86)\Git\cmd.

Credit to @VonC in this question

在这个问题中归功于@VonC

回答by Alex Bowyer

For me, I'm using Windows 10, @andrew-marshall's instructions worked (Thanks!) except that git.exewas within a cmddirectory within PortableGit..., not bin, so I had to put \cmdon the end of the path I added to PATH. Thought I would post this here in case anyone else hits the same issue. You can tell it works once gitin a new Command Prompt window returns command usage info and not an error.

对我来说,我使用的是Windows 10,@安德鲁-马歇尔的指导工作(谢谢!)除了git.exe是内cmd内的目录PortableGit...,而不是bin,所以我只好把\cmd对我添加到路径的末尾PATH。我想我会在这里发布这个以防其他人遇到同样的问题。您可以告诉它git在新的命令提示符窗口中工作一次,返回命令使用信息而不是错误。

回答by zinc

In the latest version (v2.19 for Windows when I'm writing), if you choose the option "Use git in Windows command prompt" (or sth similar, please read the options carefully when you install git), you should be able to use git commands in windows command prompt or windows powershell without any additional setting. Just remember to restart the command line tool after you install git.

在最新版本(我写的时候是v2.19 for Windows),如果你选择“在Windows命令提示符下使用git”选项(或类似的,请在安装git时仔细阅读选项),你应该可以在 windows 命令提示符或 windows powershell 中使用 git 命令,无需任何额外设置。安装git后记得重启命令行工具。

回答by Venkat

If you have installed GitHubDesktop in Windows 10, then press Ctrl + '. or in the menu go to Repository>Open in command prompt.

如果您已在 Windows 10 中安装了 GitHubDesktop,请按 Ctrl + '。或在菜单中转到 Repository>Open in command prompt。

In case git is not installed in your machine, you should get a prompt to install git.(I came to know from this that GitHubDesktop and git are different applications). Install git, close your command prompt and open it again.

如果你的机器上没有安装 git,你应该得到安装 git 的提示。(我由此知道 GitHubDesktop 和 git 是不同的应用程序)。安装 git,关闭命令提示符并再次打开它。

You can test your installation by typing in git at the command prompt.

您可以通过在命令提示符下输入 git 来测试您的安装。

回答by david

as of now, with win 1909 and using Git-2.24.0.2-64-bit installer the process is very friendly towards user configuration. Seems like the earlier problems may now be catered for.

截至目前,使用 win 1909 并使用 Git-2.24.0.2-64 位安装程序,该过程对用户配置非常友好。似乎现在可以解决早期的问题。

回答by Rico

You can install chocolatey. It's like apt-get in Linux, you can install using the command line. Run Command Prompt as Administrator and type choco install gitand you'll be able to install git devoted to the command line.

您可以安装巧克力。就像Linux中的apt-get,你可以使用命令行安装。以管理员身份运行命令提示符并键入choco install git,您将能够安装专用于命令行的 git。