如何在 Mac 上开始使用 Git

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

How to get started with Git on Mac

gitmacos

提问by johndoe

I installed Git on my Mac but I do not know how to run it or access it. From the terminal I type git but it says "command is invalid."

我在 Mac 上安装了 Git,但我不知道如何运行或访问它。我从终端输入 git 但它说“命令无效”。

I downloaded git from http://code.google.com/p/git-osx-installer/downloads/list?can=3and I downloaded the package "Git Installer 1.7.3.5 - OS X - Leopard - x86_64."

我从http://code.google.com/p/git-osx-installer/downloads/list?can=3下载了 git 并下载了包“Git Installer 1.7.3.5 - OS X - Leopard - x86_64”。

UPDATE 1:

更新1:

The content of the package is the following:

包裹内容如下:

  • README.txt
  • git-1.7.3.5-x86_64-leopard.pkg
  • setup git PATH for non-terminal programs.sh
  • uninstall.sh
  • 自述文件
  • git-1.7.3.5-x86_64-leopard.pkg
  • 为非终端程序设置 git PATH.sh
  • 卸载.sh

When I execute "setup git PATH for non-terminal programs.sh," I get the following messages:

当我执行“为非终端程序.sh 设置 git PATH”时,我收到以下消息:

No change to PATH in ~/.MacOSX/environment.plist
~ /Volumes/Git 1.7.3.5 x86_64 Leopard /Volumes/Git 1.7.3.5 x86_64 Leopard -MacBook-Pro:Git 1.7.3.5 x86_64 Leopard$ $PATH -bash: /usr/local/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/libexec: No such file or directory

~/.MacOSX/environment.plist 中的 PATH 没有变化
~ /Volumes/Git 1.7.3.5 x86_64 Leopard /Volumes/Git 1.7.3.5 x86_64 Leopard -MacBook-Pro:Git 1.7.3.5 x86_64 Leopard$ $PATH -bash: / usr/local/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/libexec: 没有那个文件或目录

UPDATE 2:

更新 2:

The content of my profile file is the following one:

我的配置文件的内容如下:

# System-wide .profile for sh(1)

if [ -x /usr/libexec/path_helper ]; then
    eval `/usr/libexec/path_helper -s`
fi

if [ "${BASH-no}" != "no" ]; then
    [ -r /etc/bashrc ] && . /etc/bashrc
fi

回答by Matthew Rankin

The git-osx-installer that you used should have installed git into /usr/local/git. See if you can cdinto that directory. If you can, then check that your PATHwas correctly set by running echo $PATHfrom the terminal and making sure that you see /usr/local/git/binin the included PATH. If not, you need to add it to your PATH.

您使用的 git-osx-installer 应该已将 git 安装到/usr/local/git. 看看你是否可以cd进入那个目录。如果可以,请PATH通过echo $PATH从终端运行并确保/usr/local/git/bin在包含的PATH. 如果没有,您需要将其添加到您的PATH.

Did you run the included shell script setup git PATH for non-terminal programs.sh?

您是否运行了包含的 shell 脚本setup git PATH for non-terminal programs.sh

Update 1: How to run the included shell script

更新 1:如何运行包含的 shell 脚本

  1. Mount the git-osx-installer disk image by double-clicking git-1.7.3.5-x86_64-leopard.dmg, which should be located in your Downloadsfolder.
  2. Open Terminal from /Applications/Utilities/Terminal
  3. Type cd /Volumes/Git 1.7.3.5 x86_64 Leopard/
  4. Type ./setup git PATH for non-terminal programs.shand hit Enter to run the shell script. Note: Once you type ./setupyou can hit the Tab key and it will autocomplete for you.
  5. Open a new Terminal and type echo $PATH
  6. Confirm that you see /usr/local/git/binin your PATH.
  1. 通过双击挂载 git-osx-installer 磁盘映像git-1.7.3.5-x86_64-leopard.dmg,它应该位于您的Downloads文件夹中。
  2. 从打开终端 /Applications/Utilities/Terminal
  3. 类型 cd /Volumes/Git 1.7.3.5 x86_64 Leopard/
  4. 键入./setup git PATH for non-terminal programs.sh并按 Enter 以运行 shell 脚本。注意:键入后,./setup您可以按 Tab 键,它会为您自动完成。
  5. 打开一个新终端并输入 echo $PATH
  6. 确认您/usr/local/git/bin在 PATH 中看到。

Update 2: Show Git Who's the Master

更新 2:向 Git 展示谁是大师

Open Terminal and issue the following commands:

打开终端并发出以下命令:

echo "/usr/local/git/bin" > git
sudo mv git /etc/paths.d

When you run sudo it will ask for your OS X password.

当您运行 sudo 时,它会询问您的 OS X 密码。

After issuing those two commands, you should be able to open a new Terminal window and see /usr/local/git/binwhen you run echo $PATH.

发出这两个命令后,您应该能够打开一个新的终端窗口并查看/usr/local/git/bin何时运行echo $PATH.

For this to work you have to have the following in /etc/profile, which it does by default:

为此,您必须在 中包含以下内容/etc/profile,默认情况下它会这样做:

if [ -x /usr/libexec/path_helper ]; then
    eval `/usr/libexec/path_helper -s`
fi

回答by miku

A general introduction:

一般介绍:

Git Immersion is a guided tour that walks through the fundamentals of Git, inspired by the premise that to know a thing is to do it.

Git Immersion 是一个带导游的导览,它通过了解一件事就是去做这件事的前提来介绍 Git 的基础知识。

回答by baton

Use MacPorts:

使用 MacPorts:

sudo port install git

回答by Sebastian Lasse

Either '/usr/local/bin' is not in your PATH or it just does not find git ...

'/usr/local/bin' 不在您的 PATH 中,或者它只是找不到 git ...

Try this

尝试这个

$ PATH='/usr/local/bin'
$ export PATH

RESTART CONSOLE

重启控制台

try again - if it still does not work

再试一次 - 如果它仍然不起作用

  1. check if the location /opt/local/bin/git or /opt/local/bin/github exists (depending on your version)
  1. 检查位置 /opt/local/bin/git 或 /opt/local/bin/github 是否存在(取决于您的版本)

if so:

如果是这样的话:

2 . type

2 . 类型

$ sudo ln -s /opt/local/bin/git /usr/bin/git

or

或者

$ sudo ln -s /opt/local/bin/git /usr/bin/github (depends on your version)

3 . should work now

3 . 现在应该工作

回答by vol7ron

It may be easier to create a symbolic link from one of your default paths:

从默认路径之一创建符号链接可能更容易:

sudo ln -s /usr/local/git/bin/git /usr/local/bin/git

回答by Scott Izu

Why not just run it directly? You can learn to configure it when you become more experienced.

为什么不直接运行呢?当您变得更有经验时,您可以学习配置它。

Check if you can run git to see the current version installed

检查是否可以运行 git 查看当前安装的版本

/usr/local/git/bin/git --version

Check if you can run git to see the help available

检查是否可以运行 git 以查看可用的帮助

/usr/local/git/bin/git --help

Later, after you learn how to use git, you can configure your path to automatically find git, by changing your path, using shell scripts, symbolic links. Any of these will then allow you to run:

稍后,在您学习如何使用 git 后,您可以通过更改您的路径、使用 shell 脚本、符号链接来配置您的路径以自动查找 git。然后,其中任何一个都将允许您运行:

git --version

git --help

When working with java or mvn or eclipse, the same goes. Try running the simplest command first to make sure you can actually run. Then, figure out how to simplify the process. If git was installed in another directory, search for it, then run it using the entire path.

使用 java 或 mvn 或 eclipse 时,也是如此。首先尝试运行最简单的命令以确保您可以实际运行。然后,找出如何简化流程。如果 git 安装在另一个目录中,请搜索它,然后使用整个路径运行它。

回答by user3729677

I think I know what you mean. I did the following to start using:

我想我知道你的意思。我做了以下开始使用:

Open your terminal and run the following command

打开终端并运行以下命令

cd /usr/local/git/bin

than try to run, for example:

比尝试运行,例如:

git --version

or

或者

git --help