Skip to content

git checkout & git 切换

切换分支 and restoring files 使用 git checkout and newer git 切换 command.

你将学到

  • 如何 切换之间分支使用 git checkout and git 切换
  • Restoring files 使用 git checkout -- and git restore
  • 创建新 分支 and 切换到 它
  • Detached HEAD 状态 and 如何 avoid 它

概念讲解

Switching Branches

git checkout is the traditional command for switching branches:

git checkout main
git checkout feature-login

Git 2.23 introduced git 切换 as a clearer, dedicated command for branch switching:

git switch main
git switch feature-login

git 切换 is preferred for new scripts and learning because it does only one thing (switch branches), while git checkout is overloaded with multiple meanings.

Creating and Switching

# Old way — create and switch
git checkout -b new-feature

# New way — create and switch
git switch -c new-feature

# Create a new branch from a specific commit
git switch -c hotfix abc1234

Restoring Files

git checkout can also restore files to a previous state, which is confusing because the same command does two different things. The newer git restore command is clearer:

# Discard uncommitted changes to a file
git checkout -- style.css
# or:
git restore style.css

# Restore a file from a specific commit
git restore --source=HEAD~1 style.css

Warning: restoring discards your uncommitted changes permanently. There is no undo.

Detached HEAD

If you checkout a commit hash directly (not a branch name), you enter detached HEAD state:

git checkout abc1234
# You are now in 'detached HEAD' state

In this state, any commits you make are not on any branch and can be lost. If you want to explore an old commit and make changes, create a branch first:

git switch -c explore-old-commit abc1234

If you accidentally entered detached HEAD and made commits, you can save them by creating a branch:

git switch -c my-rescued-branch

Checking Out Remote Branches

When you fetch a remote branch, you can switch to it and Git creates a local tracking branch automatically:

git switch origin/feature-remote
# or:
git checkout feature-remote

The Mental Model

Think of git 切换 as "更改哪里 I 是" and git restore as "更改什么 my files look 如". The old git checkout does both, which is why the Git team split it into two commands.

示例代码

              # Switch to an existing branch
git checkout main
git switch main

# Create a new branch and switch to it
git checkout -b feature-api
git switch -c feature-api

# Switch back to the previous branch (like cd -)
git switch -

# List all branches and switch
git branch -a
git switch feature-login

# Discard changes to a file (restore from last commit)
git checkout -- style.css
git restore style.css

# Restore a file from 2 commits ago
git restore --source=HEAD~2 config.js

# Restore all modified files (be careful!)
git restore .

# Checkout a remote branch (creates local tracking branch)
git switch origin/feature-remote
# or:
git checkout -b feature-remote origin/feature-remote

# If you are in detached HEAD, rescue your commits
git switch -c my-new-branch

# View a file as it was in a previous commit
git show HEAD~3:src/app.js
            

Covers 切换分支, 创建 and 切换, convenient 切换 - shortcut, restoring files, 检查 out remote 分支, and rescuing 提交从 detached HEAD. modern git 切换 and git restore commands 是 clearer than overloaded git checkout.

动手试一试

常见错误

错误写法

检查 out 提交 hash and 制作提交 in detached HEAD 状态

正确做法

检查 out 提交 (不分支) puts 你 in detached HEAD. 提交制作这里是 不 on 任何分支 and 能是 lost. 如果你 需要到 制作更改从 旧提交, 创建分支首先: git 切换 -c 新-分支 <提交>. 如果你 already committed in detached HEAD, rescue 使用 git 切换 -c rescue-分支.

错误写法

Using git checkout -- file 到 discard 更改 and 然后 realizing 你需要它们

正确做法

git checkout -- file (or git restore file) permanently discards uncommitted 更改使用否 撤销. 如果你 可能需要那些更改, stash 它们首先使用 git stash. 仅 restore 当你 是确定更改是 不需要.

相关速查表

练习 git checkout & git 切换

2 个练习

练习 git checkout & git 切换

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误