练习 - 试用 Git
在创建第一个存储库之前,必须确保已安装并配置 Git。 Git 已随 Azure Cloud Shell 预安装,因此我们可以在右侧的 Cloud Shell 中使用 Git。
配置 Git
在 Cloud Shell 中,要再次确认已安装 Git,请键入
git --version
:git --version
提示
可以使用“复制”按钮将命令复制到剪贴板。 要粘贴,请右键单击 Cloud Shell 终端中的新行,然后选择“粘贴”,或使用 Shift+Insert 键盘快捷方式(在 macOS 上为 ⌘+V)。
应当会看到与以下示例类似的输出:
git version 2.7.4
要配置 Git,必须定义一些全局变量:
user.name
和user.email
。 这两者都是进行提交所必需的。在 Cloud Shell 中,用以下命令设置你的名称。 将
<USER_NAME>
替换为要使用的用户名。git config --global user.name "<USER_NAME>"
现在,使用此命令创建
user.email
配置变量,并将<USER_EMAIL>
替换为你的电子邮件地址:git config --global user.email "<USER_EMAIL>"
运行以下命令以检查更改是否成功:
git config --list
确认输出包含类似以下示例的两行。 你的用户名和电子邮件地址将与示例中显示的内容不同。
user.name=User Name user.email=user-name@contoso.com
设置 Git 存储库
Git 的工作方式是:检查特定文件夹内文件的更改。 我们会创建一个文件夹作为“工作树”(项目目录),并将其告知 Git,以便可以开始跟踪更改。 我们将 Git 存储库初始化到该文件夹中来告知 Git 开始跟踪更改。
首先为项目创建一个空文件夹,然后在该文件夹内初始化 Git 存储库。
创建名为“Cats”的文件夹。 此文件夹将为项目目录(也称为“工作树”)。 项目目录是存储所有与项目有关的文件的位置。 在此练习中,它是存储你的网站和创建该网站的文件及其内容的位置。
mkdir Cats
使用
cd
命令切换到项目目录:cd Cats
现在,初始化新存储库,并将默认分支的名称设置为
main
:如果你运行的是 Git 版本 2.28.0 或更高版本,请使用以下命令:
git init --initial-branch=main
或者使用以下命令:
git init -b main
对于 Git 的较早版本,请使用以下命令:
git init git checkout -b main
运行初始化命令后,应当会看到与以下示例类似的输出:
Initialized empty Git repository in /home/<user>/Cats/.git/ Switched to a new branch 'main'
现在使用
git status
命令以显示工作树的状态:git status
Git 用此输出进行响应,这表示
main
为当前分支。 (它也是唯一分支。)到目前为止,一切顺利。On branch main No commits yet nothing to commit (create/copy files and use "git add" to track)
使用
ls
命令以显示工作树的内容:ls -a
确认目录包含一个名为“.git”的子目录。 (将
-a
选项与ls
结合使用非常重要,因为 Linux 通常会隐藏以句点开头的文件和目录名称。)此文件夹为 Git 存储库 - Git 用于存储工作树的元数据和历史记录的目录。你通常无需直接对“.git”目录执行任何操作。 在工作树的状态发生更改时,Git 会更新元数据,以跟踪文件中的更改。 此目录无需你执行任何操作,但它对于 Git 非常重要。
从 Git 获取帮助
与大多数命令行工具一样,Git 具有内置的帮助功能,可用于查找命令和关键字。
键入以下命令,获取 Git 相关的操作帮助:
git --help
此命令显示以下输出:
usage: git [--version] [--help] [-C <path>] [-c name=value] [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path] [-p | --paginate | --no-pager] [--no-replace-objects] [--bare] [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>] <command> [<args>] These are common Git commands used in various situations: start a working area (see also: git help tutorial) clone Clone a repository into a new directory init Create an empty Git repository or reinitialize an existing one work on the current change (see also: git help everyday) add Add file contents to the index mv Move or rename a file, a directory, or a symlink reset Reset current HEAD to the specified state rm Remove files from the working tree and from the index examine the history and state (see also: git help revisions) bisect Use binary search to find the commit that introduced a bug grep Print lines matching a pattern log Show commit logs show Show various types of objects status Show the working tree status grow, mark and tweak your common history branch List, create, or delete branches checkout Switch branches or restore working tree files commit Record changes to the repository diff Show changes between commits, commit and working tree, etc merge Join two or more development histories together rebase Forward-port local commits to the updated upstream head tag Create, list, delete or verify a tag object signed with GPG collaborate (see also: git help workflows) fetch Download objects and refs from another repository pull Fetch from and integrate with another repository or a local branch push Update remote refs along with associated objects 'git help -a' and 'git help -g' list available subcommands and some concept guides. See 'git help <command>' or 'git help <concept>' to read about a specific subcommand or concept.
请通读适用于 Git 的各种选项,并注意,每个命令都有各自的帮助页面,可供你深入了解时使用。 并不是所有这些命令你都能看懂,但是如果你有使用 VCS 的经验,可能会对一些命令感到熟悉。
在下一课中,你将详细了解你刚试用过的命令以及 Git 的基本知识。