Git 命令终极教程(第二部分)

2025-06-07

Git 命令终极教程(第二部分)

在上一篇 Git 命令文章中,您学习了如何使用Git 选项以及它们的作用。今天,我们将扩展知识范围,探索所有常用的 Git 命令

Git 命令

Git 命令用于访问 Git 工作目录并将其连接到我们的远程存储库,以便进行更改、查看不同的文件以及实现许多其他功能!设置好项目后,请务必将其与 Git 连接。

通过以下命令和简短说明,我们将确保您随时可以使用 Git。有关所有GIT 命令的详细说明,请查看git 的官方说明

$git config
您可以使用它来配置作者的姓名、电子邮件地址、文件格式以及更多与您的提交一起使用的信息。

git config --global user.name "Kolosek
git config --global user.email "kolosek@example.com"
Enter fullscreen mode Exit fullscreen mode

$git init
使用此命令可以确保你的git 仓库已初始化,并在新项目或现有项目中创建初始.git目录。输出如下:

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

$git init您可以使用撤消rm -rf .git

$git clone <path>
这将从远程源创建Git 仓库的工作副本到本地仓库。这是克隆 Git 仓库时要使用的第一个命令。

git clone /path/repository
Enter fullscreen mode Exit fullscreen mode

此外,您可以将原始位置添加为远程位置,以便您可以轻松地再次从中获取数据并在拥有权限的情况下推送。项目克隆完成后,您就可以开始工作了。编写您的RSpec 测试

git clone git@github:user/repository.git
Enter fullscreen mode Exit fullscreen mode

您可以一次克隆一个特定分支git clone -b <branch_name><repository_url>::

git clone -b branch_name git@github:user/repository.git
Enter fullscreen mode Exit fullscreen mode

$git add <file_name>
将工作目录中的一个或多个文件添加到索引中。

$git commit
将索引中写入的 所有更改-m连同消息一起带到 HEAD 分支。

git commit -m "Commit these changes."
Enter fullscreen mode Exit fullscreen mode

您还可以提交已添加到的任何文件git add,以及自那时以来更改的任何文件:

git commit -a
Enter fullscreen mode Exit fullscreen mode

始终提交所有更改,即使只是一堆Capybara 测试

$git status
它显示了索引和工作目录文件之间的状态差异。列出您已更改的文件,未跟踪的文件(因为它们仅在您的工作目录中)以及已暂存的文件(因为它们已准备好提交)。

On branch master
Initial commit

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

    File_name

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

$git remote
显示存储库的所有远程版本。

$git checkout <branch_name>:您可以从现有分支切换到另一个分支,或者创建新分支并切换到该分支git checkout -b <branch_name>

$git branch
通过它,您可以简单地列出所有现有分支,包括使用远程分支,-a或者如果提供了分支名称,则可以创建一个新分支。

$git push
所有更改推送到远程存储库。

git push origin <branch_name>
Enter fullscreen mode Exit fullscreen mode

您还可以从远程存储库中删除分支:

git push origin :<branch_name>
Enter fullscreen mode Exit fullscreen mode

$git pull
获取远程存储库中的更改并将其合并到您的工作目录。

$git merge <branch_name>
将一个或多个分支合并到您的活动分支中,如果没有冲突,它将自动创建一个新的提交。

在 Kolosek,我们将所有更改提交到 Git,并确保在与应用程序其他部分合并时通知我们的团队!尝试使用Rails 关联创建您自己的应用程序。

$git diff
显示工作树与索引之间的更改、两个分支之间的更改,或磁盘上两个文件之间的更改。例如,显示分支之间的更改:

git diff <source_branch> <target_branch>
Enter fullscreen mode Exit fullscreen mode

$git reset
将您的索引和工作目录重置为上次提交的状态。

git reset --hard origin/master
Enter fullscreen mode Exit fullscreen mode

git reset --hard还会撤消您目前所做的更改!git reset --soft如果您想保留更改,请使用。

$git revert
恢复的工作方式与非常相似$git reset,但它不会重置,而是创建一个新的提交,以撤销意外提交引入的所有内容。

$git tag
您可以使用标记来标记所做的重大更改,例如发布。

git tag 1.0.0 <commit_id>
Enter fullscreen mode Exit fullscreen mode

请务必为新发布的产品版本添加标签。不妨尝试一下CarrierWave 的实现,并为其添加发布标签!

$git log
它显示了分支上的提交列表及其相应的详细信息。

commit 134808af7c596be8d92c619f9efb94542874e1e3
Author: Kolosek <kolosek@example.com>
Date:   Fri Mar 23 14:24:54 2018 +0100

    [#1] First Commit
Enter fullscreen mode Exit fullscreen mode

结论

至此,我们已经涵盖了所有关于 Git 命令的知识。别忘了,这些只是一些最基础、最常用的命令。希望这些内容能帮助你快速上手 Git!

这篇文章最初发表在Kolosek Blog上。

文章来源:https://dev.to/neshaz/git-commands-ultimate-tutorial-part-2-7ko
PREV
Git Merge 与 Git Rebase
NEXT
使用 Git 进行调试