Git Checkout说明:如何在Git中检出,更改或切换分支

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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值