← 返回命令列表

Linux command

git-checkout 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Switch to branch

git checkout [branch-name]

Create and switch to branch

git checkout -b [new-branch]

Restore a file

git checkout -- [file.txt]

Checkout specific commit

git checkout [commit-hash]

Checkout from remote

git checkout -t origin/[branch]

说明

git checkout switches branches or restores files. It updates the working tree to match the specified branch, commit, or file version from history. The command serves multiple purposes: branch switching, branch creation with -b, file restoration with --, and detached HEAD operations. Since Git 2.23, the recommended approach is git switch for branches and git restore for files, which provide clearer separation of concerns. Despite the newer alternatives, git checkout remains widely used for its versatility and is deeply embedded in existing workflows, documentation, and scripts.

参数

-b _BRANCH_
Create and switch to new branch.
-t, --track
Set up tracking for remote branch.
-- _FILE_
Restore file from index.
-f, --force
Force switch, discard changes.
--orphan _BRANCH_
Create orphan branch.
--help
Display help information.

FAQ

What is the git-checkout command used for?

git checkout switches branches or restores files. It updates the working tree to match the specified branch, commit, or file version from history. The command serves multiple purposes: branch switching, branch creation with -b, file restoration with --, and detached HEAD operations. Since Git 2.23, the recommended approach is git switch for branches and git restore for files, which provide clearer separation of concerns. Despite the newer alternatives, git checkout remains widely used for its versatility and is deeply embedded in existing workflows, documentation, and scripts.

How do I run a basic git-checkout example?

Run `git checkout [branch-name]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -b _BRANCH_ do in git-checkout?

Create and switch to new branch.