与 pygit2 合并时 HEAD 从 origin/master 分离

HEAD detatched from origin/master when merging with pygit2

我正在使用 pygit2 来合并项目的一些分支,但是每当我合并它们时,我都会得到:

def avoid_walls(directions, board, snake):
<<<<<<< HEAD
return directions
=======
z = 1
moves = []
for direction in directions:
    new_x = snake[0][0] + dirs[direction][0]
    new_y = snake[0][1] + dirs[direction][1]
    if new_x >= 0 and new_x < len(board[0]) and new_y >= 0 and new_y < len(board):
        moves.append(direction)
return moves
>>>>>>> 357f58b6d1682760b2fa9bf7b2418da347ca353c

在我的代码中。当我用 'git status' 检查回购协议时,我发现:

HEAD detached from origin/master
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

据我所知,我做的一切都很好,所以我无法弄清楚为什么 HEAD 会分离。据我了解,当您签出特定提交而不是分支时,HEAD 是分离的,我没有这样做:

# clone repo to local directory
repo = pygit2.clone_repository(my_repo, my_dir)

# checkout master branch (checked out by default, but just to be safe)
repo.checkout('refs/remotes/origin/master')

# merge with other branches
repo.merge(repo.branches['origin/branch1'].target)

# commit changes
index = repo.index
index.add_all()
index.write()
author = pygit2.Signature("ME", "me@domain.com")
commiter = pygit2.Signature("ME", "me@domain.com")
tree = index.write_tree()
oid = repo.create_commit('refs/heads/master', author, commiter, "init commit", tree, [repo.head.target, repo.branches['origin/branch1'].target])

#some tests i tried to fix the issue
repo.head.set_target(oid)
repo.apply(oid)

合并后我是否遗漏了一些可以完成提交并解决此问题的内容?

refs/remotes/origin/master 不是分支。所有分支都以 refs/heads/:

开头
if name.startswith('refs/heads/'):
    print('{} is a branch name'.format(name))
else
    print('{} is not a branch name'.format(name))

在这种情况下,因为它以 refs/remotes/ 开头,所以它是一个 remote-tracking 名称(Git 文档通常称其为 remote-tracking 分支名称,但我认为这太误导了,因为它包含 branch 这个词,尽管它不是 branch 姓名)。

当您签出 remote-tracking 名称、标签名称或任何不是 branch 名称的名称时,您实际上是在签出特定的提交并且会得到一个分离的 HEAD.