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

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.

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.
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 非常简单。你只需进入项目的概览页面。

GitHub 项目概览
接下来,点击 “Clone or download” 按钮,会弹出一个小窗口
。

GitHub 克隆对话框
正如我们在前面的教程中解释的,使用 Git 时可以选择两种协议:HTTPS 和 SSH。
通过点击“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