首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Git:为什么删除后我仍然可以切换到一个分支?

Git:为什么删除后我仍然可以切换到一个分支?
EN

Stack Overflow用户
提问于 2022-08-08 08:48:26
回答 4查看 142关注 0票数 2

我刚刚注意到,在删除它之后,我仍然可以切换回一个分支。下面是我的一个具体例子:

代码语言:javascript
复制
git switch master
git branch -d AB-10/add_flights
...
// deleting also the remote branch on GitHub

git switch AB-10/add_flights

// I see the branch again

怎么会这样?

EN

回答 4

Stack Overflow用户

发布于 2022-08-08 09:00:56

原因很简单:您只在本地删除了它。

手册页报告:

代码语言:javascript
复制
Optionally a new branch could be created with either -c, -C, automatically from a remote branch of same name (see --guess), or detach the working tree from any branch with
       --detach, along with switching.

由于它仍然存在于远程,所以GIT可以再次切换到它。

通过git push --delete origin <branch-name>删除它,然后再试着检查它.

票数 2
EN

Stack Overflow用户

发布于 2022-08-08 09:56:28

git switch是一个新引入的命令,用于从git checkout中承担一些负担。

根据git结帐的手册,

git结帐

要准备工作,可以通过更新索引和工作树中的文件,并将头指向分支来切换到它。保存对工作树中文件的本地修改,以便将它们提交给。 如果没有找到,但确实在一个具有匹配名称的远程(调用它)中存在一个跟踪分支,并且没有指定--无猜测,则视为等效于

代码语言:javascript
复制
$ git checkout -b <branch> --track <remote>/<branch>

因此,在删除分支并运行git checkout <branch>之后,将再次从<remote>/<branch>创建分支。

它也适用于git switch

票数 2
EN

Stack Overflow用户

发布于 2022-08-08 08:59:13

一个好的做法是在删除远程和本地分支之后进行修剪,您可以这样做。

代码语言:javascript
复制
git fetch --prune

这将删除删除远程分支的所有本地副本。

来自man页面:

代码语言:javascript
复制
   -p, --prune
       Before fetching, remove any remote-tracking references that no longer exist on the remote.
       Tags are not subject to pruning if they are fetched only because of the default tag
       auto-following or due to a --tags option. However, if tags are fetched due to an explicit
       refspec (either on the command line or in the remote configuration, for example if the
       remote was cloned with the --mirror option), then they are also subject to pruning.
       Supplying --prune-tags is a shorthand for providing the tag refspec.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73275144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档