ChatGPT解决这个技术问题 Extra ChatGPT

使用单个命令进行分支和结帐

创建和使用新分支涉及两个命令:

$ git branch new_branch_name
$ git checkout new_branch_name

我倾向于忘记后者,这可能很烦人。有没有办法使用单个命令来做到这一点?也许使用别名或类似的东西?我知道我可以编写一个 shell 函数,但对于这样一个简单而常见的任务来说,这似乎有点费力。

Bazaar 使用 bzr branch --switch 表示法在一定程度上支持这一点。

由于缺乏研究工作而即将投反对票,但后来我意识到您正在回答自己的问题。

C
Community

在编写问题并在类似问题列表中找到“What is the difference between "git branch" and "git checkout -b"?”时,我自己找到了答案:

$ git checkout -b new_branch_name

我想我正在阅读错误命令的手册页,我希望这是 branch 命令的一部分,而不是 checkout。引用 checkout 的手册页:

指定 -b 会导致创建一个新分支,就像调用 git-branch(1) 然后签出一样。

正是我想要的。


P
P-Gn

Git 在 2.23 版中引入了 switch 来专门处理分支的更改,并避免使用 checkout,这可能会因它可以执行的大量操作而令人困惑。

在其他可能性中,

git switch <branch>  # to switch to an existing branch
git switch -c <new_branch>  # to create a new branch and switch to it

M
Maik Lowrey

Git 有两个 Oneliners。

git checkout -b new_branch_name。 git switch -c new_branch_name

在引擎盖下,两者都做同样的事情:

git branch new_branch_name
git checkout new_branch_name