Linux git 命令实战示例

LinuxLinuxBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

在本实验中,你将通过实际示例学习如何使用 Linux 的 git 命令。你将从一个新的 Git 仓库初始化开始,然后向仓库中添加并提交文件。最后,你将探索 Git 分支管理。本实验涵盖了 Git 的基本命令和概念,为你提供了在开发项目中管理版本控制的实践经验。

实验将引导你完成设置 Git 仓库、配置用户信息以及执行基本的 Git 操作(如添加和提交文件)的过程。这些基本技能对于在代码项目中有效协作和跟踪变更至关重要。

Linux 命令速查表


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") subgraph Lab Skills linux/ls -.-> lab-422701{{"`Linux git 命令实战示例`"}} linux/cd -.-> lab-422701{{"`Linux git 命令实战示例`"}} linux/mkdir -.-> lab-422701{{"`Linux git 命令实战示例`"}} end

初始化 Git 仓库

在这一步骤中,你将学习如何初始化一个新的 Git 仓库并设置本地开发环境。

首先,导航到你想要创建 Git 仓库的项目目录:

cd ~/project

接下来,使用 git init 命令初始化一个新的 Git 仓库:

git init

示例输出:

Initialized empty Git repository in /home/labex/project/.git/

git init 命令会在你的项目文件夹中创建一个新的 .git 目录,Git 会将所有版本控制信息存储在此目录中。

接下来,让我们配置你的 Git 用户名和电子邮件地址。这些设置将与你在该仓库中进行的任何提交相关联:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

这将为系统上的所有 Git 仓库全局设置你的用户名和电子邮件。如果需要,你也可以在仓库级别设置这些值。

现在,你的 Git 仓库已经初始化并配置完成。在下一步中,你将学习如何向仓库中添加并提交文件。

向 Git 仓库添加并提交文件

在这一步骤中,你将学习如何向 Git 仓库添加文件并提交更改。

首先,在项目目录中创建一个新文件:

echo "This is a test file." > test.txt

现在,检查 Git 仓库的状态:

git status

示例输出:

On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test.txt

nothing added to commit but untracked files present (use "git add" to track)

如你所见,test.txt 文件被列为未跟踪文件。要将其添加到 Git 仓库,请使用 git add 命令:

git add test.txt

现在,再次检查状态:

git status

示例输出:

On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   test.txt

文件现在已暂存并准备好提交。要提交更改,请使用 git commit 命令:

git commit -m "Add test.txt file"

示例输出:

[master (root-commit) 1234567] Add test.txt file
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

-m 标志允许你提供提交信息,这是描述你所做更改的良好实践。

现在,你的文件已成功添加并提交到 Git 仓库。

探索 Git 分支管理

在这一步骤中,你将学习如何使用 Git 分支,这对于管理项目的不同版本至关重要。

首先,检查当前分支:

git branch

示例输出:

* master

如你所见,你当前位于 master 分支。

现在,创建一个名为 feature/new-page 的新分支:

git checkout -b feature/new-page

示例输出:

Switched to a new branch 'feature/new-page'

git checkout -b 命令会创建一个新分支并切换到该分支。

让我们在新分支中对 test.txt 文件进行一些更改:

echo "Adding a new line to the test file." >> test.txt

现在,检查状态:

git status

示例输出:

On branch feature/new-page
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   test.txt

no changes added to commit (use "git add" and/or "git commit -a")

更改是在 feature/new-page 分支中进行的,但尚未提交。

让我们提交这些更改:

git add test.txt
git commit -m "Add new line to test.txt"

示例输出:

[feature/new-page 7890abc] Add new line to test.txt
 1 file changed, 1 insertion(+)

现在,切换回 master 分支:

git checkout master

示例输出:

Switched to branch 'master'

你可以看到,在 feature/new-page 分支中所做的更改并未出现在 master 分支中。

这是使用 Git 分支的基本工作流程。你可以根据需要创建、切换和合并分支,以管理项目的不同版本。

总结

在本实验中,你学习了如何初始化一个新的 Git 仓库、配置 Git 用户名和电子邮件、向仓库添加文件并提交更改。你首先导航到项目目录,并使用 git init 命令创建了一个新的 Git 仓库。接着,你使用 git config 命令配置了 Git 用户名和电子邮件。然后,你创建了一个新文件 test.txt,并使用 git add 命令将其暂存以提交。最后,你使用 git status 命令检查了仓库的状态,并了解了哪些更改已准备好提交。

Linux 命令速查表

您可能感兴趣的其他 Linux 教程