git switch 和 git checkout <branch> 有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/57265785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:16:55  来源:igfitidea点击:

What's the difference between git switch and git checkout <branch>

gitgit-checkout

提问by Bastian Venthur

Git 2.23 introducesa new command git switch-- after reading the docs, it seems pretty much the same as git checkout <branchname>can someone explain the difference or use case?

Git 2.23引入了一个新命令git switch——阅读文档后,它似乎与git checkout <branchname>有人可以解释差异或用例几乎相同吗?

Two new commands "git switch" and "git restore" are introduced to split "checking out a branch to work on advancing its history" and "checking out paths out of the index and/or a tree-ish to work on advancing the current history" out of the single "git checkout" command.

引入了两个新命令“git switch”和“git restore”来拆分“签出分支以推进其历史记录”和“签出索引和/或树状的路径以推进当前的历史”来自单个“git checkout”命令。

回答by kowsky

Well, according to the documentation you link to, its sole purpose is to split and clarify the two different uses of git checkout:

好吧,根据您链接到的文档,其唯一目的是拆分和阐明 的两种不同用途git checkout

  • git switchcan now be used to change branches, as git checkout <branchname>does
  • git restorecan be used to reset files to certain revisions, as git checkout --<path_to_file>does
  • git switch现在可以用来改变分支机构,git checkout <branchname>
  • git restore可用于重置文件到某些修改,如git checkout --<path_to_file>

People are confused by these different ways to use git checkout, as you can see from the many questions regarding git checkouthere on Stackoverflow. Git developers seem to have taken this into account.

人们对这些不同的使用方式感到困惑git checkout,正如您从git checkoutStackoverflow 上有关此处的许多问题中可以看出的那样。Git 开发人员似乎已经考虑到了这一点。

回答by SteveTurczyn

git checkoutis a bit of a swiss army knife in that has several unrelated uses.

git checkout有点像瑞士军刀,有几个不相关的用途。

If you modify a file but haven't staged the change, then git checkout <filename>will reverse the modifications... a quick and easy way to cancel changes to a file. You remain in the same branch.

如果您修改了文件但尚未暂存更改,git checkout <filename>则将撤消修改……这是一种取消对文件更改的快速简便的方法。你留在同一个分支。

git checkout <branchname>(as you noted) switches branches.

git checkout <branchname>(正如您所指出的)切换分支。

Two completely different purposes, which could lead to confusion if a file name and a branch name are similar.

两个完全不同的目的,如果文件名和分支名相似,可能会导致混淆。

Having it as two commands is clearer.

将其作为两个命令会更清楚。