你应该知道的基本 Git 命令

2025-06-09

你应该知道的基本 Git 命令

这篇文章最初发表于johnraptis.dev

Git 和 Github 在每个开发者的工作流程中都扮演着至关重要的角色。它们为我们提供了一种非常复杂的方式来追踪项目历史。了解如何使用它们可以让我们更高效地工作。

本文的目的更多是提醒或概述,并将所有内容归纳到一处,而不是提供全面的指南。我认为官方文档在这方面做得很好。

先决条件

  • 在您的系统中安装 Git。

  • 拥有 Github 帐户并了解创建存储库的基础知识。

但首先请快速参考一下。

那么 Git 是什么?(快速)

Git 是一个跟踪代码更改的系统。你经常会听到它被称为“版本控制”。为什么?
因为它保存了源代码所有先前版本的历史记录。这使得多个开发人员能够并行工作并共享代码。为了实现这一点,我们需要一个本地 Git 仓库(位于我们的计算机中)和一个远程 Git 仓库(位于服务器(Github、Gitlab 等)中)。

git 配置

为了在本地工作,我们必须设置用户名和电子邮件。
这些信息将在提交到远程仓库时使用,并且应该与您的 Github 个人资料凭据相匹配。

我们可以在全球范围内做到这一点。

git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

...或者在单个存储库中。

git config user.name "John Doe"
git config user.email johndoe@example.com

要检查您的信息,您可以执行以下操作

git config user.name
> Joe Doe
git config user.email
> johndoe@example.com

或者

git config --list

将其作为列表的一部分返回

. . .
. . .
. . .
user.name=John Doe
user.email=johndoe@example.com

git init

.git使用本地跟踪我们项目的文件夹初始化或重新初始化现有存储库。

git 添加 ./git 添加

当我们进行更改时,我们希望将其提交到本地存储库。但在此之前,我们必须将代码添加到暂存区。暂存区会跟踪即将提交的文件。

为了更好地理解 Git,你应该在脑海中形成以下框架。Git
有三种状态。第一种是文件被修改,这意味着你可能做了任何更改。第二种是文件被暂存,这意味着你已经准备好提交文件。最后一种是文件被提交,这意味着我们的更改已经存储到本地仓库,可以推送了。

要将单个文件添加到暂存区,请键入...

git add [name-of-file]
//ex  git add index.html

或者添加 .所有已更改的文件。

git add .

git commit -m“消息”

现在我们已经将代码添加到暂存区,可以提交文件了。我们还可以为其指定一个描述性的名称,例如“表单已添加”或“更新版本”等等。

git 状态

我们 git status可以看到文件当前处于哪个阶段。哪些文件被修改了,哪些文件在我们的暂存区或正在提交,以及我们当前在哪个分支。

git 日志

通过日志,我们可以看到从一开始到现在的所有提交。它还显示了提交的作者姓名和日期。

分支

我们也可以创建其他分支来处理项目的其他版本或部分内容。默认情况下,我们将所有代码提交到名为 的主分支 master。如有需要,我们可以将分支合并在一起。(稍后会介绍)

创建另一个分支。

git branch [name_of_branch]

git branch new_branch

切换分支。

git checkout [name_of_branch]

git checkout new_branch

使用一个命令创建一个新分支并切换到新分支。

git checkout -b [name_of_branch]

git checkout -b new_branch

删除分支。

git branch -D [name_of_branch]

git branch -D new_branch

...并查看所有分支的列表

 git show-branch

git合并

假设你在新分支上做了一些更改,并希望将其合并到主分支。前往主分支,运行合并命令,并传入你想要从中获取代码的分支名称。

git merge [new_branch]

// in master branch
git merge new_branch

git push

将代码提交到本地 Git 仓库后,您需要将代码推送到远程 Git 仓库,以便其他人可以查看和拉取您的代码。 git push我们需要指定 origin要推送的分支。

origin是我们远程 Git 仓库的 URL。类似这样。

// remote url https://github.com/[username]/[repository-name].git

git push origin master

git pull

现在假设我们正在与其他开发人员一起工作。每个人都在处理不同的工作,我们希望获得最新版本的源代码,以便每个人都能保持一致。在这种情况下,我们必须从远程 Git 仓库拉取代码,每个人都将代码推送到我们的本地 Git 仓库(在我们这里是 Github)。

git 克隆

使用 git clone 您可以在计算机本地复制远程存储库。

git clone https://github.com/[username]/[repository-name].git

git remote 设置 URL /git remote 添加

现在在某些情况下,您可能想要更改现有存储库的 URL,或者添加新的 URL,以便我们也可以将代码推送到第二个存储库。

添加远程 URL

git remote add origin https://github.com/[username]/[repository-name].git

注:名称来源是一种约定俗成的惯例。可以是任何名称,例如香蕉。

替换现有 URL

git remote set-url origin https://github.com/[username]/[repository-name].git

查看我们的存储库远程 URL 列表

git remote -v

origin  https://github.com/[username]/[repository-name].git (fetch)
origin  https://github.com/[username]/[repository-name].git (push)

如果您想添加第二个远程 URL,您仍然可以执行 git remote add,但使用不同的名称,而不是 origin。

git remote add banana https://github.com/[username]/[repository-name].git

再次列出我们的名单

git remote -v

origin https://github.com/[username]/[repository-name].git (fetch)
origin https://github.com/[username]/[repository-name].git (push)
banana https://github.com/[username]/[repository-name].git (fetch)
banana https://github.com/[username]/[repository-name].git (push)

git diff

展示我们的提交和工作树之间的区别。暂存区中有哪些内容等待提交。如果深入挖掘,你还能看到分支之间的区别等等。

git 重置

回到我们代码的某个特定版本。由于我们处理不同的状态,因此我们会针对每种情况使用不同的标志。

假设你做了一些更改并提交了它们。现在你想返回到之前提交的版本。由于每个提交都有一个唯一的编号,所以你可以这样做。

git reset --soft 9e5e6a4

通过添加此--soft标志,我们将返回到这些提交,但暂存区和工作目录将保持原样。如果我们执行此操作,git commit我们将从暂存区提交当前的更改。

git reset --mixed 9e5e6a4

如果我们做同样的事情,但使用--mixed标志,我们将再次返回直到该提交的暂存区域。

如果我们不采取git commit任何行动,那么什么也不会发生,因为暂存区与准备提交的内容相匹配。

git reset --hard 9e5e6a4

通过添加--hard标志,将应用相同的操作,但更改将返回到工作目录,就像没有添加或提交任何内容一样。

要了解有关重置的更多信息,您可以查看此处

使用 ssh

如果您不想在每次推送时都提示输入用户名和密码,您可以向您的 Github 设置提供一个 ssh 密钥,以便 Github 可以识别您。

生成新的 SSH 密钥类型

ssh-keygen

这将生成一个名为 和 的公共文件id_rsa.pub和一个id_rsa名为 的私有文件。它会询问您将文件保存在哪里。默认情况下,它会将它们保存在 users 文件夹中,文件夹名称为.ssh
前往您的 Github 个人资料设置,在侧边栏中查找SSH 和 GPG 密钥部分。
点击新建 SSH 密钥。在这里您可以命名并粘贴您的公共 SSH 密钥。

创建新仓库时,您可以选择将仓库的 URL 设置为HTTPSSSH
选择SSH,这样整个过程才能顺利进行。

如果您想要更改使用 创建的现有存储库的 URL HTTPS,您可以执行以下操作。

正如我们之前所见

git remote -v列出您的 repo 路径。

git remote -v

origin https://github.com/[username]/[repository-name].git (fetch)
origin https://github.com/[username]/[repository-name].git (push)

git remote set-url

git remote set-url origin git@github.com:USERNAME/REPOSITORY.gi

再次检查

git remote -v

origin git@github.com:[username]/[repository-name].git (fetch)
origin git@github.com:[username]/[repository-name].git (push)

现在每次你推送时,Github 都知道你是谁。

鏂囩珷鏉ユ簮锛�https://dev.to/john2220/basic-git-commands-you-should-know-51ij
PREV
Interview Questions on Node.js What is node.js? What are some key benefits to Nodejs? Is Node js single-threaded? If yes, then why? What type of applications you can build using Node js? How the content of a file is read by Node js? Discuss the streams in Nodejs? And what are the different types of streams? Streams are something that allows the reading and writing of data from source to destination in a continuous process. What is closure? Does Zlib use in Nodejs? If yes, then why? Discuss the globals in Node.js? Differentiate between Nodejs and Ajax? What is Modulus in Node Js?
NEXT
超级生产力:如何培养对时间跟踪和任务管理的兴趣