Git Switch 与 Git Checkout 的比较

GitGitBeginner
立即练习

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

介绍

在这个实验(lab)中,你将学习 git switchgit checkout 命令之间的关键区别。你将探索何时以及为什么使用每个命令,以及通过实际的例子和用例来帮助你简化你的 Git 工作流程。

理解 Git Checkout

git checkout 命令是一个基本的 Git 命令,用于切换分支或恢复工作树(working tree)文件。它会更新你工作目录中的文件,以匹配指定分支或提交中的版本。

让我们首先探索如何使用 git checkout 在分支之间切换。

首先,确保你在项目目录中:

cd ~/project

现在,让我们检查当前分支。默认情况下,在 git init 之后,你位于 mainmaster 分支(取决于你的 Git 配置)。在这个实验(lab)的设置中,我们创建了一个 feature-branch

git branch

你应该看到类似于这样的输出,用星号指示当前分支:

  feature-branch
* main

现在,使用 git checkout 切换到 feature-branch

git checkout feature-branch

你将看到确认切换的输出:

Switched to branch 'feature-branch'

让我们再次验证当前分支:

git branch

输出现在应该显示你在 feature-branch 上:

* feature-branch
  main

你已经成功使用 git checkout 切换分支。

理解 Git Switch

git switch 命令是 Git 2.23 中引入的一个较新的命令,专门用于切换分支。与功能过载的 git checkout 命令相比,它旨在提供更清晰的关注点分离(separation of concerns)。

让我们使用 git switch 切换回 main 分支。

确保你在项目目录中:

cd ~/project

现在,使用 git switch 切换到 main 分支:

git switch main

你将看到确认切换的输出:

Switched to branch 'main'

让我们再次验证当前分支:

git branch

输出现在应该显示你在 main 上:

* main
  feature-branch

你已经成功使用 git switch 切换分支。请注意,在切换分支时,输出与 git checkout 类似。

使用 Git Switch 创建和切换分支

git switch 的一个便捷功能是能够使用 -c(或 --create)选项,通过单个命令创建新分支并切换到该分支。

让我们创建一个名为 development 的新分支并切换到它。

确保你在项目目录中:

cd ~/project

现在,使用 git switch -c 创建并切换到 development 分支:

git switch -c development

你将看到输出,指示已创建新分支并且你已切换到该分支:

Switched to a new branch 'development'

让我们验证当前分支并列出所有分支:

git branch

输出应显示新的 development 分支,并指示你当前位于该分支上:

  feature-branch
  main
* development

这演示了 git switch -c 如何简化创建并立即在新分支上工作的过程。

使用 Git Checkout 恢复文件

虽然 git switch 主要用于切换分支,但 git checkout 保留了恢复文件的功能。这是它们预期用途的一个关键区别。

让我们对 file1.txt 进行一些更改,然后使用 git checkout 放弃这些更改,并将文件恢复到其在当前分支(development)中的状态。

确保你在项目目录中:

cd ~/project

file1.txt 添加一些内容:

echo "Additional content" >> file1.txt

检查文件的状态:

git status

输出将显示 file1.txt 已被修改:

On branch development
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:   file1.txt

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

现在,使用 git checkout -- 放弃对 file1.txt 的本地更改:

git checkout -- file1.txt

再次检查状态:

git status

输出现在应该显示工作目录中没有更改:

On branch development
nothing to commit, working tree clean

你对 file1.txt 所做的更改已被放弃,并且该文件已恢复到其在 development 分支中的状态。此功能由 git checkout 处理,而不是 git switch

使用 Git Checkout 检出特定提交

git checkout 保留的另一个功能是能够检出(check out)特定的提交(commit)。这会使你进入“分离 HEAD (detached HEAD)”状态,允许你在历史记录中的那个时间点检查项目。git switch 不具备此功能。

首先,让我们找到初始提交的提交哈希(commit hash)。

确保你在项目目录中:

cd ~/project

查看提交历史:

git log --oneline

你将看到类似于此的输出,其中包含提交哈希:

<commit_hash_development> (HEAD -> development) Initial commit
<commit_hash_main> (main, feature-branch) Initial commit

请注意,你的环境中的提交哈希将有所不同。复制“Initial commit”的提交哈希。

现在,使用 git checkout 后跟提交哈希来检出该特定提交。将 <commit_hash> 替换为你复制的实际哈希。

git checkout <commit_hash>

你将看到输出,指示你处于分离 HEAD 状态:

Note: switching to '<commit_hash>'

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

HEAD is now at <commit_hash> Initial commit

你现在正在查看项目在初始提交时的状态。要返回到分支,你可以使用 git switchgit checkout 切换回 developmentmain 等分支。

让我们使用 git switch 切换回 development 分支:

git switch development

你将看到确认切换的输出:

Switched to branch 'development'

你已成功使用 git checkout 探索了特定的提交,然后使用 git switch 返回到分支。

差异总结

总结 git checkoutgit switch 之间的主要区别:

  • git switch:主要用于在分支之间切换。它是一个较新的、更专注于此特定任务的命令。它还可以创建新分支并切换到该分支(git switch -c)。
  • git checkout:一个更通用的命令,可以切换分支,但也可以检出特定的提交(导致分离 HEAD)和恢复文件(git checkout -- <file>)。

虽然 git checkout 可以执行分支切换,但在较新的 Git 版本中,建议使用 git switch 来实现此目的,因为它具有更清晰的意图和关注点分离。当你需要恢复文件或探索特定提交时,请使用 git checkout

你现在已经探索了 git checkoutgit switch 的核心功能,并且了解了在你的 Git 工作流程中何时使用每个命令。

总结

在这个实验(Lab)中,你学习了 git switchgit checkout 命令之间的区别。你练习了使用这两个命令切换分支,使用 git switch 创建新分支,使用 git checkout 恢复文件,以及使用 git checkout 检出特定的提交(commit)。你现在了解了每个命令的预期用例,以及它们如何为高效的 Git 工作流程做出贡献。