From fc46fb68d66698cc4bbcb5d60b8c5e713eb731fe Mon Sep 17 00:00:00 2001 From: luojian Date: Fri, 1 Aug 2025 15:48:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=87=E5=88=86=E6=94=AF=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/project_init.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/scripts/project_init.py b/scripts/project_init.py index 0ab37fa..27bafb3 100644 --- a/scripts/project_init.py +++ b/scripts/project_init.py @@ -34,11 +34,16 @@ class ProjectInit(Task): repo.git.fetch(remote_name) if repo.active_branch.name != branch_name: - # 2. 创建本地分支并跟踪远程分支 - remote_branch_ref = f"{remote_name}/{branch_name}" - local_branch = repo.create_head(branch_name, remote_branch_ref) # 创建本地分支指向远程 - local_branch.set_tracking_branch(repo.remotes[remote_name].refs[branch_name]) # 设置跟踪 - local_branch.checkout() # 切换到该分支 + # 检查本地是否已存在该分支 + if branch_name in repo.heads: + # 本地分支已存在,直接切换 + repo.heads[branch_name].checkout() + else: + # 2. 创建本地分支并跟踪远程分支 + remote_branch_ref = f"{remote_name}/{branch_name}" + local_branch = repo.create_head(branch_name, remote_branch_ref) # 创建本地分支指向远程 + local_branch.set_tracking_branch(repo.remotes[remote_name].refs[branch_name]) # 设置跟踪 + local_branch.checkout() # 切换到该分支 self.context.local_repo_branch = repo.active_branch.name self.context.local_repo_commit = repo.head.commit.hexsha[:10]