The
git checkout
command switches between branches or restores working tree files. There are a number of different options for this command that won’t be covered here, but you can take a look at all of them in the
Git documentation
.
git checkout
命令在分支之间切换或还原工作树文件。 此命令有很多不同的选项,这里不会介绍,但是您可以在
Git文档中
查看所有这些选项。
检出特定的提交
(
Checkout a specific commit
)
to checkout a specific commit, run the command :
要检出特定的提交,请运行以下命令:
git checkout specific-commit-id
we can get the specific commit id’s by running:
我们可以通过运行以下命令获取特定的提交ID:
git log
结帐现有分支
(
Checkout an Existing Branch
)
To checkout an existing branch, run the command:
要签出现有分支,请运行以下命令:
git checkout BRANCH-NAME
Generally, Git won’t let you checkout another branch unless your working directory is clean, because you would lose any working directory changes that aren’t committed. You have three options to handle your changes: 1) trash them, 2)
commit them
, or 3)
stash them
.
通常,除非您的工作目录干净,否则Git不会让您签出另一个分支,因为您会丢失所有未提交的工作目录更改。 您有三种选择来处理您的更改:1)丢弃它们,2)
提交它们
,或3)
存放它们
。
结帐新分支
(
Checkout a New Branch
)
To create and checkout out a new branch with a single command, you can use:
要使用单个命令创建和签出新分支,可以使用:
git checkout -b NEW-BRANCH-NAME
This will automatically switch you to the new branch.
这将自动将您切换到新分支。
签出新分支或将分支重置为起点
(
Checkout a New Branch or Reset a Branch to a Start Point
)
The following command is similar to checking out a new branch, but uses the
-B
(note the captital B) flag and an optional
START-POINT
parameter:
以下命令类似于签出新分支,但使用
-B
(注意首字母B)标志和可选的
START-POINT
参数:
git checkout -B BRANCH-NAME START-POINT
If the
BRANCH-NAME
branch doesn’t exist, Git will create it and start it at
START-POINT
. If the
BRANCH-NAME
branch already exists, then Git resets the branch to
START-POINT
. This is equivalent to running
git branch
with
-f
.
如果
BRANCH-NAME
分支不存在,Git将创建它并在
START-POINT
处启动它。 如果
BRANCH-NAME
分支已经存在,则Git将分支重置为
START-POINT
。 这等效于使用
-f
运行
git branch
。
强制结帐
(
Force a Checkout
)
You can pass the
-f
or
--force
option with the
git checkout
command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from
HEAD
). Basically, it can be used to throw away local changes.
您可以在
git checkout
命令中传递
-f
或
--force
选项,以强制Git切换分支,即使您进行了未暂存的更改(换句话说,工作树的索引与
HEAD
也不相同)。 基本上,它可以用来丢弃本地更改。
When you run the following command, Git will ignore unmerged entries:
当您运行以下命令时,Git将忽略未合并的条目:
git checkout -f BRANCH-NAME
# Alternative
git checkout --force BRANCH-NAME
撤消工作目录中的更改
(
Undo Changes in your Working Directory
)
You can use the
git checkout
command to undo changes you’ve made to a file in your working directory. This will revert the file back to the version in
HEAD
:
您可以使用
git checkout
命令撤消对工作目录中的文件所做的更改。 这会将文件还原回
HEAD
的版本:
git checkout -- FILE-NAME
翻译自:
https://www.freecodecamp.org/news/git-checkout-explained/