logo

如何安装Git

https://www.notion.so/jracademy/Git-124dd76b576d80f28948f4884640961b

什么是 Git

Git 是一个用于管理源代码的分布式版本控制系统。版本控制系统会在您修改文件时记录并保存更改,使您可以随时恢复以前的工作版本。
如果您的代码没有一个版本控制系统,您可能会遇到使用不同日期和名称 (例如,2024 年 12 月 2 日-code.php;2024 年 12 月 3 日-code.php) 来手动保存文件的多个版本。在处理数百个文件时,此方法既费时又不切实际。
事实:这种类型的版本控制以泪水告终。
事实:这种类型的版本控制以泪水告终。
 
它也不会对更改进行上下文说明,以便其他人知道更改了什么、何时更改以及由谁更改。当多个团队成员处理同一个文件时,覆盖很快就会成为一个问题。了解哪个文件是最新版本也变得很困难。因此我们转向版本控制系统来解决这些 (以及更多) 问题。
使用 Git,您可以轻松访问源代码的修改历史记录。您可以看到版本如何更改以及更改的人。因为整个 Git 历史都存储在共享存储库中,所以 Git 可以防止旧版本的无意覆盖。
版本控制系统之前与版本控制系统之后
版本控制系统之前与版本控制系统之后
简而言之,像 Git 这样的版本控制系统可以很容易地
  • 跟踪代码历史记录
  • 以团队形式协作编写代码
  • 查看谁做了哪些更改

Git 组件

Git 项目包含三个主要组件:
  • 存储库 Repository
  • 工作树 Work tree
  • 索引 Index
存储库是跟踪项目文件所有更改的“容器”。它保存着您团队所做的所有提交。您可以使用 git 日志命令访问提交历史记录。
工作树或工作目录由您正在处理的文件组成。您可以将工作树视为一个文件系统,您可以在其中查看和修改文件。
索引或暂存区是准备提交的地方。暂存后,工作树中的文件将与存储库中的文件进行比较。对工作树中文件的更改在提交之前被标记为已修改。
Git 项目的三个主要组件是存储库、索引和工作树。
Git 项目的三个主要组件是存储库、索引和工作树。

Git 文件的三种状态

正如您可能从 Git 工作流程中猜到的那样,文件可以处于以下三种状态之一:
  • 已修改
  • 已暂存
  • 已提交
修改文件时,您只会在工作树中看到这些更改。然后您必须暂存更改以将它们包含在您的下一次提交中。完成暂存所有文件后,您可以提交它们并添加一条消息来描述您所做的更改。然后您的更改将安全地记录在存储库中的新快照中。
Git 的三种文件状态是已修改、已暂存和已提交。
Git 的三种文件状态是已修改、已暂存和已提交。
 

Install Git on Windows

There are a couple of different ways to install Git on Windows. We recommend using an unofficial installer called msysGit.
To get started, head over to their website, download their installer and install it onto your computer.
notion image

Verify installation on Windows

To verify that Git has been installed, open the Command Prompt from Start > Windows System, or by pressing Windows + R then typing cmd and clicking OK.
notion image
Enter the following command:
git --version
If installed properly, you should see some output like this:
git version 2.15.1
You're now ready to add some global configuration for Git.

Install Git on Mac

Option 1 - Command Line Tools for Xcode

Open the Terminal app which can be found in Applications > Utilities. In the window that appears, enter the following command and press return:
git --version
If Git has already been installed, you'll see some output like this:
git version X.XX.XX
However, if it hasn't you'll be prompted to install the Command Line Tools.

Option 2 - Git Installer for Mac

Alternatively, there's an official Git installer for macOS.
Once it's finished downloading, open the installer, and follow the installation wizard using all the default options and preferences.

Verify installation on Mac

To verify that Git has been installed successfully, open up the Terminal app and enter the following command:
git --version
If installed properly, you should see some output like this:
git version X.XX.XX
You're now ready to add some global configuration for Git.

Install Git on Linux

Open up your preferred terminal emulator and run the following commands:
# Debian/Ubuntu sudo apt-get update sudo apt-get install git # Fedora sudo dnf install git

Verify installation on Linux

To verify that Git has been installed properly, run the following command:
git --version
If installed properly, you should see some output like this:
git version 2.15.1
You're now ready to add some global configuration for Git.

Global configuration for Git

Once you've verified that Git has been installed successfully, open the Terminal (Command Prompt if you're on Windows) and run the following two commands to configure Git to use your name and e-mail address:
git config --global user.name "Your Name" git config --global user.email "you@example.com"
This will create a hidden file called .gitconfig in your home directory which will contain contents similar to the following:
[user] name = Your Name email = you@example.com
This information is used by Git to populate the author information when you save your changes in the form of commits.
Both your name and e-mail address will be visible to anyone who has access to your repositories, so make sure you're using an e-mail address you don't mind sharing!
 

获取Remote Repo的 URL

在 GitHub 上找到仓库的远程 URL 非常简单。你只需进入项目的概览页面。
notion image
GitHub 项目概览
接下来,点击 “Clone or download” 按钮,会弹出一个小窗口
notion image
GitHub 克隆对话框
正如我们在前面的教程中解释的,使用 Git 时可以选择两种协议:HTTPSSSH
通过点击“Use SSH” 或 “Use HTTPS” 链接来切换你偏好的协议,然后点击 URL 旁边的小按钮将其复制到剪贴板。

克隆仓库

一旦你获得了仓库的 URL,打开命令行,导航到你想要存放代码的目录。我通常会将所有工作代码放在用户目录中的 Projects 文件夹下。
执行如下命令以创建一个本地仓库的克隆版本:
git clone /path/to/repository
如果是远端服务器上的仓库,你的命令会是这个样子:
git clone username@host:/path/to/repository
当你准备好克隆仓库时,输入以下命令,确保将 URL 替换为你从 GitHub 刚复制的那个:
git clone git@github.com:robertlyall/shop.git
如果操作成功,输出将类似于以下内容:
Cloning into 'shop'... remote: Enumerating objects: 27, done. remote: Counting objects: 100% (27/27), done. remote: Compressing objects: 100% (23/23), done. remote: Total 27 (delta 2), reused 27 (delta 2), pack-reused 0 Receiving objects: 100% (27/27), 44.39 KiB | 478.00 KiB/s, done. Resolving deltas: 100% (2/2), done.
默认情况下,Git 会创建一个与仓库名称相同的文件夹,不过你也可以通过在 git clone 命令中添加额外的参数来更改这个文件夹名称。
例如,如果你想将项目克隆到一个名为 shop-website 的文件夹中,可以输入以下命令:
git clone git@github.com:robertlyall/shop.git shop-website
 

Setup and Configuration

# Initialize a new Git repository git init # Clone and create a local copy of a remote repository git clone <url> # Configure global Git settings git config --global <setting_name> <value> # Configure local Git settings for a specific repo git config --local <setting_name> <value> # --------------- Advanced ------------------ # Show a summary of your Git configuration settings git config --list # Set a custom text editor for Git messages git config --global core.editor "<editor_command>" # Create a Git command alias git config --global alias.<shortcut> <command> # Enable automatic colorization of Git output git config --global color.ui auto # Cache Git credentials for a certain amount of time git config --global credential.helper 'cache --timeout=<seconds>' # Configure git to detect specific types of whitespace errors git config --global core.whitespace <options> # Automatically prune remote-tracking branches when fetching updates git config --global fetch.prune true # Set a custom diff tool for Git git config --global diff.tool <tool> # Set a custom merge tool for Git git config --global merge.tool <tool> # Compare changes using a custom diff tool git difftool # Resolve merge conflicts with a custom merge tool git mergetool
logo

Follow Us

linkedinfacebooktwitterinstagramweiboyoutubebilibilitiktokxigua

We Accept

/image/layout/pay-paypal.png/image/layout/pay-visa.png/image/layout/pay-master-card.png/image/layout/pay-airwallex.png/image/layout/pay-alipay.png

地址

Level 10b, 144 Edward Street, Brisbane CBD(Headquarter)
Level 2, 171 La Trobe St, Melbourne VIC 3000
四川省成都市武侯区桂溪街道天府大道中段500号D5东方希望天祥广场B座45A13号
Business Hub, 155 Waymouth St, Adelaide SA 5000

Disclaimer

footer-disclaimerfooter-disclaimer

JR Academy acknowledges Traditional Owners of Country throughout Australia and recognises the continuing connection to lands, waters and communities. We pay our respect to Aboriginal and Torres Strait Islander cultures; and to Elders past and present. Aboriginal and Torres Strait Islander peoples should be aware that this website may contain images or names of people who have since passed away.

匠人学院网站上的所有内容,包括课程材料、徽标和匠人学院网站上提供的信息,均受澳大利亚政府知识产权法的保护。严禁未经授权使用、销售、分发、复制或修改。违规行为可能会导致法律诉讼。通过访问我们的网站,您同意尊重我们的知识产权。 JR Academy Pty Ltd 保留所有权利,包括专利、商标和版权。任何侵权行为都将受到法律追究。查看用户协议

© 2017-2025 JR Academy Pty Ltd. All rights reserved.

ABN 26621887572