发布于 2026-01-06 1 阅读
0

面向初学者的 Git 使用实用指南 DEV 的全球展示与讲述挑战赛,由 Mux 呈现:展示你的项目!

面向初学者的 Git 使用实用指南

由 Mux 主办的 DEV 全球展示挑战赛:展示你的项目!

目录

迈克:我修改了源代码,结果应用程序崩溃了。现在,我没办法撤销之前的修改。

VT:你应该使用git。

Mike: Git?那是什么?

VT: Git 是一个版本控制系统,用于跟踪变更历史记录。当您对项目进行更改时,可以随时恢复到项目的任何早期版本。

迈克:听起来很棒!但是,我该如何使用它呢?

VT:好的!我这就给你演示。

步骤 1:安装 git

VT:首先,从http://git-scm.com下载 git并将其安装到您的计算机上。

迈克:好的!完成了!

步骤 2:配置 Git

VT:配置 Git 用户信息。这有助于将您的详细信息设置到所有提交中。

打开终端(或命令提示符)并运行以下命令:

$ git config --global user.name "[name]"
$ git config --global user.email "[email address]"
Enter fullscreen mode Exit fullscreen mode

请务必将[name]和分别替换[email address]为您实际的nameemail address

迈克:搞定了!

步骤 3:创建一个新的 Git 仓库

Mike:但是什么是git仓库呢?

VT: Git 仓库是.git/项目中的一个文件夹。该仓库会跟踪项目中所有文件的更改,并随着时间的推移构建更改历史记录。

迈克:但我没有.git/文件夹。

VT:首先,你需要初始化一个git仓库。

在空文件夹(或项目文件夹)中打开终端(或命令提示符),然后运行以下命令:

$ git init
Enter fullscreen mode Exit fullscreen mode

输出:

Initialized empty Git repository in /path/to/your/Project/.git/
Enter fullscreen mode Exit fullscreen mode

迈克:哦,文件夹我拿到了.git/。接下来呢?

如果您看不到该.git/文件夹​​,请启用该Show Hidden Files选项。通常,该选项通过 . 启用Ctrl+H

VT:就是这样。现在你拥有了一个 Git 仓库,你所做的所有更改都会被 Git 跟踪。

我们现在就做出一些改变。

第四步:做出改变

VT:在初始化 git 仓库的文件夹中创建一个新文件(或更改现有文件,如果有的话)。

VT:让我们创建一个 HTML 文件(index.html),或者从这里下载一个 HTML 文件。

.git/ 文件夹和 index.html 的屏幕截图

VT:现在,您可以运行此命令来查看 git 跟踪的更改。

$ git status
Enter fullscreen mode Exit fullscreen mode

输出:

On branch master

No commits yet

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

nothing added to commit but untracked files present (use "git add" to track)
Enter fullscreen mode Exit fullscreen mode

Mike:完成了!但是输出结果中提到的branch master“and”是什么意思?commits

VT:等等!我们稍后再谈分支。先来说说提交。

步骤 5:提交更改

VT:将记录文件快照永久提交到版本历史记录中。

让我们看看如何将更改提交到 git 仓库。

VT:首先,我们必须将更改添加到“暂存区”。

迈克:什么是“集结区”?

VT:暂存区就像一个草稿空间,我们可以在这里添加一个(或多个)文件版本,以便在下次提交时保存。

VT:让我们把index.html文件添加到“暂存区”。

运行以下命令:

$ git add index.html
Enter fullscreen mode Exit fullscreen mode

或者,我们可以通过运行以下命令,一次性将所有更改添加到暂存区:

$ git add .
Enter fullscreen mode Exit fullscreen mode

VT:现在,如果我们运行该命令git status,将会得到类似这样的输出:

On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
    new file:   index.html
Enter fullscreen mode Exit fullscreen mode

VT:我们可以通过运行以下命令来提交分阶段的更改:

$ git commit -m "[descriptive message]"
Enter fullscreen mode Exit fullscreen mode

[descriptive message]我们可以通过替换上述命令中的参数来添加一条消息来描述我们的更改。

输出:

[master (root-commit) db027ec] added index.html
 1 file changed, 13 insertions(+)
 create mode 100644 index.html
Enter fullscreen mode Exit fullscreen mode

VT:我们已成功提交更改。运行以下命令即可查看所有提交:

$ git log
Enter fullscreen mode Exit fullscreen mode

输出:

Author: Vishwas Tyagi <vishwast8126@gmail.com>
Date:   Mon Aug 15 19:06:57 2022 +0530

    added index.html
Enter fullscreen mode Exit fullscreen mode

迈克:太好了!但如果我不小心提交了呢?我该如何撤销提交?

撤销提交

VT:我们可以随时通过运行git reset命令撤销任何提交。

VT:让我们看看它的实际效果。

对文件进行更改index.html以进行另一次提交。

在文件中添加以下行index.html

<p><strong>Making a change.</strong></p>
Enter fullscreen mode Exit fullscreen mode

保存文件。

现在,运行该git status命令后,我们将得到以下输出:

On branch master
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:   index.html

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

VT:现在,让我们落实我们的改变。

运行以下命令:

$ git add index.html
$ git commit -m "updated index.html"
Enter fullscreen mode Exit fullscreen mode

VT:现在我们已经提交了所有更改。

假设我们上次提交时操作失误,现在想要撤销该次提交。

首先,我们需要找到要回滚到的提交的哈希 ID。

VT:运行git log命令列出所有提交。

commit 3d52d9625962f3147feb760ff7caccbaa2828b36 (HEAD -> master)
Author: Vishwas Tyagi <vishwast8126@gmail.com>
Date:   Mon Aug 15 21:24:29 2022 +0530

    updated index.html

commit db027ecc1d3bd09298db3b0305375047b1e31894
Author: Vishwas Tyagi <vishwast8126@gmail.com>
Date:   Mon Aug 15 19:06:57 2022 +0530

    added index.html
Enter fullscreen mode Exit fullscreen mode

VT:因为我们想要撤销最后一次提交,所以我们需要最后一次提交之前的提交的哈希 ID。

屏幕截图显示了提交哈希 ID

运行以下命令撤销上次提交:

$ git reset [commit_hash_id]
Enter fullscreen mode Exit fullscreen mode

替换[commit_hash_id]为要回滚到的提交的哈希 ID。

输出:

Unstaged changes after reset:
M   index.html
Enter fullscreen mode Exit fullscreen mode

VT:我们已经撤销了提交操作,所有更改都已发送回非暂存区。

迈克:哦,我明白了。

VT:如果您想放弃这些更改,可以运行以下命令:

$ git restore index.html
Enter fullscreen mode Exit fullscreen mode

迈克:太好了!我的index.html文件已经恢复到初始状态了。

迈克:但是你之前提到的“分支”具体指的是什么呢?

VT:我认为,现在是谈论“分支”的合适时机。

Git 分支

Git分支的可视化表示

VT:在 Git 中,分支是主仓库的一个新/独立版本。

假设我们有一个大型项目,需要开发一个新功能。

我们可以创建一个新分支(比如说feature),然后我们可以在该功能上进行开发,而不会影响主分支。

$ git branch [branch_name]
Enter fullscreen mode Exit fullscreen mode

如果我们对自己的工作感到满意,我们可以将我们的分支合并到主分支中。

如果不行,那么我们可以返回主分支并放弃在该分支上所做的更改feature

迈克:我明白了,但我想看看实际效果。

VT:好的,我们开始吧。

假设我们想在我们的……中添加一张图片index.html

image运行以下命令创建一个名为 `<branch_name>` 的新分支:

$ git branch image
Enter fullscreen mode Exit fullscreen mode

我们可以通过运行以下命令列出所有分支:

$ git branch
Enter fullscreen mode Exit fullscreen mode

输出:

  image
* master
Enter fullscreen mode Exit fullscreen mode

它列出了当前 Git 仓库中的所有分支。

这里,master是默认的主分支。

星号*表示我们当前正在处理的分支。

要切换分支,请运行以下命令:

$ git checkout image
Enter fullscreen mode Exit fullscreen mode

输出:

Switched to branch 'image'
Enter fullscreen mode Exit fullscreen mode

VT:现在,我们可以进行更改,这些更改只会影响当前分支(image)。它们不会影响主分支(master)。

在以下位置添加以下行index.html

<img src="https://images.unsplash.com/photo-1471897488648-5eae4ac6686b" height="300" width="200">
Enter fullscreen mode Exit fullscreen mode

提交更改:

$ git add .
$ git commit -m "added image in index.html"
Enter fullscreen mode Exit fullscreen mode

VT:我们的变更已经确定,但仅限于该image分支机构。它不会影响其他master分支机构。

我们可以通过切换到该分支来看到这一点master

$ git checkout master
Enter fullscreen mode Exit fullscreen mode

VT:如果我们index.html切换到该master分支后检查该文件,可以看到我们添加到文件中的图像index.html不在那里。

这是因为该图像被添加到了image分支中。

迈克:我该如何合并这两个分支?

合并分支

Git分支合并的可视化表示

VT:如果我们对在单独分支中所做的更改感到满意,我们可以master通过运行以下命令将该分支与主分支合并git merge [branch_name]

首先,切换到分支master

$ git checkout master
Enter fullscreen mode Exit fullscreen mode

然后运行以下命令将该分支合并image到目标分支master

$ git merge image
Enter fullscreen mode Exit fullscreen mode

输出:

Updating fa6e17e..c5a4da8
Fast-forward
 index.html | 1 +
 1 file changed, 1 insertion(+)
Enter fullscreen mode Exit fullscreen mode

删除分支

VT:如果我们不再需要某个分支并想将其删除,我们可以直接运行该git branch -d [branch-name]命令。

image要删除项目中的分支:

$ git branch -d image
Enter fullscreen mode Exit fullscreen mode

输出:

Deleted branch image (was c5a4da8).
Enter fullscreen mode Exit fullscreen mode

VT:就这些。

迈克:哦,这对我来说有点难,但多练习几次,我就能搞定。

延伸阅读……

概括

Git 是一个免费开源的分布式版本控制系统,旨在快速高效地处理从小到大的各种项目。

在本文中,您了解了git仓库、git提交、git分支以及版本控制的整个过程是如何工作的。

希望您喜欢这篇文章,并从中有所收获。如果您愿意,也可以在LinkedInTwitter上关注我。

谢谢,下次见!✌️

文章来源:https://dev.to/vishwastyagi/practical-guide-to-use-git-for-beginners-4ln4