Skip to content

fix(git): create a branch from a detached HEAD - #4955

Open
kabishou11 wants to merge 1 commit into
modelcontextprotocol:mainfrom
kabishou11:fix/git-create-branch-detached-head
Open

kabishou11 wants to merge 1 commit into
modelcontextprotocol:mainfrom
kabishou11:fix/git-create-branch-detached-head

Conversation

@kabishou11

Copy link
Copy Markdown

Description

git_create_branch without base_branch fails whenever the repository has a detached HEAD (after checking out a commit or tag, or in the middle of a rebase). It uses repo.active_branch as the start point, and GitPython raises TypeError: HEAD is a detached symbolic reference as it points to '<sha>' when there is no active branch. Creating a branch from a detached HEAD is exactly how you keep that work (git switch -c <name>), so the tool fails on a normal workflow.

This change branches from the current commit when HEAD is detached. The result message reports the commit SHA. Behaviour with a base_branch, or on a normal branch, is unchanged.

Server Details

  • Server: git
  • Changes to: tools (git_create_branch)

Motivation and Context

repo.git.checkout(repo.head.commit.hexsha)    # detach HEAD
git_create_branch(repo, "rescued-branch")
# before: TypeError: HEAD is a detached symbolic reference as it points to '1c2a66c...'
# after:  Created branch 'rescued-branch' from '1c2a66c...'

How Has This Been Tested?

  • New test test_git_create_branch_detached_head. It fails on main with the TypeError and passes with this change.
  • uv run pytest in src/git: 48 passed.
  • uv run pyright src: 0 errors. ruff check: clean.

Breaking Changes

None.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Protocol Documentation
  • My changes follows MCP security best practices
  • I have updated the server's README accordingly (not needed)
  • I have tested this with an LLM client
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have documented all environment variables and configuration options (none added)

Additional context

#4943 changes the same function for annotated tags given as base_branch. That is a different code path and the two changes don't overlap.

git_create_branch without base_branch used repo.active_branch, which
raises TypeError when HEAD is detached. Branch from the current commit
instead, like git switch -c does.
@changeset-bot

changeset-bot Bot commented Oct 3, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: a12eb29

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant